| 1 |
import unittest |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.contrib.gis.gdal import Driver, GDALException |
| 5 |
|
| 6 |
valid_drivers = ( |
| 7 |
# vector |
| 8 |
"ESRI Shapefile", |
| 9 |
"MapInfo File", |
| 10 |
"TIGER", |
| 11 |
"S57", |
| 12 |
"DGN", |
| 13 |
"Memory", |
| 14 |
"CSV", |
| 15 |
"GML", |
| 16 |
"KML", |
| 17 |
# raster |
| 18 |
"GTiff", |
| 19 |
"JPEG", |
| 20 |
"MEM", |
| 21 |
"PNG", |
| 22 |
) |
| 23 |
|
| 24 |
invalid_drivers = ("Foo baz", "clucka", "ESRI Shp", "ESRI rast") |
| 25 |
|
| 26 |
aliases = { |
| 27 |
"eSrI": "ESRI Shapefile", |
| 28 |
"TigER/linE": "TIGER", |
| 29 |
"SHAPE": "ESRI Shapefile", |
| 30 |
"sHp": "ESRI Shapefile", |
| 31 |
"tiFf": "GTiff", |
| 32 |
"tIf": "GTiff", |
| 33 |
"jPEg": "JPEG", |
| 34 |
"jpG": "JPEG", |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
class DriverTest(unittest.TestCase): |
| 39 |
def test01_valid_driver(self): |
| 40 |
"Testing valid GDAL/OGR Data Source Drivers." |
| 41 |
for d in valid_drivers: |
| 42 |
dr = Driver(d) |
| 43 |
self.assertEqual(d, str(dr)) |
| 44 |
|
| 45 |
def test02_invalid_driver(self): |
| 46 |
"Testing invalid GDAL/OGR Data Source Drivers." |
| 47 |
for i in invalid_drivers: |
| 48 |
with self.assertRaises(GDALException): |
| 49 |
Driver(i) |
| 50 |
|
| 51 |
def test03_aliases(self): |
| 52 |
"Testing driver aliases." |
| 53 |
for alias, full_name in aliases.items(): |
| 54 |
dr = Driver(alias) |
| 55 |
self.assertEqual(full_name, str(dr)) |
| 56 |
|
| 57 |
@mock.patch("django.contrib.gis.gdal.driver.capi.get_driver_count") |
| 58 |
@mock.patch("django.contrib.gis.gdal.driver.capi.register_all") |
| 59 |
def test_registered(self, reg, count): |
| 60 |
""" |
| 61 |
Prototypes are registered only if the driver count is zero. |
| 62 |
""" |
| 63 |
|
| 64 |
def check(count_val): |
| 65 |
reg.reset_mock() |
| 66 |
count.return_value = count_val |
| 67 |
Driver.ensure_registered() |
| 68 |
if count_val: |
| 69 |
self.assertFalse(reg.called) |
| 70 |
else: |
| 71 |
reg.assert_called_once_with() |
| 72 |
|
| 73 |
check(0) |
| 74 |
check(120) |