testing.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ======================
  2. Testing GeoDjango Apps
  3. ======================
  4. .. versionchanged:: 1.2
  5. In Django 1.2, the addition of :ref:`spatial-backends` simplified the
  6. process of testing GeoDjango applications -- the process is now the
  7. same as :doc:`/topics/testing`.
  8. Included in this documentation are some additional notes and settings
  9. for :ref:`testing-postgis` and :ref:`testing-spatialite` users.
  10. .. _testing-postgis:
  11. PostGIS
  12. =======
  13. Settings
  14. --------
  15. .. note::
  16. The settings below have sensible defaults, and shouldn't require manual setting.
  17. .. setting:: POSTGIS_TEMPLATE
  18. ``POSTGIS_TEMPLATE``
  19. ^^^^^^^^^^^^^^^^^^^^
  20. .. versionchanged:: 1.2
  21. This setting may be used to customize the name of the PostGIS template
  22. database to use. In Django versions 1.2 and above, it automatically
  23. defaults to ``'template_postgis'`` (the same name used in the
  24. :ref:`installation documentation <spatialdb_template>`).
  25. .. setting:: POSTGIS_VERSION
  26. ``POSTGIS_VERSION``
  27. ^^^^^^^^^^^^^^^^^^^
  28. When GeoDjango's spatial backend initializes on PostGIS, it has to perform
  29. a SQL query to determine the version in order to figure out what
  30. features are available. Advanced users wishing to prevent this additional
  31. query may set the version manually using a 3-tuple of integers specifying
  32. the major, minor, and subminor version numbers for PostGIS. For example,
  33. to configure for PostGIS 1.5.2 you would use::
  34. POSTGIS_VERSION = (1, 5, 2)
  35. Obtaining Sufficient Privileges
  36. -------------------------------
  37. Depending on your configuration, this section describes several methods to
  38. configure a database user with sufficient privileges to run tests for
  39. GeoDjango applications on PostgreSQL. If your
  40. :ref:`spatial database template <spatialdb_template>`
  41. was created like in the instructions, then your testing database user
  42. only needs to have the ability to create databases. In other configurations,
  43. you may be required to use a database superuser.
  44. Create Database User
  45. ^^^^^^^^^^^^^^^^^^^^
  46. To make database user with the ability to create databases, use the
  47. following command::
  48. $ createuser --createdb -R -S <user_name>
  49. The ``-R -S`` flags indicate that we do not want the user to have the ability
  50. to create additional users (roles) or to be a superuser, respectively.
  51. Alternatively, you may alter an existing user's role from the SQL shell
  52. (assuming this is done from an existing superuser account)::
  53. postgres# ALTER ROLE <user_name> CREATEDB NOSUPERUSER NOCREATEROLE;
  54. Create Database Superuser
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^
  56. This may be done at the time the user is created, for example::
  57. $ createuser --superuser <user_name>
  58. Or you may alter the user's role from the SQL shell (assuming this
  59. is done from an existing superuser account)::
  60. postgres# ALTER ROLE <user_name> SUPERUSER;
  61. Create Local PostgreSQL Database
  62. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  63. 1. Initialize database: ``initdb -D /path/to/user/db``
  64. 2. If there's already a Postgres instance on the machine, it will need
  65. to use a different TCP port than 5432. Edit ``postgresql.conf`` (in
  66. ``/path/to/user/db``) to change the database port (e.g. ``port = 5433``).
  67. 3. Start this database ``pg_ctl -D /path/to/user/db start``
  68. Windows
  69. -------
  70. On Windows platforms the pgAdmin III utility may also be used as
  71. a simple way to add superuser privileges to your database user.
  72. By default, the PostGIS installer on Windows includes a template
  73. spatial database entitled ``template_postgis``.
  74. .. _testing-spatialite:
  75. SpatiaLite
  76. ==========
  77. You need to make sure needed spatial tables are created in your test spatial
  78. database as described in :ref:`create_spatialite_db`. Then all you have to do is::
  79. $ python manage.py test
  80. Settings
  81. --------
  82. .. setting:: SPATIALITE_SQL
  83. ``SPATIALITE_SQL``
  84. ^^^^^^^^^^^^^^^^^^
  85. (only relevant when using a SpatiaLite version older than 3.0).
  86. By default, the GeoDjango test runner looks for the SpatiaLite SQL in the
  87. same directory where it was invoked (by default the same directory where
  88. ``manage.py`` is located). If you want to use a different location, then
  89. you may add the following to your settings::
  90. SPATIALITE_SQL='/path/to/init_spatialite-2.3.sql'
  91. .. _geodjango-tests:
  92. GeoDjango Tests
  93. ===============
  94. .. versionchanged:: 1.3
  95. GeoDjango's test suite may be run in one of two ways, either by itself or
  96. with the rest of :ref:`Django's unit tests <running-unit-tests>`.
  97. Run only GeoDjango tests
  98. ------------------------
  99. To run *only* the tests for GeoDjango, the :setting:`TEST_RUNNER`
  100. setting must be changed to use the
  101. :class:`~django.contrib.gis.tests.GeoDjangoTestSuiteRunner`::
  102. TEST_RUNNER = 'django.contrib.gis.tests.GeoDjangoTestSuiteRunner'
  103. Example
  104. ^^^^^^^
  105. First, you'll need a bare-bones settings file, like below, that is
  106. customized with your spatial database name and user::
  107. TEST_RUNNER = 'django.contrib.gis.tests.GeoDjangoTestSuiteRunner'
  108. DATABASES = {
  109. 'default': {
  110. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  111. 'NAME': 'a_spatial_database',
  112. 'USER': 'db_user'
  113. }
  114. }
  115. Assuming the above is in a file called ``postgis.py`` that is in the
  116. the same directory as ``manage.py`` of your Django project, then
  117. you may run the tests with the following command::
  118. $ python manage.py test --settings=postgis
  119. Run with ``runtests.py``
  120. ------------------------
  121. To have the GeoDjango tests executed when
  122. :ref:`running the Django test suite <running-unit-tests>` with ``runtests.py``
  123. all of the databases in the settings file must be using one of the
  124. :ref:`spatial database backends <spatial-backends>`.
  125. .. warning::
  126. Do not change the :setting:`TEST_RUNNER` setting
  127. when running the GeoDjango tests with ``runtests.py``.
  128. Example
  129. ^^^^^^^
  130. The following is an example bare-bones settings file with spatial backends
  131. that can be used to run the entire Django test suite, including those
  132. in :mod:`django.contrib.gis`::
  133. DATABASES = {
  134. 'default': {
  135. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  136. 'NAME': 'geodjango',
  137. 'USER': 'geodjango',
  138. },
  139. 'other': {
  140. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  141. 'NAME': 'other',
  142. 'USER': 'geodjango',
  143. }
  144. }
  145. Assuming the settings above were in a ``postgis.py`` file in the same
  146. directory as ``runtests.py``, then all Django and GeoDjango tests would
  147. be performed when executing the command::
  148. $ ./runtests.py --settings=postgis