2010-03-27 04:14:53 +08:00
|
|
|
======================
|
2012-02-18 04:04:11 +08:00
|
|
|
Testing GeoDjango apps
|
2010-03-27 04:14:53 +08:00
|
|
|
======================
|
|
|
|
|
|
|
|
Included in this documentation are some additional notes and settings
|
|
|
|
for :ref:`testing-postgis` and :ref:`testing-spatialite` users.
|
|
|
|
|
|
|
|
.. _testing-postgis:
|
|
|
|
|
|
|
|
PostGIS
|
|
|
|
=======
|
|
|
|
|
|
|
|
Settings
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The settings below have sensible defaults, and shouldn't require manual setting.
|
|
|
|
|
|
|
|
.. setting:: POSTGIS_TEMPLATE
|
|
|
|
|
|
|
|
``POSTGIS_TEMPLATE``
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
This setting may be used to customize the name of the PostGIS template
|
2012-06-07 21:02:35 +08:00
|
|
|
database to use. It automatically defaults to ``'template_postgis'``
|
|
|
|
(the same name used in the
|
2010-03-27 04:14:53 +08:00
|
|
|
:ref:`installation documentation <spatialdb_template>`).
|
|
|
|
|
|
|
|
.. setting:: POSTGIS_VERSION
|
|
|
|
|
|
|
|
``POSTGIS_VERSION``
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2010-12-22 08:21:35 +08:00
|
|
|
|
2010-03-27 04:14:53 +08:00
|
|
|
When GeoDjango's spatial backend initializes on PostGIS, it has to perform
|
2013-09-06 06:23:48 +08:00
|
|
|
an SQL query to determine the version in order to figure out what
|
2012-02-18 04:04:11 +08:00
|
|
|
features are available. Advanced users wishing to prevent this additional
|
2010-12-22 08:21:35 +08:00
|
|
|
query may set the version manually using a 3-tuple of integers specifying
|
2012-02-18 04:04:11 +08:00
|
|
|
the major, minor, and subminor version numbers for PostGIS. For example,
|
2010-12-22 08:21:35 +08:00
|
|
|
to configure for PostGIS 1.5.2 you would use::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
POSTGIS_VERSION = (1, 5, 2)
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
Obtaining sufficient privileges
|
2010-03-27 04:14:53 +08:00
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
Depending on your configuration, this section describes several methods to
|
|
|
|
configure a database user with sufficient privileges to run tests for
|
2012-02-18 04:04:11 +08:00
|
|
|
GeoDjango applications on PostgreSQL. If your
|
2010-03-27 04:14:53 +08:00
|
|
|
:ref:`spatial database template <spatialdb_template>`
|
|
|
|
was created like in the instructions, then your testing database user
|
2012-02-18 04:04:11 +08:00
|
|
|
only needs to have the ability to create databases. In other configurations,
|
2010-03-27 04:14:53 +08:00
|
|
|
you may be required to use a database superuser.
|
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
Create database user
|
2010-03-27 04:14:53 +08:00
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
2010-12-22 08:21:35 +08:00
|
|
|
|
2012-02-18 04:09:40 +08:00
|
|
|
To make a database user with the ability to create databases, use the
|
2010-03-27 04:14:53 +08:00
|
|
|
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;
|
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
Create database superuser
|
2010-03-27 04:14:53 +08:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
2010-12-22 08:21:35 +08:00
|
|
|
|
2010-03-27 04:14:53 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
Create local PostgreSQL database
|
2010-03-27 04:14:53 +08:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
1. Initialize database: ``initdb -D /path/to/user/db``
|
|
|
|
|
|
|
|
2. If there's already a Postgres instance on the machine, it will need
|
|
|
|
to use a different TCP port than 5432. Edit ``postgresql.conf`` (in
|
|
|
|
``/path/to/user/db``) to change the database port (e.g. ``port = 5433``).
|
|
|
|
|
|
|
|
3. Start this database ``pg_ctl -D /path/to/user/db start``
|
|
|
|
|
2014-02-16 18:51:58 +08:00
|
|
|
Create a database using PostGIS version 2
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
When testing projects using :ref:`PostGIS 2 <spatialdb_template91>`,
|
|
|
|
the test database is created using the ``CREATE EXTENSION postgis``
|
|
|
|
instruction, provided that no template ``template_postgis`` (or named
|
|
|
|
accordingly to :setting:`POSTGIS_TEMPLATE`) exists in the current
|
|
|
|
database.
|
|
|
|
|
2010-03-27 04:14:53 +08:00
|
|
|
Windows
|
|
|
|
-------
|
2010-12-22 08:21:35 +08:00
|
|
|
|
2010-03-27 04:14:53 +08:00
|
|
|
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``.
|
|
|
|
|
|
|
|
.. _testing-spatialite:
|
|
|
|
|
|
|
|
SpatiaLite
|
|
|
|
==========
|
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
Make sure the necessary spatial tables are created in your test spatial
|
|
|
|
database, as described in :ref:`create_spatialite_db`. Then just do this::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-08-23 16:07:35 +08:00
|
|
|
$ python manage.py test
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
.. _geodjango-tests:
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2012-02-18 04:04:11 +08:00
|
|
|
GeoDjango tests
|
2010-12-22 08:21:35 +08:00
|
|
|
===============
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
To have the GeoDjango tests executed when :ref:`running the Django test suite
|
|
|
|
<running-unit-tests>` with ``runtests.py`` all of the databases in the settings
|
|
|
|
file must be using one of the :ref:`spatial database backends
|
|
|
|
<spatial-backends>`.
|
2010-12-22 08:21:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
Example
|
2013-05-11 11:08:45 +08:00
|
|
|
-------
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
The following is an example bare-bones settings file with spatial backends
|
|
|
|
that can be used to run the entire Django test suite, including those
|
|
|
|
in :mod:`django.contrib.gis`::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.contrib.gis.db.backends.postgis',
|
|
|
|
'NAME': 'geodjango',
|
|
|
|
'USER': 'geodjango',
|
|
|
|
},
|
|
|
|
'other': {
|
|
|
|
'ENGINE': 'django.contrib.gis.db.backends.postgis',
|
|
|
|
'NAME': 'other',
|
|
|
|
'USER': 'geodjango',
|
|
|
|
}
|
|
|
|
}
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2014-04-04 12:05:30 +08:00
|
|
|
SECRET_KEY = 'django_tests_secret_key'
|
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
Assuming the settings above were in a ``postgis.py`` file in the same
|
|
|
|
directory as ``runtests.py``, then all Django and GeoDjango tests would
|
|
|
|
be performed when executing the command::
|
2010-03-27 04:14:53 +08:00
|
|
|
|
2010-12-22 08:21:35 +08:00
|
|
|
$ ./runtests.py --settings=postgis
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
To run only the GeoDjango test suite, specify ``django.contrib.gis``::
|
|
|
|
|
|
|
|
$ ./runtests.py --settings=postgis django.contrib.gis
|