<s> from __future__ import unicode_literals <EOL> from django . contrib . gis . db . models import fields <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import connection , migrations , models <EOL> from django . db . migrations . migration import Migration <EOL> from django . db . migrations . state import ProjectState <EOL> from django . test import ( <EOL> TransactionTestCase , skipIfDBFeature , skipUnlessDBFeature , <EOL> ) <EOL> from . . utils import mysql <EOL> if connection . features . gis_enabled : <EOL> try : <EOL> GeometryColumns = connection . ops . geometry_columns ( ) <EOL> HAS_GEOMETRY_COLUMNS = True <EOL> except NotImplementedError : <EOL> HAS_GEOMETRY_COLUMNS = False <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> class OperationTests ( TransactionTestCase ) : <EOL> available_apps = [ '<STR_LIT>' ] <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . apply_operations ( '<STR_LIT>' , self . current_state , [ migrations . DeleteModel ( '<STR_LIT>' ) ] ) <EOL> super ( OperationTests , self ) . tearDown ( ) <EOL> def get_table_description ( self , table ) : <EOL> with connection . cursor ( ) as cursor : <EOL> return connection . introspection . get_table_description ( cursor , table ) <EOL> def assertColumnExists ( self , table , column ) : <EOL> self . assertIn ( column , [ c . name for c in self . get_table_description ( table ) ] ) <EOL> def assertColumnNotExists ( self , table , column ) : <EOL> self . assertNotIn ( column , [ c . name for c in self . get_table_description ( table ) ] ) <EOL> def apply_operations ( self , app_label , project_state , operations ) : <EOL> migration = Migration ( '<STR_LIT:name>' , app_label ) <EOL> migration . operations = operations <EOL> with connection . schema_editor ( ) as editor : <EOL> return migration . apply ( project_state , editor ) <EOL> def set_up_test_model ( self , force_raster_creation = False ) : <EOL> test_fields = [ <EOL> ( '<STR_LIT:id>' , models . AutoField ( primary_key = True ) ) , <EOL> ( '<STR_LIT:name>' , models . CharField ( max_length = <NUM_LIT:100> , unique = True ) ) , <EOL> ( '<STR_LIT>' , fields . MultiPolygonField ( srid = <NUM_LIT> ) ) <EOL> ] <EOL> if connection . features . supports_raster or force_raster_creation : <EOL> test_fields += [ ( '<STR_LIT>' , fields . RasterField ( srid = <NUM_LIT> ) ) ] <EOL> operations = [ migrations . CreateModel ( '<STR_LIT>' , test_fields ) ] <EOL> return self . apply_operations ( '<STR_LIT>' , ProjectState ( ) , operations ) <EOL> def assertGeometryColumnsCount ( self , expected_count ) : <EOL> table_name = '<STR_LIT>' <EOL> if connection . features . uppercases_column_names : <EOL> table_name = table_name . upper ( ) <EOL> self . assertEqual ( <EOL> GeometryColumns . objects . filter ( ** { <EOL> GeometryColumns . table_name_col ( ) : table_name , <EOL> } ) . count ( ) , <EOL> expected_count <EOL> ) <EOL> def assertSpatialIndexExists ( self , table , column ) : <EOL> with connection . cursor ( ) as cursor : <EOL> indexes = connection . introspection . get_indexes ( cursor , table ) <EOL> self . assertIn ( column , indexes ) <EOL> def alter_gis_model ( self , migration_class , model_name , field_name , <EOL> blank = False , field_class = None ) : <EOL> project_state = self . set_up_test_model ( ) <EOL> self . current_state = project_state <EOL> args = [ model_name , field_name ] <EOL> if field_class : <EOL> args . append ( field_class ( srid = <NUM_LIT> , blank = blank ) ) <EOL> operation = migration_class ( * args ) <EOL> new_state = project_state . clone ( ) <EOL> operation . state_forwards ( '<STR_LIT>' , new_state ) <EOL> with connection . schema_editor ( ) as editor : <EOL> operation . database_forwards ( '<STR_LIT>' , editor , project_state , new_state ) <EOL> self . current_state = new_state <EOL> def test_add_geom_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . AddField , '<STR_LIT>' , '<STR_LIT:path>' , False , fields . LineStringField ) <EOL> self . assertColumnExists ( '<STR_LIT>' , '<STR_LIT:path>' ) <EOL> if HAS_GEOMETRY_COLUMNS : <EOL> self . assertGeometryColumnsCount ( <NUM_LIT:2> ) <EOL> if self . has_spatial_indexes : <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT:path>' ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_add_raster_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . AddField , '<STR_LIT>' , '<STR_LIT>' , False , fields . RasterField ) <EOL> self . assertColumnExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . has_spatial_indexes : <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ skipIfDBFeature ( '<STR_LIT>' ) <EOL> def test_create_raster_model_on_db_without_raster_support ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> with self . assertRaisesMessage ( ImproperlyConfigured , msg ) : <EOL> self . set_up_test_model ( True ) <EOL> @ skipIfDBFeature ( '<STR_LIT>' ) <EOL> def test_add_raster_field_on_db_without_raster_support ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> with self . assertRaisesMessage ( ImproperlyConfigured , msg ) : <EOL> self . alter_gis_model ( <EOL> migrations . AddField , '<STR_LIT>' , '<STR_LIT>' , <EOL> False , fields . RasterField <EOL> ) <EOL> def test_add_blank_geom_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . AddField , '<STR_LIT>' , '<STR_LIT:path>' , True , fields . LineStringField ) <EOL> self . assertColumnExists ( '<STR_LIT>' , '<STR_LIT:path>' ) <EOL> if HAS_GEOMETRY_COLUMNS : <EOL> self . assertGeometryColumnsCount ( <NUM_LIT:2> ) <EOL> if self . has_spatial_indexes : <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT:path>' ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_add_blank_raster_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . AddField , '<STR_LIT>' , '<STR_LIT>' , True , fields . RasterField ) <EOL> self . assertColumnExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . has_spatial_indexes : <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_remove_geom_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . RemoveField , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertColumnNotExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if HAS_GEOMETRY_COLUMNS : <EOL> self . assertGeometryColumnsCount ( <NUM_LIT:0> ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_remove_raster_field ( self ) : <EOL> """<STR_LIT>""" <EOL> self . alter_gis_model ( migrations . RemoveField , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertColumnNotExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_create_model_spatial_index ( self ) : <EOL> self . current_state = self . set_up_test_model ( ) <EOL> if not self . has_spatial_indexes : <EOL> self . skipTest ( '<STR_LIT>' ) <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if connection . features . supports_raster : <EOL> self . assertSpatialIndexExists ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def has_spatial_indexes ( self ) : <EOL> if mysql : <EOL> with connection . cursor ( ) as cursor : <EOL> return connection . introspection . supports_spatial_index ( cursor , '<STR_LIT>' ) <EOL> return True </s>
