1. ======================
    
  2. Testing GeoDjango apps
    
  3. ======================
    
  4. 
    
  5. Included in this documentation are some additional notes and settings
    
  6. for :ref:`testing-postgis` users.
    
  7. 
    
  8. .. _testing-postgis:
    
  9. 
    
  10. PostGIS
    
  11. =======
    
  12. 
    
  13. Settings
    
  14. --------
    
  15. 
    
  16. .. note::
    
  17. 
    
  18.     The settings below have sensible defaults, and shouldn't require manual setting.
    
  19. 
    
  20. .. setting:: POSTGIS_VERSION
    
  21. 
    
  22. ``POSTGIS_VERSION``
    
  23. ~~~~~~~~~~~~~~~~~~~
    
  24. 
    
  25. When GeoDjango's spatial backend initializes on PostGIS, it has to perform
    
  26. an SQL query to determine the version in order to figure out what
    
  27. features are available. Advanced users wishing to prevent this additional
    
  28. query may set the version manually using a 3-tuple of integers specifying
    
  29. the major, minor, and micro version numbers for PostGIS. For example,
    
  30. to configure for PostGIS X.Y.Z you would use::
    
  31. 
    
  32.     POSTGIS_VERSION = (X, Y, Z)
    
  33. 
    
  34. Obtaining sufficient privileges
    
  35. -------------------------------
    
  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. 
    
  45. Create database user
    
  46. ~~~~~~~~~~~~~~~~~~~~
    
  47. 
    
  48. To make a database user with the ability to create databases, use the
    
  49. following command::
    
  50. 
    
  51.     $ createuser --createdb -R -S <user_name>
    
  52. 
    
  53. The ``-R -S`` flags indicate that we do not want the user to have the ability
    
  54. to create additional users (roles) or to be a superuser, respectively.
    
  55. 
    
  56. Alternatively, you may alter an existing user's role from the SQL shell
    
  57. (assuming this is done from an existing superuser account)::
    
  58. 
    
  59.     postgres# ALTER ROLE <user_name> CREATEDB NOSUPERUSER NOCREATEROLE;
    
  60. 
    
  61. Create database superuser
    
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~
    
  63. 
    
  64. This may be done at the time the user is created, for example::
    
  65. 
    
  66.     $ createuser --superuser <user_name>
    
  67. 
    
  68. Or you may alter the user's role from the SQL shell (assuming this
    
  69. is done from an existing superuser account)::
    
  70. 
    
  71.     postgres# ALTER ROLE <user_name> SUPERUSER;
    
  72. 
    
  73. Windows
    
  74. -------
    
  75. 
    
  76. On Windows platforms you can use the pgAdmin III utility to add superuser
    
  77. privileges to your database user.
    
  78. 
    
  79. By default, the PostGIS installer on Windows includes a template
    
  80. spatial database entitled ``template_postgis``.
    
  81. 
    
  82. .. _geodjango-tests:
    
  83. 
    
  84. GeoDjango tests
    
  85. ===============
    
  86. 
    
  87. To have the GeoDjango tests executed when :ref:`running the Django test suite
    
  88. <running-unit-tests>` with ``runtests.py`` all of the databases in the settings
    
  89. file must be using one of the :ref:`spatial database backends
    
  90. <spatial-backends>`.
    
  91. 
    
  92. 
    
  93. Example
    
  94. -------
    
  95. 
    
  96. The following is an example bare-bones settings file with spatial backends
    
  97. that can be used to run the entire Django test suite, including those
    
  98. in :mod:`django.contrib.gis`::
    
  99. 
    
  100.     DATABASES = {
    
  101.         'default': {
    
  102.             'ENGINE': 'django.contrib.gis.db.backends.postgis',
    
  103.             'NAME': 'geodjango',
    
  104.             'USER': 'geodjango',
    
  105.         },
    
  106.         'other': {
    
  107.             'ENGINE': 'django.contrib.gis.db.backends.postgis',
    
  108.             'NAME': 'other',
    
  109.             'USER': 'geodjango',
    
  110.         },
    
  111.     }
    
  112. 
    
  113.     SECRET_KEY = 'django_tests_secret_key'
    
  114. 
    
  115. Assuming the settings above were in a ``postgis.py`` file in the same
    
  116. directory as ``runtests.py``, then all Django and GeoDjango tests would
    
  117. be performed when executing the command::
    
  118. 
    
  119.     $ ./runtests.py --settings=postgis
    
  120. 
    
  121. To run only the GeoDjango test suite, specify ``gis_tests``::
    
  122. 
    
  123.     $ ./runtests.py --settings=postgis gis_tests