diff options
Diffstat (limited to 'django/contrib/gis/tests')
-rw-r--r-- | django/contrib/gis/tests/__init__.py | 209 | ||||
-rw-r--r-- | django/contrib/gis/tests/geogapp/tests.py | 4 | ||||
-rw-r--r-- | django/contrib/gis/tests/relatedapp/models.py | 5 | ||||
-rw-r--r-- | django/contrib/gis/tests/relatedapp/tests.py | 10 |
4 files changed, 119 insertions, 109 deletions
diff --git a/django/contrib/gis/tests/__init__.py b/django/contrib/gis/tests/__init__.py index 0d862190c0..44d62c2a98 100644 --- a/django/contrib/gis/tests/__init__.py +++ b/django/contrib/gis/tests/__init__.py @@ -1,115 +1,108 @@ import sys +import unittest + +from django.conf import settings +from django.db.models import get_app +from django.test.simple import build_suite, DjangoTestSuiteRunner def run_tests(*args, **kwargs): from django.test.simple import run_tests as base_run_tests return base_run_tests(*args, **kwargs) -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). - """ - from django.conf import settings - from django.contrib.gis.geos import GEOS_PREPARE - from django.contrib.gis.gdal import HAS_GDAL - from django.contrib.gis.utils import HAS_GEOIP - from django.contrib.gis.tests.utils import postgis, mysql - from django.db import connection - from django.utils.importlib import import_module - - gis_tests = [] - - # Adding the GEOS tests. - from django.contrib.gis.geos import tests as geos_tests - gis_tests.append(geos_tests.suite()) - - # Tests that require use of a spatial database (e.g., creation of models) - test_apps = ['geoapp', 'relatedapp'] - if postgis and connection.ops.geography: - # Test geography support with PostGIS 1.5+. - test_apps.append('geogapp') - - # Tests that do not require setting up and tearing down a spatial database. - test_suite_names = [ - 'test_measure', - ] - - if HAS_GDAL: - # These tests require GDAL. - if not mysql: - test_apps.append('distapp') - - # Only PostGIS using GEOS 3.1+ can support 3D so far. - if postgis and GEOS_PREPARE: - test_apps.append('geo3d') - - test_suite_names.extend(['test_spatialrefsys', 'test_geoforms']) - test_apps.append('layermap') +def run_gis_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=None): + import warnings + warnings.warn( + 'The run_gis_tests() test runner has been deprecated in favor of GeoDjangoTestSuiteRunner.', + PendingDeprecationWarning + ) + test_runner = GeoDjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) + return test_runner.run_tests(test_labels, extra_tests=extra_tests) + +class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner): + + def setup_test_environment(self, **kwargs): + super(GeoDjangoTestSuiteRunner, self).setup_test_environment(**kwargs) - # Adding the GDAL tests. - from django.contrib.gis.gdal import tests as gdal_tests - gis_tests.append(gdal_tests.suite()) - else: - print >>sys.stderr, "GDAL not available - no tests requiring GDAL will be run." - - if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): - test_suite_names.append('test_geoip') - - # Adding the rest of the suites from the modules specified - # in the `test_suite_names`. - for suite_name in test_suite_names: - tsuite = import_module('django.contrib.gis.tests.' + suite_name) - gis_tests.append(tsuite.suite()) - - return gis_tests, test_apps - -def run_gis_tests(test_labels, **kwargs): - """ - Use this routine as the TEST_RUNNER in your settings in order to run the - GeoDjango test suite. This must be done as a database superuser for - PostGIS, so read the docstring in `run_test()` below for more details. - """ - from django.conf import settings - from django.db.models import loading - from django.contrib.gis.tests.utils import mysql - - # Getting initial values. - old_installed = settings.INSTALLED_APPS - old_root_urlconf = settings.ROOT_URLCONF - - # Overridding the INSTALLED_APPS with only what we need, - # to prevent unnecessary database table creation. - new_installed = ['django.contrib.sites', - 'django.contrib.sitemaps', - 'django.contrib.gis', - ] - - # Setting the URLs. - settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls' - - # Creating the test suite, adding the test models to INSTALLED_APPS - # so they will be tested. - gis_tests, test_apps = geo_suite() - for test_model in test_apps: - module_name = 'django.contrib.gis.tests.%s' % test_model - new_installed.append(module_name) - - # Resetting the loaded flag to take into account what we appended to - # the INSTALLED_APPS (since this routine is invoked through - # django/core/management, it caches the apps; this ensures that syncdb - # will see our appended models) - settings.INSTALLED_APPS = new_installed - loading.cache.loaded = False - - kwargs['extra_tests'] = gis_tests - - # Running the tests using the GIS test runner. - result = run_tests(test_labels, **kwargs) - - # Restoring modified settings. - settings.INSTALLED_APPS = old_installed - settings.ROOT_URLCONF = old_root_urlconf - - return result + from django.db import connection + from django.contrib.gis.geos import GEOS_PREPARE + from django.contrib.gis.gdal import HAS_GDAL + + # Getting and storing the original values of INSTALLED_APPS and + # the ROOT_URLCONF. + self.old_installed = settings.INSTALLED_APPS + self.old_root_urlconf = settings.ROOT_URLCONF + + # Tests that require use of a spatial database (e.g., creation of models) + self.geo_apps = ['geoapp', 'relatedapp'] + if connection.ops.postgis and connection.ops.geography: + # Test geography support with PostGIS 1.5+. + self.geo_apps.append('geogapp') + + if HAS_GDAL: + # The following GeoDjango test apps depend on GDAL support. + if not connection.ops.mysql: + self.geo_apps.append('distapp') + + # 3D apps use LayerMapping, which uses GDAL. + if connection.ops.postgis and GEOS_PREPARE: + self.geo_apps.append('geo3d') + + self.geo_apps.append('layermap') + + # Constructing the new INSTALLED_APPS, and including applications + # within the GeoDjango test namespace (`self.geo_apps`). + new_installed = ['django.contrib.sites', + 'django.contrib.sitemaps', + 'django.contrib.gis', + ] + new_installed.extend(['django.contrib.gis.tests.%s' % app + for app in self.geo_apps]) + settings.INSTALLED_APPS = new_installed + + # Setting the URLs. + settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls' + + def teardown_test_environment(self, **kwargs): + super(GeoDjangoTestSuiteRunner, self).teardown_test_environment(**kwargs) + settings.INSTALLED_APPS = self.old_installed + settings.ROOT_URLCONF = self.old_root_urlconf + + def build_suite(self, test_labels, extra_tests=None, **kwargs): + """ + This method is overridden to construct a suite consisting only of tests + for GeoDjango. + """ + suite = unittest.TestSuite() + + # Adding the GEOS tests. + from django.contrib.gis.geos import tests as geos_tests + suite.addTest(geos_tests.suite()) + + # Adding the measurment tests. + from django.contrib.gis.tests import test_measure + suite.addTest(test_measure.suite()) + + # Adding GDAL tests, and any test suite that depends on GDAL, to the + # suite if GDAL is available. + from django.contrib.gis.gdal import HAS_GDAL + if HAS_GDAL: + from django.contrib.gis.gdal import tests as gdal_tests + suite.addTest(gdal_tests.suite()) + + from django.contrib.gis.tests import test_spatialrefsys, test_geoforms + suite.addTest(test_spatialrefsys.suite()) + suite.addTest(test_geoforms.suite()) + else: + sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n') + + # Add GeoIP tests to the suite, if the library and data is available. + from django.contrib.gis.utils import HAS_GEOIP + if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): + from django.contrib.gis.tests import test_geoip + suite.addTest(test_geoip.suite()) + + # Finally, adding the suites for each of the GeoDjango test apps. + for app_name in self.geo_apps: + suite.addTest(build_suite(get_app(app_name))) + + return suite diff --git a/django/contrib/gis/tests/geogapp/tests.py b/django/contrib/gis/tests/geogapp/tests.py index 7be5193103..3dea930afd 100644 --- a/django/contrib/gis/tests/geogapp/tests.py +++ b/django/contrib/gis/tests/geogapp/tests.py @@ -44,6 +44,10 @@ class GeographyTest(TestCase): # `@` operator not available. self.assertRaises(ValueError, City.objects.filter(point__contained=z.poly).count) + # Regression test for #14060, `~=` was never really implemented for PostGIS. + htown = City.objects.get(name='Houston') + self.assertRaises(ValueError, City.objects.get, point__exact=htown.point) + def test05_geography_layermapping(self): "Testing LayerMapping support on models with geography fields." # There is a similar test in `layermap` that uses the same data set, diff --git a/django/contrib/gis/tests/relatedapp/models.py b/django/contrib/gis/tests/relatedapp/models.py index 726f9826c0..2e9a62b61f 100644 --- a/django/contrib/gis/tests/relatedapp/models.py +++ b/django/contrib/gis/tests/relatedapp/models.py @@ -38,6 +38,11 @@ class Author(models.Model): name = models.CharField(max_length=100) objects = models.GeoManager() +class Article(models.Model): + title = models.CharField(max_length=100) + author = models.ForeignKey(Author, unique=True) + objects = models.GeoManager() + class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, related_name='books', null=True) diff --git a/django/contrib/gis/tests/relatedapp/tests.py b/django/contrib/gis/tests/relatedapp/tests.py index 184b65b9c7..5d3d00f08d 100644 --- a/django/contrib/gis/tests/relatedapp/tests.py +++ b/django/contrib/gis/tests/relatedapp/tests.py @@ -4,7 +4,7 @@ from django.contrib.gis.db.models import Collect, Count, Extent, F, Union from django.contrib.gis.geometry.backend import Geometry from django.contrib.gis.tests.utils import mysql, oracle, postgis, spatialite, no_mysql, no_oracle, no_spatialite from django.conf import settings -from models import City, Location, DirectoryEntry, Parcel, Book, Author +from models import City, Location, DirectoryEntry, Parcel, Book, Author, Article cities = (('Aurora', 'TX', -97.516111, 33.058333), ('Roswell', 'NM', -104.528056, 33.387222), @@ -291,6 +291,14 @@ class RelatedGeoModelTest(unittest.TestCase): self.assertEqual(4, len(coll)) self.assertEqual(ref_geom, coll) + def test15_invalid_select_related(self): + "Testing doing select_related on the related name manager of a unique FK. See #13934." + qs = Article.objects.select_related('author__article') + # This triggers TypeError when `get_default_columns` has no `local_only` + # keyword. The TypeError is swallowed if QuerySet is actually + # evaluated as list generation swallows TypeError in CPython. + sql = str(qs.query) + # TODO: Related tests for KML, GML, and distance lookups. def suite(): |