web/lib/django/contrib/gis/tests/__init__.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 import sys
       
     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)
       
     6 
       
     7 def geo_suite():
       
     8     """
       
     9     Builds a test suite for the GIS package.  This is not named
       
    10     `suite` so it will not interfere with the Django test suite (since
       
    11     spatial database tables are required to execute these tests on
       
    12     some backends).
       
    13     """
       
    14     from django.conf import settings
       
    15     from django.contrib.gis.geos import GEOS_PREPARE
       
    16     from django.contrib.gis.gdal import HAS_GDAL
       
    17     from django.contrib.gis.utils import HAS_GEOIP
       
    18     from django.contrib.gis.tests.utils import postgis, mysql
       
    19     from django.db import connection
       
    20     from django.utils.importlib import import_module
       
    21 
       
    22     gis_tests = []
       
    23 
       
    24     # Adding the GEOS tests.
       
    25     from django.contrib.gis.geos import tests as geos_tests
       
    26     gis_tests.append(geos_tests.suite())
       
    27 
       
    28     # Tests that require use of a spatial database (e.g., creation of models)
       
    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')
       
    33 
       
    34     # Tests that do not require setting up and tearing down a spatial database.
       
    35     test_suite_names = [
       
    36         'test_measure',
       
    37         ]
       
    38 
       
    39     if HAS_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 
       
    48         test_suite_names.extend(['test_spatialrefsys', 'test_geoforms'])
       
    49         test_apps.append('layermap')
       
    50         
       
    51         # Adding the GDAL tests.
       
    52         from django.contrib.gis.gdal import tests as gdal_tests
       
    53         gis_tests.append(gdal_tests.suite())
       
    54     else:
       
    55         print >>sys.stderr, "GDAL not available - no tests requiring GDAL will be run."
       
    56 
       
    57     if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'):
       
    58         test_suite_names.append('test_geoip')
       
    59 
       
    60     # Adding the rest of the suites from the modules specified
       
    61     # in the `test_suite_names`.
       
    62     for suite_name in test_suite_names:
       
    63         tsuite = import_module('django.contrib.gis.tests.' + suite_name)
       
    64         gis_tests.append(tsuite.suite())
       
    65 
       
    66     return gis_tests, test_apps
       
    67 
       
    68 def run_gis_tests(test_labels, **kwargs):
       
    69     """
       
    70     Use this routine as the TEST_RUNNER in your settings in order to run the
       
    71     GeoDjango test suite.  This must be done as a database superuser for
       
    72     PostGIS, so read the docstring in `run_test()` below for more details.
       
    73     """
       
    74     from django.conf import settings
       
    75     from django.db.models import loading
       
    76     from django.contrib.gis.tests.utils import mysql
       
    77 
       
    78     # Getting initial values.
       
    79     old_installed = settings.INSTALLED_APPS
       
    80     old_root_urlconf = settings.ROOT_URLCONF
       
    81 
       
    82     # Overridding the INSTALLED_APPS with only what we need,
       
    83     # to prevent unnecessary database table creation.
       
    84     new_installed =  ['django.contrib.sites',
       
    85                       'django.contrib.sitemaps',
       
    86                       'django.contrib.gis',
       
    87                       ]
       
    88 
       
    89     # Setting the URLs.
       
    90     settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls'
       
    91 
       
    92     # Creating the test suite, adding the test models to INSTALLED_APPS
       
    93     # so they will be tested.
       
    94     gis_tests, test_apps = geo_suite()
       
    95     for test_model in test_apps:
       
    96         module_name = 'django.contrib.gis.tests.%s' % test_model
       
    97         new_installed.append(module_name)
       
    98 
       
    99     # Resetting the loaded flag to take into account what we appended to
       
   100     # the INSTALLED_APPS (since this routine is invoked through
       
   101     # django/core/management, it caches the apps; this ensures that syncdb
       
   102     # will see our appended models)
       
   103     settings.INSTALLED_APPS = new_installed
       
   104     loading.cache.loaded = False
       
   105 
       
   106     kwargs['extra_tests'] = gis_tests
       
   107 
       
   108     # Running the tests using the GIS test runner.
       
   109     result = run_tests(test_labels, **kwargs)
       
   110 
       
   111     # Restoring modified settings.
       
   112     settings.INSTALLED_APPS = old_installed
       
   113     settings.ROOT_URLCONF = old_root_urlconf
       
   114 
       
   115     return result