testing.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ======================
  2. Testing GeoDjango apps
  3. ======================
  4. Included in this documentation are some additional notes and settings
  5. for :ref:`testing-postgis` and :ref:`testing-spatialite` users.
  6. .. _testing-postgis:
  7. PostGIS
  8. =======
  9. Settings
  10. --------
  11. .. note::
  12. The settings below have sensible defaults, and shouldn't require manual setting.
  13. .. setting:: POSTGIS_TEMPLATE
  14. ``POSTGIS_TEMPLATE``
  15. ^^^^^^^^^^^^^^^^^^^^
  16. This setting may be used to customize the name of the PostGIS template
  17. database to use. It automatically defaults to ``'template_postgis'``
  18. (the same name used in the
  19. :ref:`installation documentation <spatialdb_template>`).
  20. .. setting:: POSTGIS_VERSION
  21. ``POSTGIS_VERSION``
  22. ^^^^^^^^^^^^^^^^^^^
  23. When GeoDjango's spatial backend initializes on PostGIS, it has to perform
  24. an SQL query to determine the version in order to figure out what
  25. features are available. Advanced users wishing to prevent this additional
  26. query may set the version manually using a 3-tuple of integers specifying
  27. the major, minor, and subminor version numbers for PostGIS. For example,
  28. to configure for PostGIS 1.5.2 you would use::
  29. POSTGIS_VERSION = (1, 5, 2)
  30. Obtaining sufficient privileges
  31. -------------------------------
  32. Depending on your configuration, this section describes several methods to
  33. configure a database user with sufficient privileges to run tests for
  34. GeoDjango applications on PostgreSQL. If your
  35. :ref:`spatial database template <spatialdb_template>`
  36. was created like in the instructions, then your testing database user
  37. only needs to have the ability to create databases. In other configurations,
  38. you may be required to use a database superuser.
  39. Create database user
  40. ^^^^^^^^^^^^^^^^^^^^
  41. To make a database user with the ability to create databases, use the
  42. following command::
  43. $ createuser --createdb -R -S <user_name>
  44. The ``-R -S`` flags indicate that we do not want the user to have the ability
  45. to create additional users (roles) or to be a superuser, respectively.
  46. Alternatively, you may alter an existing user's role from the SQL shell
  47. (assuming this is done from an existing superuser account)::
  48. postgres# ALTER ROLE <user_name> CREATEDB NOSUPERUSER NOCREATEROLE;
  49. Create database superuser
  50. ^^^^^^^^^^^^^^^^^^^^^^^^^
  51. This may be done at the time the user is created, for example::
  52. $ createuser --superuser <user_name>
  53. Or you may alter the user's role from the SQL shell (assuming this
  54. is done from an existing superuser account)::
  55. postgres# ALTER ROLE <user_name> SUPERUSER;
  56. Create local PostgreSQL database
  57. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  58. 1. Initialize database: ``initdb -D /path/to/user/db``
  59. 2. If there's already a Postgres instance on the machine, it will need
  60. to use a different TCP port than 5432. Edit ``postgresql.conf`` (in
  61. ``/path/to/user/db``) to change the database port (e.g. ``port = 5433``).
  62. 3. Start this database ``pg_ctl -D /path/to/user/db start``
  63. Create a database using PostGIS version 2
  64. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  65. When testing projects using :ref:`PostGIS 2 <spatialdb_template91>`,
  66. the test database is created using the ``CREATE EXTENSION postgis``
  67. instruction, provided that no template ``template_postgis`` (or named
  68. accordingly to :setting:`POSTGIS_TEMPLATE`) exists in the current
  69. database.
  70. Windows
  71. -------
  72. On Windows platforms the pgAdmin III utility may also be used as
  73. a simple way to add superuser privileges to your database user.
  74. By default, the PostGIS installer on Windows includes a template
  75. spatial database entitled ``template_postgis``.
  76. .. _testing-spatialite:
  77. SpatiaLite
  78. ==========
  79. Make sure the necessary spatial tables are created in your test spatial
  80. database, as described in :ref:`create_spatialite_db`. Then just do this::
  81. $ python manage.py test
  82. .. _geodjango-tests:
  83. GeoDjango tests
  84. ===============
  85. To have the GeoDjango tests executed when :ref:`running the Django test suite
  86. <running-unit-tests>` with ``runtests.py`` all of the databases in the settings
  87. file must be using one of the :ref:`spatial database backends
  88. <spatial-backends>`.
  89. Example
  90. -------
  91. The following is an example bare-bones settings file with spatial backends
  92. that can be used to run the entire Django test suite, including those
  93. in :mod:`django.contrib.gis`::
  94. DATABASES = {
  95. 'default': {
  96. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  97. 'NAME': 'geodjango',
  98. 'USER': 'geodjango',
  99. },
  100. 'other': {
  101. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  102. 'NAME': 'other',
  103. 'USER': 'geodjango',
  104. }
  105. }
  106. SECRET_KEY = 'django_tests_secret_key'
  107. Assuming the settings above were in a ``postgis.py`` file in the same
  108. directory as ``runtests.py``, then all Django and GeoDjango tests would
  109. be performed when executing the command::
  110. $ ./runtests.py --settings=postgis
  111. To run only the GeoDjango test suite, specify ``django.contrib.gis``::
  112. $ ./runtests.py --settings=postgis django.contrib.gis