1 import sys, unittest |
1 import sys |
2 from django.utils.importlib import import_module |
2 |
|
3 def run_tests(*args, **kwargs): |
|
4 from django.test.simple import run_tests as base_run_tests |
|
5 return base_run_tests(*args, **kwargs) |
3 |
6 |
4 def geo_suite(): |
7 def geo_suite(): |
5 """ |
8 """ |
6 Builds a test suite for the GIS package. This is not named |
9 Builds a test suite for the GIS package. This is not named |
7 `suite` so it will not interfere with the Django test suite (since |
10 `suite` so it will not interfere with the Django test suite (since |
8 spatial database tables are required to execute these tests on |
11 spatial database tables are required to execute these tests on |
9 some backends). |
12 some backends). |
10 """ |
13 """ |
11 from django.conf import settings |
14 from django.conf import settings |
|
15 from django.contrib.gis.geos import GEOS_PREPARE |
12 from django.contrib.gis.gdal import HAS_GDAL |
16 from django.contrib.gis.gdal import HAS_GDAL |
13 from django.contrib.gis.utils import HAS_GEOIP |
17 from django.contrib.gis.utils import HAS_GEOIP |
14 from django.contrib.gis.tests.utils import mysql |
18 from django.contrib.gis.tests.utils import postgis, mysql |
|
19 from django.db import connection |
|
20 from django.utils.importlib import import_module |
15 |
21 |
16 # The test suite. |
22 gis_tests = [] |
17 s = unittest.TestSuite() |
|
18 |
23 |
19 # Adding the GEOS tests. |
24 # Adding the GEOS tests. |
20 from django.contrib.gis.geos import tests as geos_tests |
25 from django.contrib.gis.geos import tests as geos_tests |
21 s.addTest(geos_tests.suite()) |
26 gis_tests.append(geos_tests.suite()) |
22 |
27 |
23 # Tests that require use of a spatial database (e.g., creation of models) |
28 # Tests that require use of a spatial database (e.g., creation of models) |
24 test_apps = ['geoapp', 'relatedapp'] |
29 test_apps = ['geoapp', 'relatedapp'] |
|
30 if postgis and connection.ops.geography: |
|
31 # Test geography support with PostGIS 1.5+. |
|
32 test_apps.append('geogapp') |
25 |
33 |
26 # Tests that do not require setting up and tearing down a spatial database. |
34 # Tests that do not require setting up and tearing down a spatial database. |
27 test_suite_names = [ |
35 test_suite_names = [ |
28 'test_measure', |
36 'test_measure', |
29 ] |
37 ] |
30 |
38 |
31 # Tests applications that require a test spatial db. |
|
32 if not mysql: |
|
33 test_apps.append('distapp') |
|
34 |
|
35 if HAS_GDAL: |
39 if HAS_GDAL: |
36 # These tests require GDAL. |
40 # These tests require GDAL. |
|
41 if not mysql: |
|
42 test_apps.append('distapp') |
|
43 |
|
44 # Only PostGIS using GEOS 3.1+ can support 3D so far. |
|
45 if postgis and GEOS_PREPARE: |
|
46 test_apps.append('geo3d') |
|
47 |
37 test_suite_names.extend(['test_spatialrefsys', 'test_geoforms']) |
48 test_suite_names.extend(['test_spatialrefsys', 'test_geoforms']) |
38 test_apps.append('layermap') |
49 test_apps.append('layermap') |
39 |
50 |
40 # Adding the GDAL tests. |
51 # Adding the GDAL tests. |
41 from django.contrib.gis.gdal import tests as gdal_tests |
52 from django.contrib.gis.gdal import tests as gdal_tests |
42 s.addTest(gdal_tests.suite()) |
53 gis_tests.append(gdal_tests.suite()) |
43 else: |
54 else: |
44 print >>sys.stderr, "GDAL not available - no tests requiring GDAL will be run." |
55 print >>sys.stderr, "GDAL not available - no tests requiring GDAL will be run." |
45 |
56 |
46 if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): |
57 if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): |
47 test_suite_names.append('test_geoip') |
58 test_suite_names.append('test_geoip') |
48 |
59 |
49 # Adding the rest of the suites from the modules specified |
60 # Adding the rest of the suites from the modules specified |
50 # in the `test_suite_names`. |
61 # in the `test_suite_names`. |
51 for suite_name in test_suite_names: |
62 for suite_name in test_suite_names: |
52 tsuite = import_module('django.contrib.gis.tests.' + suite_name) |
63 tsuite = import_module('django.contrib.gis.tests.' + suite_name) |
53 s.addTest(tsuite.suite()) |
64 gis_tests.append(tsuite.suite()) |
54 |
65 |
55 return s, test_apps |
66 return gis_tests, test_apps |
56 |
67 |
57 def run_gis_tests(test_labels, **kwargs): |
68 def run_gis_tests(test_labels, **kwargs): |
58 """ |
69 """ |
59 Use this routine as the TEST_RUNNER in your settings in order to run the |
70 Use this routine as the TEST_RUNNER in your settings in order to run the |
60 GeoDjango test suite. This must be done as a database superuser for |
71 GeoDjango test suite. This must be done as a database superuser for |
76 ] |
87 ] |
77 |
88 |
78 # Setting the URLs. |
89 # Setting the URLs. |
79 settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls' |
90 settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls' |
80 |
91 |
81 # Creating the test suite, adding the test models to INSTALLED_APPS, and |
92 # Creating the test suite, adding the test models to INSTALLED_APPS |
82 # adding the model test suites to our suite package. |
93 # so they will be tested. |
83 gis_suite, test_apps = geo_suite() |
94 gis_tests, test_apps = geo_suite() |
84 for test_model in test_apps: |
95 for test_model in test_apps: |
85 module_name = 'django.contrib.gis.tests.%s' % test_model |
96 module_name = 'django.contrib.gis.tests.%s' % test_model |
86 if mysql: |
|
87 test_module = 'tests_mysql' |
|
88 else: |
|
89 test_module = 'tests' |
|
90 new_installed.append(module_name) |
97 new_installed.append(module_name) |
91 |
|
92 # Getting the model test suite |
|
93 tsuite = import_module(module_name + '.' + test_module) |
|
94 gis_suite.addTest(tsuite.suite()) |
|
95 |
98 |
96 # Resetting the loaded flag to take into account what we appended to |
99 # Resetting the loaded flag to take into account what we appended to |
97 # the INSTALLED_APPS (since this routine is invoked through |
100 # the INSTALLED_APPS (since this routine is invoked through |
98 # django/core/management, it caches the apps; this ensures that syncdb |
101 # django/core/management, it caches the apps; this ensures that syncdb |
99 # will see our appended models) |
102 # will see our appended models) |
100 settings.INSTALLED_APPS = new_installed |
103 settings.INSTALLED_APPS = new_installed |
101 loading.cache.loaded = False |
104 loading.cache.loaded = False |
102 |
105 |
|
106 kwargs['extra_tests'] = gis_tests |
|
107 |
103 # Running the tests using the GIS test runner. |
108 # Running the tests using the GIS test runner. |
104 result = run_tests(test_labels, suite=gis_suite, **kwargs) |
109 result = run_tests(test_labels, **kwargs) |
105 |
110 |
106 # Restoring modified settings. |
111 # Restoring modified settings. |
107 settings.INSTALLED_APPS = old_installed |
112 settings.INSTALLED_APPS = old_installed |
108 settings.ROOT_URLCONF = old_root_urlconf |
113 settings.ROOT_URLCONF = old_root_urlconf |
109 |
114 |
110 return result |
115 return result |
111 |
|
112 def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): |
|
113 """ |
|
114 Set `TEST_RUNNER` in your settings with this routine in order to |
|
115 scaffold test spatial databases correctly for your GeoDjango models. |
|
116 For more documentation, please consult the following URL: |
|
117 http://geodjango.org/docs/testing.html. |
|
118 """ |
|
119 from django.conf import settings |
|
120 from django.db import connection |
|
121 from django.db.models import get_app, get_apps |
|
122 from django.test.simple import build_suite, build_test, reorder_suite, TestCase |
|
123 from django.test.utils import setup_test_environment, teardown_test_environment |
|
124 |
|
125 # The `create_test_spatial_db` routine abstracts away all the steps needed |
|
126 # to properly construct a spatial database for the backend. |
|
127 from django.contrib.gis.db.backend import create_test_spatial_db |
|
128 |
|
129 # Setting up for testing. |
|
130 setup_test_environment() |
|
131 settings.DEBUG = False |
|
132 old_name = settings.DATABASE_NAME |
|
133 |
|
134 # Creating the test spatial database. |
|
135 create_test_spatial_db(verbosity=verbosity, autoclobber=not interactive) |
|
136 |
|
137 # The suite may be passed in manually, e.g., when we run the GeoDjango test, |
|
138 # we want to build it and pass it in due to some customizations. Otherwise, |
|
139 # the normal test suite creation process from `django.test.simple.run_tests` |
|
140 # is used to create the test suite. |
|
141 if suite is None: |
|
142 suite = unittest.TestSuite() |
|
143 if test_labels: |
|
144 for label in test_labels: |
|
145 if '.' in label: |
|
146 suite.addTest(build_test(label)) |
|
147 else: |
|
148 app = get_app(label) |
|
149 suite.addTest(build_suite(app)) |
|
150 else: |
|
151 for app in get_apps(): |
|
152 suite.addTest(build_suite(app)) |
|
153 |
|
154 for test in extra_tests: |
|
155 suite.addTest(test) |
|
156 |
|
157 suite = reorder_suite(suite, (TestCase,)) |
|
158 |
|
159 # Executing the tests (including the model tests), and destorying the |
|
160 # test database after the tests have completed. |
|
161 result = unittest.TextTestRunner(verbosity=verbosity).run(suite) |
|
162 connection.creation.destroy_test_db(old_name, verbosity) |
|
163 teardown_test_environment() |
|
164 |
|
165 # Returning the total failures and errors |
|
166 return len(result.failures) + len(result.errors) |
|
167 |
|
168 # Class for creating a fake module with a run method. This is for the |
|
169 # GEOS and GDAL tests that were moved to their respective modules. |
|
170 class _DeprecatedTestModule(object): |
|
171 def __init__(self, mod_name): |
|
172 self.mod_name = mod_name |
|
173 |
|
174 def run(self): |
|
175 from warnings import warn |
|
176 warn('This test module is deprecated because it has moved to ' \ |
|
177 '`django.contrib.gis.%s.tests` and will disappear in 1.2.' % |
|
178 self.mod_name, DeprecationWarning) |
|
179 tests = import_module('django.contrib.gis.%s.tests' % self.mod_name) |
|
180 tests.run() |
|
181 |
|
182 test_geos = _DeprecatedTestModule('geos') |
|
183 test_gdal = _DeprecatedTestModule('gdal') |
|