Django

Code

Changeset 8434

Show
Ignore:
Timestamp:
08/18/08 10:58:29 (11 months ago)
Author:
jbronn
Message:

GeoDjango users may now use django.contrib.gis.tests.run_tests for their TEST_RUNNER so that a testing spatial database is properly created; the GeoDjango test suite runner is renamed to run_gis_tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/gis/db/backend/postgis/creation.py

    r8308 r8434  
     1import os, re, sys 
     2 
    13from django.conf import settings 
    24from django.core.management import call_command 
    35from django.db import connection 
    46from django.db.backends.creation import TEST_DATABASE_PREFIX 
    5 import os, re, sys 
    67 
    78def getstatusoutput(cmd): 
     
    5859        else: 
    5960            raise Exception('Spatial Database Creation canceled.') 
    60 foo = _create_with_cursor 
    6161 
    6262created_regex = re.compile(r'^createdb: database creation failed: ERROR:  database ".+" already exists') 
  • django/trunk/django/contrib/gis/tests/__init__.py

    r8414 r8434  
    1 import sys 
    2 from unittest import TestSuite, TextTestRunner 
     1import sys, unittest 
    32 
    43from django.conf import settings 
    5 if not settings._target: settings.configure() 
     4from django.db import connection 
     5from django.db.models import get_app, get_apps, loading 
     6from django.test.simple import build_suite, build_test 
     7from django.test.utils import setup_test_environment, teardown_test_environment 
    68 
    79def geo_suite(): 
     
    4951        test_suite_names.append('test_geoip') 
    5052 
    51     s = TestSuite() 
     53    s = unittest.TestSuite() 
    5254    for test_suite in test_suite_names: 
    5355        tsuite = getattr(__import__('django.contrib.gis.tests', globals(), locals(), [test_suite]),test_suite) 
     
    5557    return s, test_models 
    5658 
    57 def run(verbosity=1): 
    58     "Runs the tests that do not require geographic (GEOS, GDAL, etc.) models." 
    59     TextTestRunner(verbosity=verbosity).run(geo_suite()) 
    60  
    61 def run_tests(module_list, verbosity=1, interactive=True): 
    62     """ 
    63     Run the tests that require creation of a spatial database. 
    64      
    65     In order to run geographic model tests the DATABASE_USER will require 
    66     superuser priviliges.  To accomplish this outside the `postgres` user, 
    67     create your own PostgreSQL database as a user: 
    68      (1) Initialize database: `initdb -D /path/to/user/db` 
    69      (2) If there's already a Postgres instance on the machine, it will need 
    70          to use a different TCP port than 5432. Edit postgresql.conf (in  
    71          /path/to/user/db) to change the database port (e.g. `port = 5433`).   
    72      (3) Start this database `pg_ctl -D /path/to/user/db start` 
    73  
    74     On Windows platforms simply use the pgAdmin III utility to add superuser  
    75     privileges to your database user. 
    76  
    77     Make sure your settings.py matches the settings of the user database.  
    78     For example, set the same port number (`DATABASE_PORT=5433`).   
    79     DATABASE_NAME or TEST_DATABSE_NAME must be set, along with DATABASE_USER. 
    80        
    81     In settings.py set TEST_RUNNER='django.contrib.gis.tests.run_tests'. 
    82  
    83     Finally, this assumes that the PostGIS SQL files (lwpostgis.sql and  
    84     spatial_ref_sys.sql) are installed in the directory specified by  
    85     `pg_config --sharedir` (and defaults to /usr/local/share if that fails). 
    86     This behavior is overridden if `POSTGIS_SQL_PATH` is in your settings. 
    87      
    88     Windows users should set POSTGIS_SQL_PATH manually because the output 
    89     of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'. 
    90  
    91     Finally, the tests may be run by invoking `./manage.py test`. 
    92     """ 
    93     from django.contrib.gis.db.backend import create_spatial_db 
     59def run_gis_tests(test_labels, **kwargs): 
     60    """ 
     61    Use this routine as the TEST_RUNNER in your settings in order to run the 
     62    GeoDjango test suite.  This must be done as a database superuser for 
     63    PostGIS, so read the docstring in `run_test()` below for more details. 
     64    """ 
    9465    from django.contrib.gis.tests.utils import mysql 
    95     from django.db import connection 
    96     from django.db.models import loading 
    9766 
    9867    # Getting initial values. 
    99     old_debug = settings.DEBUG 
    100     old_name = settings.DATABASE_NAME 
    10168    old_installed = settings.INSTALLED_APPS 
    10269    old_root_urlconf = settings.ROOT_URLCONF 
     
    11986    settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls' 
    12087 
    121     # Want DEBUG to be set to False. 
    122     settings.DEBUG = False 
    123  
    12488    # Creating the test suite, adding the test models to INSTALLED_APPS, and 
    12589    # adding the model test suites to our suite package. 
    126     test_suite, test_models = geo_suite() 
     90    gis_suite, test_models = geo_suite() 
    12791    for test_model in test_models: 
    12892        module_name = 'django.contrib.gis.tests.%s' % test_model 
     
    136100        tsuite = getattr(__import__('django.contrib.gis.tests.%s' % test_model, globals(), locals(), [test_module_name]),  
    137101                         test_module_name) 
    138         test_suite.addTest(tsuite.suite()) 
    139      
     102        gis_suite.addTest(tsuite.suite()) 
     103 
    140104    # Resetting the loaded flag to take into account what we appended to  
    141105    # the INSTALLED_APPS (since this routine is invoked through  
     
    145109    loading.cache.loaded = False 
    146110 
     111    # Running the tests using the GIS test runner. 
     112    result = run_tests(test_labels, suite=gis_suite, **kwargs) 
     113 
     114    # Restoring modified settings. 
     115    settings.INSTALLED_APPS = old_installed 
     116    settings.ROOT_URLCONF = old_root_urlconf 
     117 
     118    return result 
     119 
     120def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): 
     121    """ 
     122    This module allows users to run tests for GIS apps that require the creation  
     123    of a spatial database.  Currently, this is only required for PostgreSQL as 
     124    PostGIS needs extra overhead in test database creation. 
     125 
     126    In order to create a PostGIS database, the DATABASE_USER (or  
     127    TEST_DATABASE_USER, if defined) will require superuser priviliges.   
     128 
     129    To accomplish this outside the `postgres` user, you have a few options: 
     130      (A) Make your user a super user: 
     131        This may be done at the time the user is created, for example: 
     132        $ createuser --superuser <user_name> 
     133 
     134        Or you may alter the user's role from the SQL shell (assuming this 
     135        is done from an existing superuser role): 
     136        postgres# ALTER ROLE <user_name> SUPERUSER; 
     137 
     138      (B) Create your own PostgreSQL database as a local user: 
     139        1. Initialize database: `initdb -D /path/to/user/db` 
     140        2. If there's already a Postgres instance on the machine, it will need 
     141           to use a different TCP port than 5432. Edit postgresql.conf (in  
     142           /path/to/user/db) to change the database port (e.g. `port = 5433`).   
     143        3. Start this database `pg_ctl -D /path/to/user/db start` 
     144 
     145      (C) On Windows platforms the pgAdmin III utility may also be used as  
     146        a simple way to add superuser privileges to your database user. 
     147 
     148    The TEST_RUNNER needs to be set in your settings like so: 
     149 
     150      TEST_RUNNER='django.contrib.gis.tests.run_tests' 
     151 
     152    Note: This test runner assumes that the PostGIS SQL files ('lwpostgis.sql' 
     153    and 'spatial_ref_sys.sql') are installed in the directory specified by  
     154    `pg_config --sharedir` (and defaults to /usr/local/share if that fails). 
     155    This behavior is overridden if POSTGIS_SQL_PATH is set in your settings. 
     156     
     157    Windows users should set POSTGIS_SQL_PATH manually because the output 
     158    of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'. 
     159 
     160    Finally, the tests may be run by invoking `./manage.py test`. 
     161    """ 
     162    # The `create_spatial_db` routine abstracts away all the steps needed 
     163    # to properly construct a spatial database for the backend. 
     164    from django.contrib.gis.db.backend import create_spatial_db 
     165 
     166    # Setting up for testing. 
     167    setup_test_environment() 
     168    settings.DEBUG = False 
     169    old_name = settings.DATABASE_NAME 
     170 
     171    # The suite may be passed in manually, e.g., when we run the GeoDjango test, 
     172    # we want to build it and pass it in due to some customizations.  Otherwise,  
     173    # the normal test suite creation process from `django.test.simple.run_tests`  
     174    # is used to create the test suite. 
     175    if suite is None: 
     176        suite = unittest.TestSuite() 
     177        if test_labels: 
     178            for label in test_labels: 
     179                if '.' in label: 
     180                    suite.addTest(build_test(label)) 
     181                else: 
     182                    app = get_app(label) 
     183                    suite.addTest(build_suite(app)) 
     184        else: 
     185            for app in get_apps(): 
     186                suite.addTest(build_suite(app)) 
     187     
     188        for test in extra_tests: 
     189            suite.addTest(test) 
     190 
    147191    # Creating the test spatial database. 
    148192    create_spatial_db(test=True, verbosity=verbosity) 
    149193 
    150     # Executing the tests (including the model tests) 
    151     result = TextTestRunner(verbosity=verbosity).run(test_suite) 
    152  
    153     # Cleaning up, destroying the test spatial database and resetting the INSTALLED_APPS. 
     194    # Executing the tests (including the model tests), and destorying the 
     195    # test database after the tests have completed. 
     196    result = unittest.TextTestRunner(verbosity=verbosity).run(suite) 
    154197    connection.creation.destroy_test_db(old_name, verbosity) 
    155     settings.DEBUG = old_debug 
    156     settings.INSTALLED_APPS = old_installed 
    157     settings.ROOT_URLCONF = old_root_urlconf 
     198    teardown_test_environment() 
    158199 
    159200    # Returning the total failures and errors 
    160201    return len(result.failures) + len(result.errors) 
    161  
    162 if __name__ == '__main__': 
    163     run()