/django/contrib/gis/tests/__init__.py
Python | 139 lines | 95 code | 25 blank | 19 comment | 18 complexity | 0cd8752208cf1de0c6c622db46c2be31 MD5 | raw file
Possible License(s): BSD-3-Clause
1from django.conf import settings 2from django.test.simple import build_suite, DjangoTestSuiteRunner 3from django.utils import unittest 4 5def run_tests(*args, **kwargs): 6 from django.test.simple import run_tests as base_run_tests 7 return base_run_tests(*args, **kwargs) 8 9 10def run_gis_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=None): 11 import warnings 12 warnings.warn( 13 'The run_gis_tests() test runner has been deprecated in favor of GeoDjangoTestSuiteRunner.', 14 DeprecationWarning 15 ) 16 test_runner = GeoDjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) 17 return test_runner.run_tests(test_labels, extra_tests=extra_tests) 18 19 20def geo_apps(namespace=True, runtests=False): 21 """ 22 Returns a list of GeoDjango test applications that reside in 23 `django.contrib.gis.tests` that can be used with the current 24 database and the spatial libraries that are installed. 25 """ 26 from django.db import connection 27 from django.contrib.gis.geos import GEOS_PREPARE 28 from django.contrib.gis.gdal import HAS_GDAL 29 30 apps = ['geoapp', 'relatedapp'] 31 32 # No distance queries on MySQL. 33 if not connection.ops.mysql: 34 apps.append('distapp') 35 36 # Test geography support with PostGIS 1.5+. 37 if connection.ops.postgis and connection.ops.geography: 38 apps.append('geogapp') 39 40 # The following GeoDjango test apps depend on GDAL support. 41 if HAS_GDAL: 42 # 3D apps use LayerMapping, which uses GDAL. 43 if connection.ops.postgis and GEOS_PREPARE: 44 apps.append('geo3d') 45 46 apps.append('layermap') 47 48 if runtests: 49 return [('django.contrib.gis.tests', app) for app in apps] 50 elif namespace: 51 return ['django.contrib.gis.tests.%s' % app 52 for app in apps] 53 else: 54 return apps 55 56 57def geodjango_suite(apps=True): 58 """ 59 Returns a TestSuite consisting only of GeoDjango tests that can be run. 60 """ 61 import sys 62 from django.db.models import get_app 63 64 suite = unittest.TestSuite() 65 66 # Adding the GEOS tests. 67 from django.contrib.gis.geos import tests as geos_tests 68 suite.addTest(geos_tests.suite()) 69 70 # Adding the measurment tests. 71 from django.contrib.gis.tests import test_measure 72 suite.addTest(test_measure.suite()) 73 74 # Adding GDAL tests, and any test suite that depends on GDAL, to the 75 # suite if GDAL is available. 76 from django.contrib.gis.gdal import HAS_GDAL 77 if HAS_GDAL: 78 from django.contrib.gis.gdal import tests as gdal_tests 79 suite.addTest(gdal_tests.suite()) 80 81 from django.contrib.gis.tests import test_spatialrefsys, test_geoforms 82 suite.addTest(test_spatialrefsys.suite()) 83 suite.addTest(test_geoforms.suite()) 84 else: 85 sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n') 86 87 # Add GeoIP tests to the suite, if the library and data is available. 88 from django.contrib.gis.utils import HAS_GEOIP 89 if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): 90 from django.contrib.gis.tests import test_geoip 91 suite.addTest(test_geoip.suite()) 92 93 # Finally, adding the suites for each of the GeoDjango test apps. 94 if apps: 95 for app_name in geo_apps(namespace=False): 96 suite.addTest(build_suite(get_app(app_name))) 97 98 return suite 99 100 101class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner): 102 103 def setup_test_environment(self, **kwargs): 104 super(GeoDjangoTestSuiteRunner, self).setup_test_environment(**kwargs) 105 106 # Saving original values of INSTALLED_APPS, ROOT_URLCONF, and SITE_ID. 107 self.old_installed = getattr(settings, 'INSTALLED_APPS', None) 108 self.old_root_urlconf = getattr(settings, 'ROOT_URLCONF', '') 109 self.old_site_id = getattr(settings, 'SITE_ID', None) 110 111 # Constructing the new INSTALLED_APPS, and including applications 112 # within the GeoDjango test namespace. 113 new_installed = ['django.contrib.sites', 114 'django.contrib.sitemaps', 115 'django.contrib.gis', 116 ] 117 118 # Calling out to `geo_apps` to get GeoDjango applications supported 119 # for testing. 120 new_installed.extend(geo_apps()) 121 settings.INSTALLED_APPS = new_installed 122 123 # SITE_ID needs to be set 124 settings.SITE_ID = 1 125 126 # ROOT_URLCONF needs to be set, else `AttributeErrors` are raised 127 # when TestCases are torn down that have `urls` defined. 128 settings.ROOT_URLCONF = '' 129 130 131 def teardown_test_environment(self, **kwargs): 132 super(GeoDjangoTestSuiteRunner, self).teardown_test_environment(**kwargs) 133 settings.INSTALLED_APPS = self.old_installed 134 settings.ROOT_URLCONF = self.old_root_urlconf 135 settings.SITE_ID = self.old_site_id 136 137 138 def build_suite(self, test_labels, extra_tests=None, **kwargs): 139 return geodjango_suite()