2014-04-21 09:25:48 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
from django.contrib.gis.db.models import fields
|
|
|
|
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
|
|
|
|
2015-02-10 15:07:44 +00:00
|
|
|
from ..utils import mysql
|
|
|
|
|
2014-08-17 17:06:25 +00:00
|
|
|
if connection.features.gis_enabled:
|
2014-04-21 12:04:53 +00:00
|
|
|
try:
|
2014-06-21 12:07:30 +00:00
|
|
|
GeometryColumns = connection.ops.geometry_columns()
|
2014-04-21 12:04:53 +00:00
|
|
|
HAS_GEOMETRY_COLUMNS = True
|
2014-06-21 12:07:30 +00:00
|
|
|
except NotImplementedError:
|
2014-04-21 12:04:53 +00:00
|
|
|
HAS_GEOMETRY_COLUMNS = False
|
2014-04-21 09:25:48 +00:00
|
|
|
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
@skipUnlessDBFeature('gis_enabled')
|
2014-04-21 09:25:48 +00:00
|
|
|
class OperationTests(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')])
|
2014-04-21 21:08:00 +00:00
|
|
|
super(OperationTests, self).tearDown()
|
|
|
|
|
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:
|
|
|
|
test_fields += [('rast', fields.RasterField(srid=4326))]
|
|
|
|
operations = [migrations.CreateModel('Neighborhood', test_fields)]
|
2014-04-21 09:25:48 +00:00
|
|
|
return self.apply_operations('gis', ProjectState(), operations)
|
|
|
|
|
2014-09-18 11:42:51 +00:00
|
|
|
def assertGeometryColumnsCount(self, expected_count):
|
2015-06-19 15:46:03 +00:00
|
|
|
table_name = 'gis_neighborhood'
|
2014-09-18 11:42:51 +00:00
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
table_name = table_name.upper()
|
|
|
|
self.assertEqual(
|
|
|
|
GeometryColumns.objects.filter(**{
|
|
|
|
GeometryColumns.table_name_col(): table_name,
|
|
|
|
}).count(),
|
|
|
|
expected_count
|
|
|
|
)
|
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
def assertSpatialIndexExists(self, table, column):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, table)
|
|
|
|
self.assertIn(column, indexes)
|
|
|
|
|
|
|
|
def alter_gis_model(self, migration_class, model_name, field_name,
|
|
|
|
blank=False, field_class=None):
|
2014-04-21 09:25:48 +00:00
|
|
|
project_state = self.set_up_test_model()
|
2014-10-31 13:04:46 +00:00
|
|
|
self.current_state = project_state
|
2015-06-19 15:46:03 +00:00
|
|
|
args = [model_name, field_name]
|
|
|
|
if field_class:
|
|
|
|
args.append(field_class(srid=4326, blank=blank))
|
|
|
|
operation = migration_class(*args)
|
2014-04-21 09:25:48 +00:00
|
|
|
new_state = project_state.clone()
|
2015-06-19 15:46:03 +00:00
|
|
|
operation.state_forwards('gis', new_state)
|
2014-04-21 09:25:48 +00:00
|
|
|
with connection.schema_editor() as editor:
|
2015-06-19 15:46:03 +00:00
|
|
|
operation.database_forwards('gis', editor, project_state, new_state)
|
2014-09-17 23:45:42 +00:00
|
|
|
self.current_state = new_state
|
2015-06-19 15:46:03 +00:00
|
|
|
|
|
|
|
def test_add_geom_field(self):
|
|
|
|
"""
|
|
|
|
Test the AddField operation with a geometry-enabled column.
|
|
|
|
"""
|
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood',
|
|
|
|
'path', False, fields.LineStringField)
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood',
|
|
|
|
'heatmap', False, fields.RasterField)
|
|
|
|
self.assertColumnExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
# Test spatial indices when available
|
|
|
|
if self.has_spatial_indexes:
|
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
@skipIfDBFeature('supports_raster')
|
|
|
|
def test_create_raster_model_on_db_without_raster_support(self):
|
|
|
|
"""
|
|
|
|
Test creating a model with a raster field on a db without raster support.
|
|
|
|
"""
|
|
|
|
msg = 'Raster fields require backends with raster support.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.set_up_test_model(True)
|
|
|
|
|
|
|
|
@skipIfDBFeature('supports_raster')
|
|
|
|
def test_add_raster_field_on_db_without_raster_support(self):
|
|
|
|
"""
|
|
|
|
Test adding a raster field on a db without raster support.
|
|
|
|
"""
|
|
|
|
msg = 'Raster fields require backends with raster support.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.alter_gis_model(
|
|
|
|
migrations.AddField, 'Neighborhood', 'heatmap',
|
|
|
|
False, fields.RasterField
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2015-06-19 15:46:03 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood',
|
|
|
|
'path', True, fields.LineStringField)
|
|
|
|
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
|
|
|
"""
|
2015-06-19 15:46:03 +00:00
|
|
|
self.alter_gis_model(migrations.AddField, 'Neighborhood',
|
|
|
|
'heatmap', True, fields.RasterField)
|
|
|
|
self.assertColumnExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
# Test spatial indices when available
|
|
|
|
if self.has_spatial_indexes:
|
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'heatmap')
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.current_state = self.set_up_test_model()
|
|
|
|
|
|
|
|
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:
|
|
|
|
self.assertSpatialIndexExists('gis_neighborhood', 'rast')
|
2014-09-23 18:13:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_spatial_indexes(self):
|
|
|
|
if mysql:
|
|
|
|
with connection.cursor() as cursor:
|
2015-06-19 15:46:03 +00:00
|
|
|
return connection.introspection.supports_spatial_index(cursor, 'gis_neighborhood')
|
2014-09-23 18:13:59 +00:00
|
|
|
return True
|