2008-08-18 23:58:29 +08:00
|
|
|
import sys, unittest
|
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-03-31 01:15:49 +08:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL
|
|
|
|
from django.contrib.gis.utils import HAS_GEOIP
|
|
|
|
from django.contrib.gis.tests.utils import mysql
|
2008-08-07 09:14:10 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
# The test suite.
|
|
|
|
s = unittest.TestSuite()
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
if HAS_GDAL:
|
2009-03-08 07:02:48 +08:00
|
|
|
# These tests require GDAL.
|
|
|
|
test_suite_names.append('test_spatialrefsys')
|
|
|
|
test_apps.append('layermap')
|
|
|
|
|
|
|
|
# Adding the GDAL tests.
|
|
|
|
from django.contrib.gis.gdal import tests as gdal_tests
|
|
|
|
s.addTest(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)
|
2008-08-06 02:13:06 +08:00
|
|
|
s.addTest(tsuite.suite())
|
2009-03-31 01:15:49 +08:00
|
|
|
|
|
|
|
# Adding the GEOS tests _last_. Doing this because if suite starts
|
|
|
|
# immediately with this test while after running syncdb, it will cause a
|
|
|
|
# segmentation fault. My initial guess is that SpatiaLite is still in
|
|
|
|
# critical areas of non thread-safe GEOS code when the test suite is run.
|
|
|
|
# TODO: Confirm my reasoning. Are there other consequences?
|
|
|
|
from django.contrib.gis.geos import tests as geos_tests
|
|
|
|
s.addTest(geos_tests.suite())
|
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
return s, 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
|
|
|
|
|
|
|
# Creating the test suite, adding the test models to INSTALLED_APPS, and
|
2008-08-17 04:40:47 +08:00
|
|
|
# adding the model test suites to our suite package.
|
2009-03-08 07:02:48 +08:00
|
|
|
gis_suite, 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
|
|
|
if mysql:
|
2009-03-20 10:50:48 +08:00
|
|
|
test_module = 'tests_mysql'
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2009-03-20 10:50:48 +08:00
|
|
|
test_module = 'tests'
|
2008-08-06 02:13:06 +08:00
|
|
|
new_installed.append(module_name)
|
|
|
|
|
2008-08-17 04:40:47 +08:00
|
|
|
# Getting the model test suite
|
2009-03-20 10:50:48 +08:00
|
|
|
tsuite = import_module(module_name + '.' + test_module)
|
2008-08-18 23:58:29 +08:00
|
|
|
gis_suite.addTest(tsuite.suite())
|
|
|
|
|
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
|
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
# Running the tests using the GIS test runner.
|
|
|
|
result = run_tests(test_labels, suite=gis_suite, **kwargs)
|
|
|
|
|
|
|
|
# Restoring modified settings.
|
|
|
|
settings.INSTALLED_APPS = old_installed
|
|
|
|
settings.ROOT_URLCONF = old_root_urlconf
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None):
|
|
|
|
"""
|
2009-03-31 01:15:49 +08:00
|
|
|
This module allows users to run tests for GIS apps that require the creation
|
2008-08-18 23:58:29 +08:00
|
|
|
of a spatial database. Currently, this is only required for PostgreSQL as
|
|
|
|
PostGIS needs extra overhead in test database creation.
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
In order to create a PostGIS database, the DATABASE_USER (or
|
|
|
|
TEST_DATABASE_USER, if defined) will require superuser priviliges.
|
2008-08-18 23:58:29 +08:00
|
|
|
|
|
|
|
To accomplish this outside the `postgres` user, you have a few options:
|
|
|
|
(A) Make your user a super user:
|
|
|
|
This may be done at the time the user is created, for example:
|
|
|
|
$ createuser --superuser <user_name>
|
|
|
|
|
|
|
|
Or you may alter the user's role from the SQL shell (assuming this
|
|
|
|
is done from an existing superuser role):
|
|
|
|
postgres# ALTER ROLE <user_name> SUPERUSER;
|
|
|
|
|
|
|
|
(B) Create your own PostgreSQL database as a local user:
|
|
|
|
1. Initialize database: `initdb -D /path/to/user/db`
|
|
|
|
2. If there's already a Postgres instance on the machine, it will need
|
2009-03-31 01:15:49 +08:00
|
|
|
to use a different TCP port than 5432. Edit postgresql.conf (in
|
|
|
|
/path/to/user/db) to change the database port (e.g. `port = 5433`).
|
2008-08-18 23:58:29 +08:00
|
|
|
3. Start this database `pg_ctl -D /path/to/user/db start`
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
(C) On Windows platforms the pgAdmin III utility may also be used as
|
2008-08-18 23:58:29 +08:00
|
|
|
a simple way to add superuser privileges to your database user.
|
|
|
|
|
|
|
|
The TEST_RUNNER needs to be set in your settings like so:
|
|
|
|
|
|
|
|
TEST_RUNNER='django.contrib.gis.tests.run_tests'
|
|
|
|
|
|
|
|
Note: This test runner assumes that the PostGIS SQL files ('lwpostgis.sql'
|
2009-03-31 01:15:49 +08:00
|
|
|
and 'spatial_ref_sys.sql') are installed in the directory specified by
|
2008-08-18 23:58:29 +08:00
|
|
|
`pg_config --sharedir` (and defaults to /usr/local/share if that fails).
|
|
|
|
This behavior is overridden if POSTGIS_SQL_PATH is set in your settings.
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
Windows users should set POSTGIS_SQL_PATH manually because the output
|
|
|
|
of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'.
|
|
|
|
|
|
|
|
Finally, the tests may be run by invoking `./manage.py test`.
|
|
|
|
"""
|
2008-09-16 02:06:02 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection
|
|
|
|
from django.db.models import get_app, get_apps
|
|
|
|
from django.test.simple import build_suite, build_test
|
|
|
|
from django.test.utils import setup_test_environment, teardown_test_environment
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# The `create_test_spatial_db` routine abstracts away all the steps needed
|
2008-08-18 23:58:29 +08:00
|
|
|
# to properly construct a spatial database for the backend.
|
2009-03-31 01:15:49 +08:00
|
|
|
from django.contrib.gis.db.backend import create_test_spatial_db
|
2008-08-18 23:58:29 +08:00
|
|
|
|
|
|
|
# Setting up for testing.
|
|
|
|
setup_test_environment()
|
|
|
|
settings.DEBUG = False
|
|
|
|
old_name = settings.DATABASE_NAME
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Creating the test spatial database.
|
|
|
|
create_test_spatial_db(verbosity=verbosity)
|
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
# The suite may be passed in manually, e.g., when we run the GeoDjango test,
|
2009-03-31 01:15:49 +08:00
|
|
|
# we want to build it and pass it in due to some customizations. Otherwise,
|
|
|
|
# the normal test suite creation process from `django.test.simple.run_tests`
|
2008-08-18 23:58:29 +08:00
|
|
|
# is used to create the test suite.
|
|
|
|
if suite is None:
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
if test_labels:
|
|
|
|
for label in test_labels:
|
|
|
|
if '.' in label:
|
|
|
|
suite.addTest(build_test(label))
|
|
|
|
else:
|
|
|
|
app = get_app(label)
|
|
|
|
suite.addTest(build_suite(app))
|
|
|
|
else:
|
|
|
|
for app in get_apps():
|
|
|
|
suite.addTest(build_suite(app))
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2008-08-18 23:58:29 +08:00
|
|
|
for test in extra_tests:
|
|
|
|
suite.addTest(test)
|
|
|
|
|
|
|
|
# Executing the tests (including the model tests), and destorying the
|
|
|
|
# test database after the tests have completed.
|
|
|
|
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
|
2008-08-12 01:54:44 +08:00
|
|
|
connection.creation.destroy_test_db(old_name, verbosity)
|
2008-08-18 23:58:29 +08:00
|
|
|
teardown_test_environment()
|
2008-08-17 04:40:47 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Returning the total failures and errors
|
|
|
|
return len(result.failures) + len(result.errors)
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
# Class for creating a fake module with a run method. This is for the
|
|
|
|
# GEOS and GDAL tests that were moved to their respective modules.
|
|
|
|
class _DeprecatedTestModule(object):
|
2009-03-24 12:26:56 +08:00
|
|
|
def __init__(self, mod_name):
|
|
|
|
self.mod_name = mod_name
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
from warnings import warn
|
|
|
|
warn('This test module is deprecated because it has moved to ' \
|
|
|
|
'`django.contrib.gis.%s.tests` and will disappear in 1.2.' %
|
2009-03-24 12:26:56 +08:00
|
|
|
self.mod_name, DeprecationWarning)
|
|
|
|
tests = import_module('django.contrib.gis.%s.tests' % self.mod_name)
|
|
|
|
tests.run()
|
2009-03-08 07:02:48 +08:00
|
|
|
|
2009-03-24 12:26:56 +08:00
|
|
|
test_geos = _DeprecatedTestModule('geos')
|
|
|
|
test_gdal = _DeprecatedTestModule('gdal')
|