2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2013-07-01 12:22:27 +00:00
|
|
|
|
2017-05-03 01:27:11 +00:00
|
|
|
from django.contrib.gis.gdal import Driver, GDALException
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2014-12-02 16:53:04 +00:00
|
|
|
valid_drivers = (
|
|
|
|
# vector
|
|
|
|
"ESRI Shapefile",
|
|
|
|
"MapInfo File",
|
|
|
|
"TIGER",
|
|
|
|
"S57",
|
|
|
|
"DGN",
|
|
|
|
"Memory",
|
|
|
|
"CSV",
|
|
|
|
"GML",
|
|
|
|
"KML",
|
|
|
|
# raster
|
2014-12-17 14:30:12 +00:00
|
|
|
"GTiff",
|
|
|
|
"JPEG",
|
|
|
|
"MEM",
|
|
|
|
"PNG",
|
2014-12-02 16:53:04 +00:00
|
|
|
)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2014-12-02 16:53:04 +00:00
|
|
|
invalid_drivers = ("Foo baz", "clucka", "ESRI Shp", "ESRI rast")
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2014-12-02 16:53:04 +00:00
|
|
|
aliases = {
|
|
|
|
"eSrI": "ESRI Shapefile",
|
|
|
|
"TigER/linE": "TIGER",
|
|
|
|
"SHAPE": "ESRI Shapefile",
|
|
|
|
"sHp": "ESRI Shapefile",
|
|
|
|
"tiFf": "GTiff",
|
|
|
|
"tIf": "GTiff",
|
|
|
|
"jPEg": "JPEG",
|
|
|
|
"jpG": "JPEG",
|
|
|
|
}
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2013-05-11 03:08:45 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
class DriverTest(unittest.TestCase):
|
|
|
|
def test01_valid_driver(self):
|
2014-12-02 16:53:04 +00:00
|
|
|
"Testing valid GDAL/OGR Data Source Drivers."
|
2008-08-05 18:13:06 +00:00
|
|
|
for d in valid_drivers:
|
|
|
|
dr = Driver(d)
|
|
|
|
self.assertEqual(d, str(dr))
|
|
|
|
|
|
|
|
def test02_invalid_driver(self):
|
2014-12-02 16:53:04 +00:00
|
|
|
"Testing invalid GDAL/OGR Data Source Drivers."
|
2008-08-05 18:13:06 +00:00
|
|
|
for i in invalid_drivers:
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Driver(i)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
def test03_aliases(self):
|
|
|
|
"Testing driver aliases."
|
|
|
|
for alias, full_name in aliases.items():
|
|
|
|
dr = Driver(alias)
|
|
|
|
self.assertEqual(full_name, str(dr))
|
2016-08-22 11:59:25 +00:00
|
|
|
|
2023-03-13 17:38:23 +00:00
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.capi.get_driver_count")
|
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.capi.register_all")
|
|
|
|
def test_registered(self, reg, count):
|
2016-08-22 11:59:25 +00:00
|
|
|
"""
|
2023-03-13 17:38:23 +00:00
|
|
|
Prototypes are registered only if the driver count is zero.
|
2016-08-22 11:59:25 +00:00
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2023-03-13 17:38:23 +00:00
|
|
|
def check(count_val):
|
|
|
|
reg.reset_mock()
|
|
|
|
count.return_value = count_val
|
2016-08-22 11:59:25 +00:00
|
|
|
Driver.ensure_registered()
|
2023-03-13 17:38:23 +00:00
|
|
|
if count_val:
|
|
|
|
self.assertFalse(reg.called)
|
2016-08-22 11:59:25 +00:00
|
|
|
else:
|
2023-03-13 17:38:23 +00:00
|
|
|
reg.assert_called_once_with()
|
2016-08-22 11:59:25 +00:00
|
|
|
|
2023-03-13 17:38:23 +00:00
|
|
|
check(0)
|
|
|
|
check(120)
|