test_operations.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from __future__ import unicode_literals
  2. from django.contrib.gis.db.models import fields
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.db import connection, migrations, models
  5. from django.db.migrations.migration import Migration
  6. from django.db.migrations.state import ProjectState
  7. from django.test import (
  8. TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
  9. )
  10. from ..utils import mysql
  11. if connection.features.gis_enabled:
  12. try:
  13. GeometryColumns = connection.ops.geometry_columns()
  14. HAS_GEOMETRY_COLUMNS = True
  15. except NotImplementedError:
  16. HAS_GEOMETRY_COLUMNS = False
  17. @skipUnlessDBFeature('gis_enabled')
  18. class OperationTests(TransactionTestCase):
  19. available_apps = ['gis_tests.gis_migrations']
  20. def tearDown(self):
  21. # Delete table after testing
  22. if hasattr(self, 'current_state'):
  23. self.apply_operations('gis', self.current_state, [migrations.DeleteModel('Neighborhood')])
  24. super(OperationTests, self).tearDown()
  25. def get_table_description(self, table):
  26. with connection.cursor() as cursor:
  27. return connection.introspection.get_table_description(cursor, table)
  28. def assertColumnExists(self, table, column):
  29. self.assertIn(column, [c.name for c in self.get_table_description(table)])
  30. def assertColumnNotExists(self, table, column):
  31. self.assertNotIn(column, [c.name for c in self.get_table_description(table)])
  32. def apply_operations(self, app_label, project_state, operations):
  33. migration = Migration('name', app_label)
  34. migration.operations = operations
  35. with connection.schema_editor() as editor:
  36. return migration.apply(project_state, editor)
  37. def set_up_test_model(self, force_raster_creation=False):
  38. test_fields = [
  39. ('id', models.AutoField(primary_key=True)),
  40. ('name', models.CharField(max_length=100, unique=True)),
  41. ('geom', fields.MultiPolygonField(srid=4326))
  42. ]
  43. if connection.features.supports_raster or force_raster_creation:
  44. test_fields += [('rast', fields.RasterField(srid=4326))]
  45. operations = [migrations.CreateModel('Neighborhood', test_fields)]
  46. return self.apply_operations('gis', ProjectState(), operations)
  47. def assertGeometryColumnsCount(self, expected_count):
  48. table_name = 'gis_neighborhood'
  49. if connection.features.uppercases_column_names:
  50. table_name = table_name.upper()
  51. self.assertEqual(
  52. GeometryColumns.objects.filter(**{
  53. GeometryColumns.table_name_col(): table_name,
  54. }).count(),
  55. expected_count
  56. )
  57. def assertSpatialIndexExists(self, table, column):
  58. with connection.cursor() as cursor:
  59. indexes = connection.introspection.get_indexes(cursor, table)
  60. self.assertIn(column, indexes)
  61. def alter_gis_model(self, migration_class, model_name, field_name,
  62. blank=False, field_class=None):
  63. project_state = self.set_up_test_model()
  64. self.current_state = project_state
  65. args = [model_name, field_name]
  66. if field_class:
  67. args.append(field_class(srid=4326, blank=blank))
  68. operation = migration_class(*args)
  69. new_state = project_state.clone()
  70. operation.state_forwards('gis', new_state)
  71. self.current_state = new_state
  72. with connection.schema_editor() as editor:
  73. operation.database_forwards('gis', editor, project_state, new_state)
  74. def test_add_geom_field(self):
  75. """
  76. Test the AddField operation with a geometry-enabled column.
  77. """
  78. self.alter_gis_model(migrations.AddField, 'Neighborhood', 'path', False, fields.LineStringField)
  79. self.assertColumnExists('gis_neighborhood', 'path')
  80. # Test GeometryColumns when available
  81. if HAS_GEOMETRY_COLUMNS:
  82. self.assertGeometryColumnsCount(2)
  83. # Test spatial indices when available
  84. if self.has_spatial_indexes:
  85. self.assertSpatialIndexExists('gis_neighborhood', 'path')
  86. @skipUnlessDBFeature('supports_raster')
  87. def test_add_raster_field(self):
  88. """
  89. Test the AddField operation with a raster-enabled column.
  90. """
  91. self.alter_gis_model(migrations.AddField, 'Neighborhood', 'heatmap', False, fields.RasterField)
  92. self.assertColumnExists('gis_neighborhood', 'heatmap')
  93. # Test spatial indices when available
  94. if self.has_spatial_indexes:
  95. self.assertSpatialIndexExists('gis_neighborhood', 'heatmap')
  96. @skipIfDBFeature('supports_raster')
  97. def test_create_raster_model_on_db_without_raster_support(self):
  98. """
  99. Test creating a model with a raster field on a db without raster support.
  100. """
  101. msg = 'Raster fields require backends with raster support.'
  102. with self.assertRaisesMessage(ImproperlyConfigured, msg):
  103. self.set_up_test_model(True)
  104. @skipIfDBFeature('supports_raster')
  105. def test_add_raster_field_on_db_without_raster_support(self):
  106. """
  107. Test adding a raster field on a db without raster support.
  108. """
  109. msg = 'Raster fields require backends with raster support.'
  110. with self.assertRaisesMessage(ImproperlyConfigured, msg):
  111. self.alter_gis_model(
  112. migrations.AddField, 'Neighborhood', 'heatmap',
  113. False, fields.RasterField
  114. )
  115. def test_add_blank_geom_field(self):
  116. """
  117. Should be able to add a GeometryField with blank=True.
  118. """
  119. self.alter_gis_model(migrations.AddField, 'Neighborhood', 'path', True, fields.LineStringField)
  120. self.assertColumnExists('gis_neighborhood', 'path')
  121. # Test GeometryColumns when available
  122. if HAS_GEOMETRY_COLUMNS:
  123. self.assertGeometryColumnsCount(2)
  124. # Test spatial indices when available
  125. if self.has_spatial_indexes:
  126. self.assertSpatialIndexExists('gis_neighborhood', 'path')
  127. @skipUnlessDBFeature('supports_raster')
  128. def test_add_blank_raster_field(self):
  129. """
  130. Should be able to add a RasterField with blank=True.
  131. """
  132. self.alter_gis_model(migrations.AddField, 'Neighborhood', 'heatmap', True, fields.RasterField)
  133. self.assertColumnExists('gis_neighborhood', 'heatmap')
  134. # Test spatial indices when available
  135. if self.has_spatial_indexes:
  136. self.assertSpatialIndexExists('gis_neighborhood', 'heatmap')
  137. def test_remove_geom_field(self):
  138. """
  139. Test the RemoveField operation with a geometry-enabled column.
  140. """
  141. self.alter_gis_model(migrations.RemoveField, 'Neighborhood', 'geom')
  142. self.assertColumnNotExists('gis_neighborhood', 'geom')
  143. # Test GeometryColumns when available
  144. if HAS_GEOMETRY_COLUMNS:
  145. self.assertGeometryColumnsCount(0)
  146. @skipUnlessDBFeature('supports_raster')
  147. def test_remove_raster_field(self):
  148. """
  149. Test the RemoveField operation with a raster-enabled column.
  150. """
  151. self.alter_gis_model(migrations.RemoveField, 'Neighborhood', 'rast')
  152. self.assertColumnNotExists('gis_neighborhood', 'rast')
  153. def test_create_model_spatial_index(self):
  154. self.current_state = self.set_up_test_model()
  155. if not self.has_spatial_indexes:
  156. self.skipTest('No support for Spatial indexes')
  157. self.assertSpatialIndexExists('gis_neighborhood', 'geom')
  158. if connection.features.supports_raster:
  159. self.assertSpatialIndexExists('gis_neighborhood', 'rast')
  160. @property
  161. def has_spatial_indexes(self):
  162. if mysql:
  163. with connection.cursor() as cursor:
  164. return connection.introspection.supports_spatial_index(cursor, 'gis_neighborhood')
  165. return True