diff --git a/django/contrib/gis/gdal/driver.py b/django/contrib/gis/gdal/driver.py index 51d0c9fe33..030e3b162b 100644 --- a/django/contrib/gis/gdal/driver.py +++ b/django/contrib/gis/gdal/driver.py @@ -76,10 +76,11 @@ class Driver(GDALBase): """ Attempts to register all the data source drivers. """ - # Only register all if the driver count is 0 (or else all drivers + # Only register all if the driver counts are 0 (or else all drivers # will be registered over and over again) - if not cls.driver_count(): + if not vcapi.get_driver_count(): vcapi.register_all() + if not rcapi.get_driver_count(): rcapi.register_all() @classmethod diff --git a/tests/gis_tests/gdal_tests/test_driver.py b/tests/gis_tests/gdal_tests/test_driver.py index 1efefcf131..74d6019db6 100644 --- a/tests/gis_tests/gdal_tests/test_driver.py +++ b/tests/gis_tests/gdal_tests/test_driver.py @@ -1,6 +1,7 @@ import unittest from django.contrib.gis.gdal import HAS_GDAL +from django.test import mock if HAS_GDAL: from django.contrib.gis.gdal import Driver, GDALException @@ -48,3 +49,32 @@ class DriverTest(unittest.TestCase): for alias, full_name in aliases.items(): dr = Driver(alias) self.assertEqual(full_name, str(dr)) + + @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)