Django

Code

Changeset 5763

Show
Ignore:
Timestamp:
07/25/07 21:27:17 (1 year ago)
Author:
jbronn
Message:

gis: geographic models may now be tested; started test geographic application to test geographic queries and lookup types.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/tests/__init__.py

    r5635 r5763  
    1 from unittest import TestSuite, makeSuite, TextTestRunner 
     1from copy import copy 
     2from unittest import TestSuite, TextTestRunner 
     3from django.contrib.gis.utils import create_spatial_db 
     4from django.db import connection 
     5from django.test.utils import destroy_test_db 
    26 
     7# Tests that do not require setting up and tearing down a spatial database. 
    38test_suite_names = ['test_gdal_driver', 
    4               'test_gdal_ds', 
    5               'test_gdal_geom', 
    6               'test_gdal_srs', 
    7               'test_geos', 
    8               'test_measure', 
    9               'test_spatialrefsys', 
    10                
     9                    'test_gdal_ds', 
     10                    'test_gdal_geom', 
     11                    'test_gdal_srs', 
     12                    'test_geos', 
     13                    'test_measure', 
     14                    'test_spatialrefsys', 
     15                    
    1116 
     17test_models = ['geoapp'] 
    1218 
    1319def suite(): 
     20    "Builds a test suite for the GIS package." 
    1421    s = TestSuite() 
    1522    for test_suite in test_suite_names: 
    16         suite = getattr(__import__('django.contrib.gis.tests', fromlist=[test_suite]),test_suite) 
    17         s.addTest(suite.suite()) 
     23        tsuite = getattr(__import__('django.contrib.gis.tests', fromlist=[test_suite]),test_suite) 
     24        s.addTest(tsuite.suite()) 
    1825    return s 
    1926 
    2027def run(verbosity=1): 
     28    "Runs the tests that do not require geographic (GEOS, GDAL, etc.) models." 
    2129    TextTestRunner(verbosity=verbosity).run(suite()) 
     30 
     31def run_tests(module_list, verbosity=1): 
     32    """Run the tests that require creation of a spatial database.  Does not 
     33      yet work on Windows platforms. 
     34     
     35    In order to run geographic model tests the DATABASE_USER will require 
     36      superuser priviliges.  To accomplish this outside the `postgres` user, 
     37      create your own PostgreSQL database as a user: 
     38        (1) Initialize database: `initdb -D /path/to/user/db` 
     39        (2) If there's already a Postgres instance on the machine, it will need 
     40            to use a different TCP port than 5432. Edit postgresql.conf (in  
     41            /path/to/user/db) to change the database port (e.g. `port = 5433`).   
     42        (3) Start this database `pg_ctl -D /path/to/user/db start` 
     43 
     44    Make sure your settings.py matches the settings of the user database. For example, set the  
     45      same port number (`DATABASE_PORT=5433`).  DATABASE_NAME or TEST_DATABSE_NAME must be set, 
     46      along with DATABASE_USER. 
     47       
     48    In settings.py set TEST_RUNNER='django.contrib.gis.tests.run_tests'. 
     49 
     50    Finally, this assumes that the PostGIS SQL files (lwpostgis.sql and spatial_ref_sys.sql) 
     51      are installed in /usr/local/share.  If they are not, add `POSTGIS_SQL_PATH=/path/to/sql` 
     52      in your settings.py. 
     53 
     54    The tests may be run by invoking `./manage.py test`. 
     55    """ 
     56    from django.conf import settings 
     57 
     58    # Getting initial values. 
     59    old_name = copy(settings.DATABASE_NAME) 
     60    old_installed = copy(settings.INSTALLED_APPS) 
     61 
     62    # Creating the test suite, adding the test models to INSTALLED_APPS, and 
     63    #  adding the model test suites to our suite package. 
     64    test_suite = suite() 
     65    for test_model in test_models: 
     66        module_name = 'django.contrib.gis.tests.%s' % test_model 
     67        settings.INSTALLED_APPS.append(module_name) 
     68        tsuite = getattr(__import__('django.contrib.gis.tests.%s' % test_model, fromlist=['tests']), 'tests') 
     69        test_suite.addTest(tsuite.suite()) 
     70 
     71    # Resetting the loaded flag to take into account what we appended to the INSTALLED_APPS 
     72    #  (since this routine is invoked through django/core/management, it caches the apps, 
     73    #   this ensures that syncdb will see our appended models) 
     74    from django.db.models import loading 
     75    loading._loaded = False 
     76 
     77    # Creating the test spatial database. 
     78    create_spatial_db(test=True, verbosity=verbosity) 
     79 
     80    # Executing the tests (including the model tests) 
     81    result = TextTestRunner(verbosity=verbosity).run(test_suite) 
     82 
     83    # Cleaning up, destroying the test spatial database and resetting the INSTALLED_APPS. 
     84    destroy_test_db(old_name, verbosity) 
     85    settings.INSTALLED_APPS = old_installed 
     86     
     87    # Returning the total failures and errors 
     88    return len(result.failures) + len(result.errors) 
    2289 
    2390if __name__ == '__main__':