PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/eric-brechemier/django
Python | 108 lines | 67 code | 22 blank | 19 comment | 12 complexity | f524be6087d972e5621dc5fe91c0fe6d MD5 | raw file
  1. import sys
  2. import unittest
  3. from django.conf import settings
  4. from django.db.models import get_app
  5. from django.test.simple import build_suite, DjangoTestSuiteRunner
  6. def run_tests(*args, **kwargs):
  7. from django.test.simple import run_tests as base_run_tests
  8. return base_run_tests(*args, **kwargs)
  9. def run_gis_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=None):
  10. import warnings
  11. warnings.warn(
  12. 'The run_gis_tests() test runner has been deprecated in favor of GeoDjangoTestSuiteRunner.',
  13. PendingDeprecationWarning
  14. )
  15. test_runner = GeoDjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
  16. return test_runner.run_tests(test_labels, extra_tests=extra_tests)
  17. class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner):
  18. def setup_test_environment(self, **kwargs):
  19. super(GeoDjangoTestSuiteRunner, self).setup_test_environment(**kwargs)
  20. from django.db import connection
  21. from django.contrib.gis.geos import GEOS_PREPARE
  22. from django.contrib.gis.gdal import HAS_GDAL
  23. # Getting and storing the original values of INSTALLED_APPS and
  24. # the ROOT_URLCONF.
  25. self.old_installed = settings.INSTALLED_APPS
  26. self.old_root_urlconf = settings.ROOT_URLCONF
  27. # Tests that require use of a spatial database (e.g., creation of models)
  28. self.geo_apps = ['geoapp', 'relatedapp']
  29. if connection.ops.postgis and connection.ops.geography:
  30. # Test geography support with PostGIS 1.5+.
  31. self.geo_apps.append('geogapp')
  32. if HAS_GDAL:
  33. # The following GeoDjango test apps depend on GDAL support.
  34. if not connection.ops.mysql:
  35. self.geo_apps.append('distapp')
  36. # 3D apps use LayerMapping, which uses GDAL.
  37. if connection.ops.postgis and GEOS_PREPARE:
  38. self.geo_apps.append('geo3d')
  39. self.geo_apps.append('layermap')
  40. # Constructing the new INSTALLED_APPS, and including applications
  41. # within the GeoDjango test namespace (`self.geo_apps`).
  42. new_installed = ['django.contrib.sites',
  43. 'django.contrib.sitemaps',
  44. 'django.contrib.gis',
  45. ]
  46. new_installed.extend(['django.contrib.gis.tests.%s' % app
  47. for app in self.geo_apps])
  48. settings.INSTALLED_APPS = new_installed
  49. # Setting the URLs.
  50. settings.ROOT_URLCONF = 'django.contrib.gis.tests.urls'
  51. def teardown_test_environment(self, **kwargs):
  52. super(GeoDjangoTestSuiteRunner, self).teardown_test_environment(**kwargs)
  53. settings.INSTALLED_APPS = self.old_installed
  54. settings.ROOT_URLCONF = self.old_root_urlconf
  55. def build_suite(self, test_labels, extra_tests=None, **kwargs):
  56. """
  57. This method is overridden to construct a suite consisting only of tests
  58. for GeoDjango.
  59. """
  60. suite = unittest.TestSuite()
  61. # Adding the GEOS tests.
  62. from django.contrib.gis.geos import tests as geos_tests
  63. suite.addTest(geos_tests.suite())
  64. # Adding the measurment tests.
  65. from django.contrib.gis.tests import test_measure
  66. suite.addTest(test_measure.suite())
  67. # Adding GDAL tests, and any test suite that depends on GDAL, to the
  68. # suite if GDAL is available.
  69. from django.contrib.gis.gdal import HAS_GDAL
  70. if HAS_GDAL:
  71. from django.contrib.gis.gdal import tests as gdal_tests
  72. suite.addTest(gdal_tests.suite())
  73. from django.contrib.gis.tests import test_spatialrefsys, test_geoforms
  74. suite.addTest(test_spatialrefsys.suite())
  75. suite.addTest(test_geoforms.suite())
  76. else:
  77. sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n')
  78. # Add GeoIP tests to the suite, if the library and data is available.
  79. from django.contrib.gis.utils import HAS_GEOIP
  80. if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'):
  81. from django.contrib.gis.tests import test_geoip
  82. suite.addTest(test_geoip.suite())
  83. # Finally, adding the suites for each of the GeoDjango test apps.
  84. for app_name in self.geo_apps:
  85. suite.addTest(build_suite(get_app(app_name)))
  86. return suite