diff --git a/django/contrib/gis/models.py b/django/contrib/gis/models.py index 3231f926ff..d3e8dbdb83 100644 --- a/django/contrib/gis/models.py +++ b/django/contrib/gis/models.py @@ -211,9 +211,23 @@ class SpatialRefSysMixin(object): # The SpatialRefSys and GeometryColumns models _srid_info = True if settings.DATABASE_ENGINE == 'postgresql_psycopg2': - from django.contrib.gis.db.backend.postgis.models import GeometryColumns, SpatialRefSys + # Because the PostGIS version is checked when initializing the spatial + # backend a `ProgrammingError` will be raised if the PostGIS tables + # and functions are not installed. We catch here so it won't be raised when + # running the Django test suite. + from psycopg2 import ProgrammingError + try: + from django.contrib.gis.db.backend.postgis.models import GeometryColumns, SpatialRefSys + except ProgrammingError: + _srid_info = False elif settings.DATABASE_ENGINE == 'oracle': - from django.contrib.gis.db.backend.oracle.models import GeometryColumns, SpatialRefSys + # Same thing as above, except the GEOS library is attempted to be loaded for + # `BaseSpatialBackend`, and an exception will be raised during the + # Django test suite if it doesn't exist. + try: + from django.contrib.gis.db.backend.oracle.models import GeometryColumns, SpatialRefSys + except: + _srid_info = False else: _srid_info = False diff --git a/django/contrib/gis/tests/__init__.py b/django/contrib/gis/tests/__init__.py index 44465f4f23..9bf1ccd936 100644 --- a/django/contrib/gis/tests/__init__.py +++ b/django/contrib/gis/tests/__init__.py @@ -44,8 +44,13 @@ if HAS_GEOIP: if hasattr(settings, 'GEOIP_PATH'): test_suite_names.append('test_geoip') -def suite(): - "Builds a test suite for the GIS package." +def geo_suite(): + """ + Builds a test suite for the GIS package. This is not named + `suite` so it will not interfere with the Django test suite (since + spatial database tables are required to execute these tests on + some backends). + """ s = TestSuite() for test_suite in test_suite_names: tsuite = getattr(__import__('django.contrib.gis.tests', globals(), locals(), [test_suite]),test_suite) @@ -54,7 +59,7 @@ def suite(): def run(verbosity=1): "Runs the tests that do not require geographic (GEOS, GDAL, etc.) models." - TextTestRunner(verbosity=verbosity).run(suite()) + TextTestRunner(verbosity=verbosity).run(geo_suite()) def run_tests(module_list, verbosity=1, interactive=True): """ @@ -105,7 +110,7 @@ def run_tests(module_list, verbosity=1, interactive=True): # Creating the test suite, adding the test models to INSTALLED_APPS, and # adding the model test suites to our suite package. - test_suite = suite() + test_suite = geo_suite() for test_model in test_models: module_name = 'django.contrib.gis.tests.%s' % test_model if mysql: