2016-09-19 07:58:58 +00:00
|
|
|
from unittest import skipIf
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
from django.contrib.gis.db.models import fields
|
2016-09-19 07:58:58 +00:00
|
|
|
from django.contrib.gis.geos import MultiPolygon, Polygon
|
2015-06-19 15:46:03 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-04-21 09:25:48 +00:00
|
|
|
from django.db import connection, migrations, models
|
|
|
|
from django.db.migrations.migration import Migration
|
|
|
|
from django.db.migrations.state import ProjectState
|
2015-06-19 15:46:03 +00:00
|
|
|
from django.test import (
|
|
|
|
TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
|
|
|
|
)
|
2014-04-21 09:25:48 +00:00
|
|
|
|
2016-09-19 07:58:58 +00:00
|
|
|
from ..utils import mysql, spatialite
|
2015-02-10 15:07:44 +00:00
|
|
|
|
2017-05-04 14:14:35 +00:00
|
|
|
try:
|
|
|
|
GeometryColumns = connection.ops.geometry_columns()
|
|
|
|
HAS_GEOMETRY_COLUMNS = True
|
|
|
|
except NotImplementedError:
|
|
|
|
HAS_GEOMETRY_COLUMNS = False
|
2014-04-21 09:25:48 +00:00
|
|
|
|
|
|
|
|
2016-09-19 07:58:10 +00:00
|
|
|
class OperationTestCase(TransactionTestCase):
|
2015-06-19 15:46:03 +00:00
|
|
|
available_apps = ['gis_tests.gis_migrations']
|
2014-04-21 09:25:48 +00:00
|
|
|
|
2014-04-21 21:08:00 +00:00
|
|
|
def tearDown(self):
|
|
|
|
# Delete table after testing
|
2015-06-19 15:46:03 +00:00
|
|
|
if hasattr(self, 'current_state'):
|
|
|
|
self.apply_operations('gis', self.current_state, [migrations.DeleteModel('Neighborhood')])
|
2017-01-21 13:13:44 +00:00
|
|
|
super().tearDown()
|
2016-09-19 07:58:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_spatial_indexes(self):
|
|
|
|
if mysql:
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
return connection.introspection.supports_spatial_index(cursor, 'gis_neighborhood')
|
|
|
|
return True
|
2014-04-21 21:08:00 +00:00
|
|
|
|
2014-04-21 09:25:48 +00:00
|
|
|
def get_table_description(self, table):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
return connection.introspection.get_table_description(cursor, table)
|
|
|
|
|
|
|
|
def assertColumnExists(self, table, column):
|
|
|
|
self.assertIn(column, [c.name for c in self.get_table_description(table)])
|
|
|
|
|
2014-04-21 12:04:53 +00:00
|
|
|
def assertColumnNotExists(self, table, column):
|
|
|
|
self.assertNotIn(column, [c.name for c in self.get_table_description(table)])
|
|
|
|
|
2014-04-21 09:25:48 +00:00
|
|
|
def apply_operations(self, app_label, project_state, operations):
|
|
|
|
migration = Migration('name', app_label)
|
|
|
|
migration.operations = operations
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
return migration.apply(project_state, editor)
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
def set_up_test_model(self, force_raster_creation=False):
|
|
|
|
test_fields = [
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('name', models.CharField(max_length=100, unique=True)),
|
|
|
|
('geom', fields.MultiPolygonField(srid=4326))
|
|
|
|
]
|
|
|
|
if connection.features.supports_raster or force_raster_creation:
|
2016-09-19 07:58:58 +00:00
|
|
|
test_fields += [('rast', fields.RasterField(srid=4326, null=True))]
|
2015-06-19 15:46:03 +00:00
|
|
|
operations = [migrations.CreateModel('Neighborhood', test_fields)]
|
2016-09-19 07:58:10 +00:00
|
|
|
self.current_state = self.apply_operations('gis', ProjectState(), operations)
|
2014-04-21 09:25:48 +00:00
|
|
|
|
2014-09-18 11:42:51 +00:00
|
|
|
def assertGeometryColumnsCount(self, expected_count):
|
|
|
|
self.assertEqual(
|
|
|
|
GeometryColumns.objects.filter(**{
|
2018-11-21 08:06:50 +00:00
|
|
|
'%s__iexact' % GeometryColumns.table_name_col(): 'gis_neighborhood',
|
2014-09-18 11:42:51 +00:00
|
|
|
}).count(),
|
|
|
|
expected_count
|
|
|
|
)
|
|
|
|
|
2016-08-31 19:05:39 +00:00
|
|
|
def assertSpatialIndexExists(self, table, column, raster=False):
|
2015-06-19 15:46:03 +00:00
|
|
|
with connection.cursor() as cursor:
|
2016-08-20 10:14:02 +00:00
|
|
|
constraints = connection.introspection.get_constraints(cursor, table)
|
2016-08-31 19:05:39 +00:00
|
|
|
if raster:
|
|
|
|
self.assertTrue(any(
|
|
|
|
'st_convexhull(%s)' % column in c['definition']
|
|
|
|
for c in constraints.values()
|
|
|
|
if c['definition'] is not None
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
self.assertIn([column], [c['columns'] for c in constraints.values()])
|
2015-06-19 15:46:03 +00:00
|
|
|
|
|
|
|
def alter_gis_model(self, migration_class, model_name, field_name,
|
2016-09-19 07:58:10 +00:00
|
|
|
blank=False, field_class=None, field_class_kwargs=None):
|
2015-06-19 15:46:03 +00:00
|
|
|
args = [model_name, field_name]
|
|
|
|
if field_class:
|
2016-09-19 07:58:10 +00:00
|
|
|
field_class_kwargs = field_class_kwargs or {'srid': 4326, 'blank': blank}
|
|
|
|
args.append(field_class(**field_class_kwargs))
|
2015-06-19 15:46:03 +00:00
|
|
|
operation = migration_class(*args)
|
2016-09-19 07:58:10 +00:00
|
|
|
old_state = self.current_state.clone()
|
|
|
|
operation.state_forwards('gis', self.current_state)
|
2014-04-21 09:25:48 +00:00
|
|
|
with connection.schema_editor() as editor:
|
2016-09-19 07:58:10 +00:00
|
|
|
operation.database_forwards('gis', editor, old_state, self.current_state)
|
|
|
|
|
|
|
|
|
|
|
|
class OperationTests(OperationTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2016-09-19 07:58:10 +00:00
|
|
|
self.set_up_test_model()
|
2015-06-19 15:46:03 +00:00
|
|
|
|
|
|
|
def test_add_geom_field(self):
|
|
|
|
"""
|
|
|
|
Test the AddField operation with a geometry-enabled column.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood', 'path', False, fields.LineStringField)
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertColumnExists('gis_neighborhood', 'path')
|
2014-04-21 09:25:48 +00:00
|
|
|
|
|
|
|
# Test GeometryColumns when available
|
2014-04-21 12:04:53 +00:00
|
|
|
if HAS_GEOMETRY_COLUMNS:
|
2014-09-18 11:42:51 +00:00
|
|
|
self.assertGeometryColumnsCount(2)
|
2014-04-21 12:04:53 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
# Test spatial indices when available
|
2014-09-23 18:13:59 +00:00
|
|
|
if self.has_spatial_indexes:
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'path')
|
2014-09-23 18:13:59 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
@skipUnlessDBFeature('supports_raster')
|
|
|
|
def test_add_raster_field(self):
|
|
|
|
"""
|
|
|
|
Test the AddField operation with a raster-enabled column.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood', 'heatmap', False, fields.RasterField)
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertColumnExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
# Test spatial indices when available
|
|
|
|
if self.has_spatial_indexes:
|
2016-08-31 19:05:39 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'heatmap', raster=True)
|
2015-06-19 15:46:03 +00:00
|
|
|
|
|
|
|
def test_add_blank_geom_field(self):
|
2014-10-31 13:04:46 +00:00
|
|
|
"""
|
|
|
|
Should be able to add a GeometryField with blank=True.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood', 'path', True, fields.LineStringField)
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertColumnExists('gis_neighborhood', 'path')
|
2014-10-31 13:04:46 +00:00
|
|
|
|
|
|
|
# Test GeometryColumns when available
|
|
|
|
if HAS_GEOMETRY_COLUMNS:
|
|
|
|
self.assertGeometryColumnsCount(2)
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
# Test spatial indices when available
|
2014-10-31 13:04:46 +00:00
|
|
|
if self.has_spatial_indexes:
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'path')
|
2014-10-31 13:04:46 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
@skipUnlessDBFeature('supports_raster')
|
|
|
|
def test_add_blank_raster_field(self):
|
2014-04-21 12:04:53 +00:00
|
|
|
"""
|
2015-06-19 15:46:03 +00:00
|
|
|
Should be able to add a RasterField with blank=True.
|
2014-04-21 12:04:53 +00:00
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood', 'heatmap', True, fields.RasterField)
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertColumnExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
# Test spatial indices when available
|
|
|
|
if self.has_spatial_indexes:
|
2016-08-31 19:05:39 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'heatmap', raster=True)
|
2015-06-19 15:46:03 +00:00
|
|
|
|
|
|
|
def test_remove_geom_field(self):
|
|
|
|
"""
|
|
|
|
Test the RemoveField operation with a geometry-enabled column.
|
|
|
|
"""
|
|
|
|
self.alter_gis_model(migrations.RemoveField, 'Neighborhood', 'geom')
|
|
|
|
self.assertColumnNotExists('gis_neighborhood', 'geom')
|
2014-04-21 12:04:53 +00:00
|
|
|
|
|
|
|
# Test GeometryColumns when available
|
|
|
|
if HAS_GEOMETRY_COLUMNS:
|
2014-09-18 11:42:51 +00:00
|
|
|
self.assertGeometryColumnsCount(0)
|
2014-09-23 18:13:59 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
@skipUnlessDBFeature('supports_raster')
|
|
|
|
def test_remove_raster_field(self):
|
|
|
|
"""
|
|
|
|
Test the RemoveField operation with a raster-enabled column.
|
|
|
|
"""
|
|
|
|
self.alter_gis_model(migrations.RemoveField, 'Neighborhood', 'rast')
|
|
|
|
self.assertColumnNotExists('gis_neighborhood', 'rast')
|
|
|
|
|
2014-09-23 18:13:59 +00:00
|
|
|
def test_create_model_spatial_index(self):
|
|
|
|
if not self.has_spatial_indexes:
|
2015-06-19 15:46:03 +00:00
|
|
|
self.skipTest('No support for Spatial indexes')
|
2014-09-23 18:13:59 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'geom')
|
|
|
|
|
|
|
|
if connection.features.supports_raster:
|
2016-08-31 19:05:39 +00:00
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'rast', raster=True)
|
2014-09-23 18:13:59 +00:00
|
|
|
|
2016-09-19 07:58:58 +00:00
|
|
|
@skipUnlessDBFeature("supports_3d_storage")
|
|
|
|
@skipIf(spatialite, "Django currently doesn't support altering Spatialite geometry fields")
|
|
|
|
def test_alter_geom_field_dim(self):
|
|
|
|
Neighborhood = self.current_state.apps.get_model('gis', 'Neighborhood')
|
|
|
|
p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
|
|
|
|
Neighborhood.objects.create(name='TestDim', geom=MultiPolygon(p1, p1))
|
|
|
|
# Add 3rd dimension.
|
|
|
|
self.alter_gis_model(
|
|
|
|
migrations.AlterField, 'Neighborhood', 'geom', False,
|
|
|
|
fields.MultiPolygonField, field_class_kwargs={'srid': 4326, 'dim': 3}
|
|
|
|
)
|
|
|
|
self.assertTrue(Neighborhood.objects.first().geom.hasz)
|
|
|
|
# Rewind to 2 dimensions.
|
|
|
|
self.alter_gis_model(
|
|
|
|
migrations.AlterField, 'Neighborhood', 'geom', False,
|
|
|
|
fields.MultiPolygonField, field_class_kwargs={'srid': 4326, 'dim': 2}
|
|
|
|
)
|
|
|
|
self.assertFalse(Neighborhood.objects.first().geom.hasz)
|
|
|
|
|
2016-09-19 07:58:10 +00:00
|
|
|
|
|
|
|
@skipIfDBFeature('supports_raster')
|
|
|
|
class NoRasterSupportTests(OperationTestCase):
|
|
|
|
def test_create_raster_model_on_db_without_raster_support(self):
|
|
|
|
msg = 'Raster fields require backends with raster support.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.set_up_test_model(force_raster_creation=True)
|
|
|
|
|
|
|
|
def test_add_raster_field_on_db_without_raster_support(self):
|
|
|
|
msg = 'Raster fields require backends with raster support.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.set_up_test_model()
|
|
|
|
self.alter_gis_model(
|
|
|
|
migrations.AddField, 'Neighborhood', 'heatmap',
|
|
|
|
False, fields.RasterField
|
|
|
|
)
|