Debian Codegraph @

tests/gis_tests/gis_migrations/test_operations.py

raw
1 from unittest import skipUnless
2
3 from django.contrib.gis.db.models import fields
4 from django.contrib.gis.geos import MultiPolygon, Polygon
5 from django.core.exceptions import ImproperlyConfigured
6 from django.db import connection, migrations, models
7 from django.db.migrations.migration import Migration
8 from django.db.migrations.state import ProjectState
9 from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature
10
11 try:
12 GeometryColumns = connection.ops.geometry_columns()
13 HAS_GEOMETRY_COLUMNS = True
14 except NotImplementedError:
15 HAS_GEOMETRY_COLUMNS = False
16
17
18 class OperationTestCase(TransactionTestCase):
19 available_apps = ["gis_tests.gis_migrations"]
20 get_opclass_query = """
21 SELECT opcname, c.relname FROM pg_opclass AS oc
22 JOIN pg_index as i on oc.oid = ANY(i.indclass)
23 JOIN pg_class as c on c.oid = i.indexrelid
24 WHERE c.relname = %s
25 """
26
27 def tearDown(self):
28 # Delete table after testing
29 if hasattr(self, "current_state"):
30 self.apply_operations(
31 "gis", self.current_state, [migrations.DeleteModel("Neighborhood")]
32 )
33 super().tearDown()
34
35 @property
36 def has_spatial_indexes(self):
37 if connection.ops.mysql:
38 with connection.cursor() as cursor:
39 return connection.introspection.supports_spatial_index(
40 cursor, "gis_neighborhood"
41 )
42 return True
43
44 def get_table_description(self, table):
45 with connection.cursor() as cursor:
46 return connection.introspection.get_table_description(cursor, table)
47
48 def assertColumnExists(self, table, column):
49 self.assertIn(column, [c.name for c in self.get_table_description(table)])
50
51 def assertColumnNotExists(self, table, column):
52 self.assertNotIn(column, [c.name for c in self.get_table_description(table)])
53
54 def apply_operations(self, app_label, project_state, operations):
55 migration = Migration("name", app_label)
56 migration.operations = operations
57 with connection.schema_editor() as editor:
58 return migration.apply(project_state, editor)
59
60 def set_up_test_model(self, force_raster_creation=False):
61 test_fields = [
62 ("id", models.AutoField(primary_key=True)),
63 ("name", models.CharField(max_length=100, unique=True)),
64 ("geom", fields.MultiPolygonField(srid=4326)),
65 ]
66 if connection.features.supports_raster or force_raster_creation:
67 test_fields += [("rast", fields.RasterField(srid=4326, null=True))]
68 operations = [migrations.CreateModel("Neighborhood", test_fields)]
69 self.current_state = self.apply_operations("gis", ProjectState(), operations)
70
71 def assertGeometryColumnsCount(self, expected_count):
72 self.assertEqual(
73 GeometryColumns.objects.filter(
74 **{
75 "%s__iexact" % GeometryColumns.table_name_col(): "gis_neighborhood",
76 }
77 ).count(),
78 expected_count,
79 )
80
81 def assertSpatialIndexExists(self, table, column, raster=False):
82 with connection.cursor() as cursor:
83 constraints = connection.introspection.get_constraints(cursor, table)
84 if raster:
85 self.assertTrue(
86 any(
87 "st_convexhull(%s)" % column in c["definition"]
88 for c in constraints.values()
89 if c["definition"] is not None
90 )
91 )
92 else:
93 self.assertIn([column], [c["columns"] for c in constraints.values()])
94
95 def assertSpatialIndexNotExists(self, table, column, raster=False):
96 with connection.cursor() as cursor:
97 constraints = connection.introspection.get_constraints(cursor, table)
98 if raster:
99 self.assertFalse(
100 any(
101 "st_convexhull(%s)" % column in c["definition"]
102 for c in constraints.values()
103 if c["definition"] is not None
104 )
105 )
106 else:
107 self.assertNotIn([column], [c["columns"] for c in constraints.values()])
108
109 def alter_gis_model(
110 self,
111 migration_class,
112 model_name,
113 field_name,
114 field_class=None,
115 field_class_kwargs=None,
116 ):
117 args = [model_name, field_name]
118 if field_class:
119 field_class_kwargs = field_class_kwargs or {}
120 args.append(field_class(**field_class_kwargs))
121 operation = migration_class(*args)
122 old_state = self.current_state.clone()
123 operation.state_forwards("gis", self.current_state)
124 with connection.schema_editor() as editor:
125 operation.database_forwards("gis", editor, old_state, self.current_state)
126
127
128 class OperationTests(OperationTestCase):
129 def setUp(self):
130 super().setUp()
131 self.set_up_test_model()
132
133 def test_add_geom_field(self):
134 """
135 Test the AddField operation with a geometry-enabled column.
136 """
137 self.alter_gis_model(
138 migrations.AddField, "Neighborhood", "path", fields.LineStringField
139 )
140 self.assertColumnExists("gis_neighborhood", "path")
141
142 # Test GeometryColumns when available
143 if HAS_GEOMETRY_COLUMNS:
144 self.assertGeometryColumnsCount(2)
145
146 # Test spatial indices when available
147 if self.has_spatial_indexes:
148 self.assertSpatialIndexExists("gis_neighborhood", "path")
149
150 @skipUnless(connection.vendor == "mysql", "MySQL specific test")
151 def test_remove_geom_field_nullable_with_index(self):
152 # MySQL doesn't support spatial indexes on NULL columns.
153 with self.assertNumQueries(1) as ctx:
154 self.alter_gis_model(
155 migrations.AddField,
156 "Neighborhood",
157 "path",
158 fields.LineStringField,
159 field_class_kwargs={"null": True},
160 )
161 self.assertColumnExists("gis_neighborhood", "path")
162 self.assertNotIn("CREATE SPATIAL INDEX", ctx.captured_queries[0]["sql"])
163
164 with self.assertNumQueries(1), self.assertNoLogs("django.contrib.gis", "ERROR"):
165 self.alter_gis_model(migrations.RemoveField, "Neighborhood", "path")
166 self.assertColumnNotExists("gis_neighborhood", "path")
167
168 @skipUnless(HAS_GEOMETRY_COLUMNS, "Backend doesn't support GeometryColumns.")
169 def test_geom_col_name(self):
170 self.assertEqual(
171 GeometryColumns.geom_col_name(),
172 "column_name" if connection.ops.oracle else "f_geometry_column",
173 )
174
175 @skipUnlessDBFeature("supports_raster")
176 def test_add_raster_field(self):
177 """
178 Test the AddField operation with a raster-enabled column.
179 """
180 self.alter_gis_model(
181 migrations.AddField, "Neighborhood", "heatmap", fields.RasterField
182 )
183 self.assertColumnExists("gis_neighborhood", "heatmap")
184
185 # Test spatial indices when available
186 if self.has_spatial_indexes:
187 self.assertSpatialIndexExists("gis_neighborhood", "heatmap", raster=True)
188
189 def test_add_blank_geom_field(self):
190 """
191 Should be able to add a GeometryField with blank=True.
192 """
193 self.alter_gis_model(
194 migrations.AddField,
195 "Neighborhood",
196 "path",
197 fields.LineStringField,
198 field_class_kwargs={"blank": True},
199 )
200 self.assertColumnExists("gis_neighborhood", "path")
201
202 # Test GeometryColumns when available
203 if HAS_GEOMETRY_COLUMNS:
204 self.assertGeometryColumnsCount(2)
205
206 # Test spatial indices when available
207 if self.has_spatial_indexes:
208 self.assertSpatialIndexExists("gis_neighborhood", "path")
209
210 @skipUnlessDBFeature("supports_raster")
211 def test_add_blank_raster_field(self):
212 """
213 Should be able to add a RasterField with blank=True.
214 """
215 self.alter_gis_model(
216 migrations.AddField,
217 "Neighborhood",
218 "heatmap",
219 fields.RasterField,
220 field_class_kwargs={"blank": True},
221 )
222 self.assertColumnExists("gis_neighborhood", "heatmap")
223
224 # Test spatial indices when available
225 if self.has_spatial_indexes:
226 self.assertSpatialIndexExists("gis_neighborhood", "heatmap", raster=True)
227
228 def test_remove_geom_field(self):
229 """
230 Test the RemoveField operation with a geometry-enabled column.
231 """
232 self.alter_gis_model(migrations.RemoveField, "Neighborhood", "geom")
233 self.assertColumnNotExists("gis_neighborhood", "geom")
234
235 # Test GeometryColumns when available
236 if HAS_GEOMETRY_COLUMNS:
237 self.assertGeometryColumnsCount(0)
238
239 @skipUnlessDBFeature("supports_raster")
240 def test_remove_raster_field(self):
241 """
242 Test the RemoveField operation with a raster-enabled column.
243 """
244 self.alter_gis_model(migrations.RemoveField, "Neighborhood", "rast")
245 self.assertColumnNotExists("gis_neighborhood", "rast")
246
247 def test_create_model_spatial_index(self):
248 if not self.has_spatial_indexes:
249 self.skipTest("No support for Spatial indexes")
250
251 self.assertSpatialIndexExists("gis_neighborhood", "geom")
252
253 if connection.features.supports_raster:
254 self.assertSpatialIndexExists("gis_neighborhood", "rast", raster=True)
255
256 @skipUnlessDBFeature("can_alter_geometry_field")
257 def test_alter_field_add_spatial_index(self):
258 if not self.has_spatial_indexes:
259 self.skipTest("No support for Spatial indexes")
260
261 self.alter_gis_model(
262 migrations.AddField,
263 "Neighborhood",
264 "point",
265 fields.PointField,
266 field_class_kwargs={"spatial_index": False},
267 )
268 self.assertSpatialIndexNotExists("gis_neighborhood", "point")
269
270 self.alter_gis_model(
271 migrations.AlterField,
272 "Neighborhood",
273 "point",
274 fields.PointField,
275 field_class_kwargs={"spatial_index": True},
276 )
277 self.assertSpatialIndexExists("gis_neighborhood", "point")
278
279 @skipUnlessDBFeature("can_alter_geometry_field")
280 def test_alter_field_remove_spatial_index(self):
281 if not self.has_spatial_indexes:
282 self.skipTest("No support for Spatial indexes")
283
284 self.assertSpatialIndexExists("gis_neighborhood", "geom")
285
286 self.alter_gis_model(
287 migrations.AlterField,
288 "Neighborhood",
289 "geom",
290 fields.MultiPolygonField,
291 field_class_kwargs={"spatial_index": False},
292 )
293 self.assertSpatialIndexNotExists("gis_neighborhood", "geom")
294
295 @skipUnlessDBFeature("can_alter_geometry_field")
296 @skipUnless(connection.vendor == "mysql", "MySQL specific test")
297 def test_alter_field_nullable_with_spatial_index(self):
298 if not self.has_spatial_indexes:
299 self.skipTest("No support for Spatial indexes")
300
301 self.alter_gis_model(
302 migrations.AddField,
303 "Neighborhood",
304 "point",
305 fields.PointField,
306 field_class_kwargs={"spatial_index": False, "null": True},
307 )
308 # MySQL doesn't support spatial indexes on NULL columns.
309 self.assertSpatialIndexNotExists("gis_neighborhood", "point")
310
311 self.alter_gis_model(
312 migrations.AlterField,
313 "Neighborhood",
314 "point",
315 fields.PointField,
316 field_class_kwargs={"spatial_index": True, "null": True},
317 )
318 self.assertSpatialIndexNotExists("gis_neighborhood", "point")
319
320 self.alter_gis_model(
321 migrations.AlterField,
322 "Neighborhood",
323 "point",
324 fields.PointField,
325 field_class_kwargs={"spatial_index": False, "null": True},
326 )
327 self.assertSpatialIndexNotExists("gis_neighborhood", "point")
328
329 @skipUnlessDBFeature("can_alter_geometry_field")
330 def test_alter_field_with_spatial_index(self):
331 if not self.has_spatial_indexes:
332 self.skipTest("No support for Spatial indexes")
333
334 self.alter_gis_model(
335 migrations.AddField,
336 "Neighborhood",
337 "point",
338 fields.PointField,
339 field_class_kwargs={"spatial_index": True},
340 )
341 self.assertSpatialIndexExists("gis_neighborhood", "point")
342
343 self.alter_gis_model(
344 migrations.AlterField,
345 "Neighborhood",
346 "point",
347 fields.PointField,
348 field_class_kwargs={"spatial_index": True, "srid": 3086},
349 )
350 self.assertSpatialIndexExists("gis_neighborhood", "point")
351
352 @skipUnlessDBFeature("supports_3d_storage")
353 def test_add_3d_field_opclass(self):
354 if not connection.ops.postgis:
355 self.skipTest("PostGIS-specific test.")
356
357 self.alter_gis_model(
358 migrations.AddField,
359 "Neighborhood",
360 "point3d",
361 field_class=fields.PointField,
362 field_class_kwargs={"dim": 3},
363 )
364 self.assertColumnExists("gis_neighborhood", "point3d")
365 self.assertSpatialIndexExists("gis_neighborhood", "point3d")
366
367 with connection.cursor() as cursor:
368 index_name = "gis_neighborhood_point3d_113bc868_id"
369 cursor.execute(self.get_opclass_query, [index_name])
370 self.assertEqual(
371 cursor.fetchall(),
372 [("gist_geometry_ops_nd", index_name)],
373 )
374
375 @skipUnlessDBFeature("can_alter_geometry_field", "supports_3d_storage")
376 def test_alter_geom_field_dim(self):
377 Neighborhood = self.current_state.apps.get_model("gis", "Neighborhood")
378 p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
379 Neighborhood.objects.create(name="TestDim", geom=MultiPolygon(p1, p1))
380 # Add 3rd dimension.
381 self.alter_gis_model(
382 migrations.AlterField,
383 "Neighborhood",
384 "geom",
385 fields.MultiPolygonField,
386 field_class_kwargs={"dim": 3},
387 )
388 self.assertTrue(Neighborhood.objects.first().geom.hasz)
389 # Rewind to 2 dimensions.
390 self.alter_gis_model(
391 migrations.AlterField,
392 "Neighborhood",
393 "geom",
394 fields.MultiPolygonField,
395 field_class_kwargs={"dim": 2},
396 )
397 self.assertFalse(Neighborhood.objects.first().geom.hasz)
398
399 @skipUnlessDBFeature(
400 "supports_column_check_constraints", "can_introspect_check_constraints"
401 )
402 def test_add_check_constraint(self):
403 Neighborhood = self.current_state.apps.get_model("gis", "Neighborhood")
404 poly = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
405 constraint = models.CheckConstraint(
406 condition=models.Q(geom=poly),
407 name="geom_within_constraint",
408 )
409 Neighborhood._meta.constraints = [constraint]
410 with connection.schema_editor() as editor:
411 editor.add_constraint(Neighborhood, constraint)
412 with connection.cursor() as cursor:
413 constraints = connection.introspection.get_constraints(
414 cursor,
415 Neighborhood._meta.db_table,
416 )
417 self.assertIn("geom_within_constraint", constraints)
418
419
420 @skipIfDBFeature("supports_raster")
421 class NoRasterSupportTests(OperationTestCase):
422 def test_create_raster_model_on_db_without_raster_support(self):
423 msg = "Raster fields require backends with raster support."
424 with self.assertRaisesMessage(ImproperlyConfigured, msg):
425 self.set_up_test_model(force_raster_creation=True)
426
427 def test_add_raster_field_on_db_without_raster_support(self):
428 msg = "Raster fields require backends with raster support."
429 with self.assertRaisesMessage(ImproperlyConfigured, msg):
430 self.set_up_test_model()
431 self.alter_gis_model(
432 migrations.AddField, "Neighborhood", "heatmap", fields.RasterField
433 )