In Django 1.2, the addition of Spatial Backends simplified the process of testing GeoDjango applications. Specifically, testing GeoDjango applications is now the same as Testing Django applications.
Included in this documentation are some additional notes and settings for PostGIS and SpatiaLite users.
Note
Django 1.1 users are still required to use a custom TEST_RUNNER. See the Testing GeoDjango Applications in 1.1 section for more details.
Note
The settings below have sensible defaults, and shouldn’t require manual setting.
This setting may be used to customize the name of the PostGIS template database to use. In Django versions 1.2 and above, it automatically defaults to 'template_postgis' (the same name used in the installation documentation).
Note
Django 1.1 users will still have to define the POSTGIS_TEMPLATE with a value, for example:
POSTGIS_TEMPLATE='template_postgis'
When GeoDjango's spatial backend initializes on PostGIS, it has to perform a SQL query to determine the version. Setting the version manually prevents this query to the database:
POSTGIS_VERSION=('1.3.6', 1, 3, 6)
Depending on your configuration, this section describes several methods to configure a database user with sufficient privileges to run tests for GeoDjango applications on PostgreSQL. If your spatial database template was created like in the instructions, then your testing database user only needs to have the ability to create databases. In other configurations, you may be required to use a database superuser.
To make database user with the ability to create databases, use the following command:
$ createuser --createdb -R -S <user_name>
The -R -S flags indicate that we do not want the user to have the ability to create additional users (roles) or to be a superuser, respectively.
Alternatively, you may alter an existing user's role from the SQL shell (assuming this is done from an existing superuser account):
postgres# ALTER ROLE <user_name> CREATEDB NOSUPERUSER NOCREATEROLE;
This may be done at the time the user is created, for example:
$ createuser --superuser <user_name>
Or you may alter the user's role from the SQL shell (assuming this is done from an existing superuser account):
postgres# ALTER ROLE <user_name> SUPERUSER;
On Windows platforms the pgAdmin III utility may also be used as a simple way to add superuser privileges to your database user.
By default, the PostGIS installer on Windows includes a template spatial database entitled template_postgis.
You will need to download the initialization SQL script for SpatiaLite:
$ wget http://www.gaia-gis.it/spatialite/init_spatialite-2.3.zip
$ unzip init_spatialite-2.3.zip
If init_spatialite-2.3.sql is in the same path as your project's manage.py, then all you have to do is:
$ python manage.py test
By default, the GeoDjango test runner looks for the SpatiaLite SQL in the same directory where it was invoked (by default the same directory where manage.py is located). If you want to use a different location, then you may add the following to your settings:
SPATIALITE_SQL='/path/to/init_spatialite-2.3.sql'
In Django 1.1, to accommodate the extra steps required to scaffalod a spatial database automatically, a test runner customized for GeoDjango must be used. To use this runner, configure TEST_RUNNER as follows:
TEST_RUNNER='django.contrib.gis.tests.run_tests'
Note
In order to create a spatial database, the DATABASE_USER setting (or TEST_DATABASE_USER, if optionally defined on Oracle) requires elevated privileges. When using PostGIS or MySQL, the database user must have at least the ability to create databases. When testing on Oracle, the user should be a superuser.
To run GeoDjango's own internal test suite, configure the TEST_RUNNER setting as follows:
TEST_RUNNER='django.contrib.gis.tests.run_gis_tests'
Jul 05, 2010