testing.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 micro version numbers for PostGIS. For example,
  28. to configure for PostGIS X.Y.Z you would use::
  29. POSTGIS_VERSION = (X, Y, Z)
  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 a database using PostGIS version 2
  57. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  58. When testing projects using :ref:`PostGIS 2 <spatialdb_template91>`,
  59. the test database is created using the ``CREATE EXTENSION postgis``
  60. instruction, provided that no template ``template_postgis`` (or named
  61. accordingly to :setting:`POSTGIS_TEMPLATE`) exists in the current
  62. database.
  63. Windows
  64. -------
  65. On Windows platforms the pgAdmin III utility may also be used as
  66. a simple way to add superuser privileges to your database user.
  67. By default, the PostGIS installer on Windows includes a template
  68. spatial database entitled ``template_postgis``.
  69. .. _testing-spatialite:
  70. SpatiaLite
  71. ==========
  72. Make sure the necessary spatial tables are created in your test spatial
  73. database, as described in :ref:`create_spatialite_db`. Then just do this::
  74. $ python manage.py test
  75. .. _geodjango-tests:
  76. GeoDjango tests
  77. ===============
  78. To have the GeoDjango tests executed when :ref:`running the Django test suite
  79. <running-unit-tests>` with ``runtests.py`` all of the databases in the settings
  80. file must be using one of the :ref:`spatial database backends
  81. <spatial-backends>`.
  82. Example
  83. -------
  84. The following is an example bare-bones settings file with spatial backends
  85. that can be used to run the entire Django test suite, including those
  86. in :mod:`django.contrib.gis`::
  87. DATABASES = {
  88. 'default': {
  89. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  90. 'NAME': 'geodjango',
  91. 'USER': 'geodjango',
  92. },
  93. 'other': {
  94. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  95. 'NAME': 'other',
  96. 'USER': 'geodjango',
  97. }
  98. }
  99. SECRET_KEY = 'django_tests_secret_key'
  100. Assuming the settings above were in a ``postgis.py`` file in the same
  101. directory as ``runtests.py``, then all Django and GeoDjango tests would
  102. be performed when executing the command::
  103. $ ./runtests.py --settings=postgis
  104. To run only the GeoDjango test suite, specify ``gis_tests``::
  105. $ ./runtests.py --settings=postgis gis_tests