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