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
|
|
|
|
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.vcapi.get_driver_count")
|
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.rcapi.get_driver_count")
|
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.vcapi.register_all")
|
|
|
|
@mock.patch("django.contrib.gis.gdal.driver.rcapi.register_all")
|
|
|
|
def test_registered(self, rreg, vreg, rcount, vcount):
|
|
|
|
"""
|
|
|
|
Prototypes are registered only if their respective driver counts are
|
|
|
|
zero.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-08-22 11:59:25 +00:00
|
|
|
def check(rcount_val, vcount_val):
|
|
|
|
vreg.reset_mock()
|
|
|
|
rreg.reset_mock()
|
|
|
|
rcount.return_value = rcount_val
|
|
|
|
vcount.return_value = vcount_val
|
|
|
|
Driver.ensure_registered()
|
|
|
|
if rcount_val:
|
|
|
|
self.assertFalse(rreg.called)
|
|
|
|
else:
|
|
|
|
rreg.assert_called_once_with()
|
|
|
|
if vcount_val:
|
|
|
|
self.assertFalse(vreg.called)
|
|
|
|
else:
|
|
|
|
vreg.assert_called_once_with()
|
|
|
|
|
|
|
|
check(0, 0)
|
|
|
|
check(120, 0)
|
|
|
|
check(0, 120)
|
|
|
|
check(120, 120)
|