| 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 |
|---|
| | 59 | def 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 | """ |
|---|
| | 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 | |
|---|
| | 120 | def 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 | |
|---|