2008-08-18 23:58:29 +08:00
|
|
|
import sys, unittest
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.test.simple import run_tests
|
2009-03-20 10:50:48 +08:00
|
|
|
from django.utils.importlib import import_module
|
2008-08-07 09:14:10 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
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).
|
|
|
|
"""
|
2008-09-16 02:06:02 +08:00
|
|
|
from django.conf import settings
|
2009-11-17 02:49:00 +08:00
|
|
|
from django.contrib.gis.geos import GEOS_PREPARE
|
2009-03-31 01:15:49 +08:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL
|
|
|
|
from django.contrib.gis.utils import HAS_GEOIP
|
2009-11-17 02:49:00 +08:00
|
|
|
from django.contrib.gis.tests.utils import postgis, mysql
|
2008-08-07 09:14:10 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
gis_tests = []
|
2009-03-08 07:02:48 +08:00
|
|
|
|
2009-04-20 07:07:24 +08:00
|
|
|
# Adding the GEOS tests.
|
|
|
|
from django.contrib.gis.geos import tests as geos_tests
|
2009-12-22 23:18:51 +08:00
|
|
|
gis_tests.append(geos_tests.suite())
|
2009-04-20 07:07:24 +08:00
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Tests that require use of a spatial database (e.g., creation of models)
|
2009-03-08 07:02:48 +08:00
|
|
|
test_apps = ['geoapp', 'relatedapp']
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Tests that do not require setting up and tearing down a spatial database.
|
2008-08-07 09:14:10 +08:00
|
|
|
test_suite_names = [
|
|
|
|
'test_measure',
|
|
|
|
]
|
2009-03-08 07:02:48 +08:00
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Tests applications that require a test spatial db.
|
|
|
|
if not mysql:
|
|
|
|
test_apps.append('distapp')
|
|
|
|
|
2009-11-17 02:49:00 +08:00
|
|
|
# Only PostGIS using GEOS 3.1+ can support 3D so far.
|
|
|
|
if postgis and GEOS_PREPARE:
|
|
|
|
test_apps.append('geo3d')
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
if HAS_GDAL:
|
2009-03-08 07:02:48 +08:00
|
|
|
# These tests require GDAL.
|
2009-04-26 02:24:32 +08:00
|
|
|
test_suite_names.extend(['test_spatialrefsys', 'test_geoforms'])
|
2009-03-08 07:02:48 +08:00
|
|
|
test_apps.append('layermap')
|
|
|
|
|
|
|
|
# Adding the GDAL tests.
|
|
|
|
from django.contrib.gis.gdal import tests as gdal_tests
|
2009-12-22 23:18:51 +08:00
|
|
|
gis_tests.append(gdal_tests.suite())
|
2008-08-07 09:14:10 +08:00
|
|
|
else:
|
2009-03-31 01:15:49 +08:00
|
|
|
print >>sys.stderr, "GDAL not available - no tests requiring GDAL will be run."
|
2008-08-07 09:14:10 +08:00
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'):
|
2008-08-07 09:14:10 +08:00
|
|
|
test_suite_names.append('test_geoip')
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Adding the rest of the suites from the modules specified
|
|
|
|
# in the `test_suite_names`.
|
2009-03-08 07:02:48 +08:00
|
|
|
for suite_name in test_suite_names:
|
2009-03-20 10:50:48 +08:00
|
|
|
tsuite = import_module('django.contrib.gis.tests.' + suite_name)
|
2009-12-22 23:18:51 +08:00
|
|
|
gis_tests.append(tsuite.suite())
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
return gis_tests, test_apps
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
def run_gis_tests(test_labels, **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2008-08-18 23:58:29 +08:00
|
|
|
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.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2008-09-16 02:06:02 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models import loading
|
2008-08-07 09:14:10 +08:00
|
|
|
from django.contrib.gis.tests.utils import mysql
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Getting initial values.
|
2008-08-17 04:40:47 +08:00
|
|
|
old_installed = settings.INSTALLED_APPS
|
|
|
|
old_root_urlconf = settings.ROOT_URLCONF
|
|
|
|
|
2009-04-11 03:19:19 +08:00
|
|
|
# Overridding the INSTALLED_APPS with only what we need,
|
|
|
|
# to prevent unnecessary database table creation.
|
|
|
|
new_installed = ['django.contrib.sites',
|
2008-08-24 03:22:23 +08:00
|
|
|
'django.contrib.sitemaps',
|
2008-08-17 04:40:47 +08:00
|
|
|
'django.contrib.gis',
|
|
|
|
]
|
|
|
|
|
|
|
|
# Setting the URLs.
|
|
|
|
settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Creating the test suite, adding the test models to INSTALLED_APPS
|
|
|
|
# so they will be tested.
|
|
|
|
gis_tests, test_apps = geo_suite()
|
2009-03-31 01:15:49 +08:00
|
|
|
for test_model in test_apps:
|
|
|
|
module_name = 'django.contrib.gis.tests.%s' % test_model
|
2008-08-06 02:13:06 +08:00
|
|
|
new_installed.append(module_name)
|
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
# 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
|
2008-08-06 02:13:06 +08:00
|
|
|
# will see our appended models)
|
|
|
|
settings.INSTALLED_APPS = new_installed
|
|
|
|
loading.cache.loaded = False
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
kwargs['extra_tests'] = gis_tests
|
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
# Running the tests using the GIS test runner.
|
2009-12-22 23:18:51 +08:00
|
|
|
result = run_tests(test_labels, **kwargs)
|
2008-08-18 23:58:29 +08:00
|
|
|
|
|
|
|
# Restoring modified settings.
|
|
|
|
settings.INSTALLED_APPS = old_installed
|
|
|
|
settings.ROOT_URLCONF = old_root_urlconf
|
|
|
|
|
|
|
|
return result
|