| | 30 | |
|---|
| | 31 | def 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) |
|---|