2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2017-05-03 09:27:11 +08:00
|
|
|
from django.contrib.gis.gdal import Driver, GDALException
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
valid_drivers = (
|
|
|
|
# vector
|
|
|
|
'ESRI Shapefile', 'MapInfo File', 'TIGER', 'S57', 'DGN', 'Memory', 'CSV',
|
|
|
|
'GML', 'KML',
|
|
|
|
# raster
|
2014-12-17 22:30:12 +08:00
|
|
|
'GTiff', 'JPEG', 'MEM', 'PNG',
|
2014-12-03 00:53:04 +08:00
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
invalid_drivers = ('Foo baz', 'clucka', 'ESRI Shp', 'ESRI rast')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
aliases = {
|
|
|
|
'eSrI': 'ESRI Shapefile',
|
|
|
|
'TigER/linE': 'TIGER',
|
|
|
|
'SHAPE': 'ESRI Shapefile',
|
|
|
|
'sHp': 'ESRI Shapefile',
|
|
|
|
'tiFf': 'GTiff',
|
|
|
|
'tIf': 'GTiff',
|
|
|
|
'jPEg': 'JPEG',
|
|
|
|
'jpG': 'JPEG',
|
|
|
|
}
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class DriverTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test01_valid_driver(self):
|
2014-12-03 00:53:04 +08:00
|
|
|
"Testing valid GDAL/OGR Data Source Drivers."
|
2008-08-06 02:13:06 +08:00
|
|
|
for d in valid_drivers:
|
|
|
|
dr = Driver(d)
|
|
|
|
self.assertEqual(d, str(dr))
|
|
|
|
|
|
|
|
def test02_invalid_driver(self):
|
2014-12-03 00:53:04 +08:00
|
|
|
"Testing invalid GDAL/OGR Data Source Drivers."
|
2008-08-06 02:13:06 +08:00
|
|
|
for i in invalid_drivers:
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Driver(i)
|
2008-08-06 02:13:06 +08: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 19:59:25 +08: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.
|
|
|
|
"""
|
|
|
|
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)
|