/django/contrib/gis/tests/__init__.py

https://code.google.com/p/mango-py/ · Python · 139 lines · 80 code · 33 blank · 26 comment · 17 complexity · 0cd8752208cf1de0c6c622db46c2be31 MD5 · raw file

  1. from django.conf import settings
  2. from django.test.simple import build_suite, DjangoTestSuiteRunner
  3. from django.utils import unittest
  4. def run_tests(*args, **kwargs):
  5. from django.test.simple import run_tests as base_run_tests
  6. return base_run_tests(*args, **kwargs)
  7. def run_gis_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=None):
  8. import warnings
  9. warnings.warn(
  10. 'The run_gis_tests() test runner has been deprecated in favor of GeoDjangoTestSuiteRunner.',
  11. DeprecationWarning
  12. )
  13. test_runner = GeoDjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
  14. return test_runner.run_tests(test_labels, extra_tests=extra_tests)
  15. def geo_apps(namespace=True, runtests=False):
  16. """
  17. Returns a list of GeoDjango test applications that reside in
  18. `django.contrib.gis.tests` that can be used with the current
  19. database and the spatial libraries that are installed.
  20. """
  21. from django.db import connection
  22. from django.contrib.gis.geos import GEOS_PREPARE
  23. from django.contrib.gis.gdal import HAS_GDAL
  24. apps = ['geoapp', 'relatedapp']
  25. # No distance queries on MySQL.
  26. if not connection.ops.mysql:
  27. apps.append('distapp')
  28. # Test geography support with PostGIS 1.5+.
  29. if connection.ops.postgis and connection.ops.geography:
  30. apps.append('geogapp')
  31. # The following GeoDjango test apps depend on GDAL support.
  32. if HAS_GDAL:
  33. # 3D apps use LayerMapping, which uses GDAL.
  34. if connection.ops.postgis and GEOS_PREPARE:
  35. apps.append('geo3d')
  36. apps.append('layermap')
  37. if runtests:
  38. return [('django.contrib.gis.tests', app) for app in apps]
  39. elif namespace:
  40. return ['django.contrib.gis.tests.%s' % app
  41. for app in apps]
  42. else:
  43. return apps
  44. def geodjango_suite(apps=True):
  45. """
  46. Returns a TestSuite consisting only of GeoDjango tests that can be run.
  47. """
  48. import sys
  49. from django.db.models import get_app
  50. suite = unittest.TestSuite()
  51. # Adding the GEOS tests.
  52. from django.contrib.gis.geos import tests as geos_tests
  53. suite.addTest(geos_tests.suite())
  54. # Adding the measurment tests.
  55. from django.contrib.gis.tests import test_measure
  56. suite.addTest(test_measure.suite())
  57. # Adding GDAL tests, and any test suite that depends on GDAL, to the
  58. # suite if GDAL is available.
  59. from django.contrib.gis.gdal import HAS_GDAL
  60. if HAS_GDAL:
  61. from django.contrib.gis.gdal import tests as gdal_tests
  62. suite.addTest(gdal_tests.suite())
  63. from django.contrib.gis.tests import test_spatialrefsys, test_geoforms
  64. suite.addTest(test_spatialrefsys.suite())
  65. suite.addTest(test_geoforms.suite())
  66. else:
  67. sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n')
  68. # Add GeoIP tests to the suite, if the library and data is available.
  69. from django.contrib.gis.utils import HAS_GEOIP
  70. if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'):
  71. from django.contrib.gis.tests import test_geoip
  72. suite.addTest(test_geoip.suite())
  73. # Finally, adding the suites for each of the GeoDjango test apps.
  74. if apps:
  75. for app_name in geo_apps(namespace=False):
  76. suite.addTest(build_suite(get_app(app_name)))
  77. return suite
  78. class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner):
  79. def setup_test_environment(self, **kwargs):
  80. super(GeoDjangoTestSuiteRunner, self).setup_test_environment(**kwargs)
  81. # Saving original values of INSTALLED_APPS, ROOT_URLCONF, and SITE_ID.
  82. self.old_installed = getattr(settings, 'INSTALLED_APPS', None)
  83. self.old_root_urlconf = getattr(settings, 'ROOT_URLCONF', '')
  84. self.old_site_id = getattr(settings, 'SITE_ID', None)
  85. # Constructing the new INSTALLED_APPS, and including applications
  86. # within the GeoDjango test namespace.
  87. new_installed = ['django.contrib.sites',
  88. 'django.contrib.sitemaps',
  89. 'django.contrib.gis',
  90. ]
  91. # Calling out to `geo_apps` to get GeoDjango applications supported
  92. # for testing.
  93. new_installed.extend(geo_apps())
  94. settings.INSTALLED_APPS = new_installed
  95. # SITE_ID needs to be set
  96. settings.SITE_ID = 1
  97. # ROOT_URLCONF needs to be set, else `AttributeErrors` are raised
  98. # when TestCases are torn down that have `urls` defined.
  99. settings.ROOT_URLCONF = ''
  100. def teardown_test_environment(self, **kwargs):
  101. super(GeoDjangoTestSuiteRunner, self).teardown_test_environment(**kwargs)
  102. settings.INSTALLED_APPS = self.old_installed
  103. settings.ROOT_URLCONF = self.old_root_urlconf
  104. settings.SITE_ID = self.old_site_id
  105. def build_suite(self, test_labels, extra_tests=None, **kwargs):
  106. return geodjango_suite()