testing.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ======================
  2. Testing GeoDjango apps
  3. ======================
  4. Included in this documentation are some additional notes and settings
  5. for :ref:`testing-postgis` 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_VERSION
  14. ``POSTGIS_VERSION``
  15. ~~~~~~~~~~~~~~~~~~~
  16. When GeoDjango's spatial backend initializes on PostGIS, it has to perform
  17. an SQL query to determine the version in order to figure out what
  18. features are available. Advanced users wishing to prevent this additional
  19. query may set the version manually using a 3-tuple of integers specifying
  20. the major, minor, and micro version numbers for PostGIS. For example,
  21. to configure for PostGIS X.Y.Z you would use::
  22. POSTGIS_VERSION = (X, Y, Z)
  23. Obtaining sufficient privileges
  24. -------------------------------
  25. Depending on your configuration, this section describes several methods to
  26. configure a database user with sufficient privileges to run tests for
  27. GeoDjango applications on PostgreSQL. If your
  28. :ref:`spatial database template <spatialdb_template>`
  29. was created like in the instructions, then your testing database user
  30. only needs to have the ability to create databases. In other configurations,
  31. you may be required to use a database superuser.
  32. Create database user
  33. ~~~~~~~~~~~~~~~~~~~~
  34. To make a database user with the ability to create databases, use the
  35. following command:
  36. .. code-block:: shell
  37. $ createuser --createdb -R -S <user_name>
  38. The ``-R -S`` flags indicate that we do not want the user to have the ability
  39. to create additional users (roles) or to be a superuser, respectively.
  40. Alternatively, you may alter an existing user's role from the SQL shell
  41. (assuming this is done from an existing superuser account):
  42. .. code-block:: psql
  43. postgres# ALTER ROLE <user_name> CREATEDB NOSUPERUSER NOCREATEROLE;
  44. Create database superuser
  45. ~~~~~~~~~~~~~~~~~~~~~~~~~
  46. This may be done at the time the user is created, for example:
  47. .. code-block:: shell
  48. $ createuser --superuser <user_name>
  49. Or you may alter the user's role from the SQL shell (assuming this
  50. is done from an existing superuser account):
  51. .. code-block:: psql
  52. postgres# ALTER ROLE <user_name> SUPERUSER;
  53. Windows
  54. -------
  55. On Windows platforms you can use the pgAdmin III utility to add superuser
  56. privileges to your database user.
  57. By default, the PostGIS installer on Windows includes a template
  58. spatial database entitled ``template_postgis``.
  59. .. _geodjango-tests:
  60. GeoDjango tests
  61. ===============
  62. To have the GeoDjango tests executed when :ref:`running the Django test suite
  63. <running-unit-tests>` with ``runtests.py`` all of the databases in the settings
  64. file must be using one of the :ref:`spatial database backends
  65. <spatial-backends>`.
  66. Example
  67. -------
  68. The following is an example bare-bones settings file with spatial backends
  69. that can be used to run the entire Django test suite, including those
  70. in :mod:`django.contrib.gis`::
  71. DATABASES = {
  72. "default": {
  73. "ENGINE": "django.contrib.gis.db.backends.postgis",
  74. "NAME": "geodjango",
  75. "USER": "geodjango",
  76. },
  77. "other": {
  78. "ENGINE": "django.contrib.gis.db.backends.postgis",
  79. "NAME": "other",
  80. "USER": "geodjango",
  81. },
  82. }
  83. SECRET_KEY = "django_tests_secret_key"
  84. Assuming the settings above were in a ``postgis.py`` file in the same
  85. directory as ``runtests.py``, then all Django and GeoDjango tests would
  86. be performed when executing the command:
  87. .. code-block:: shell
  88. $ ./runtests.py --settings=postgis
  89. To run only the GeoDjango test suite, specify ``gis_tests``:
  90. .. code-block:: shell
  91. $ ./runtests.py --settings=postgis gis_tests