Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9664 -- `LayerMapping` now works with MySQL spatial ba…

…ckends.

Backport of r9688 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9689 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d2c9c1261a816c73b6622885d1caf770ec650180 1 parent 04ce539
Justin Bronn authored December 26, 2008
2  django/contrib/gis/tests/__init__.py
@@ -29,7 +29,7 @@ def geo_suite():
29 29
         elif postgis:
30 30
             test_models += ['distapp', 'layermap', 'relatedapp']
31 31
         elif mysql:
32  
-            test_models += ['relatedapp']
  32
+            test_models += ['relatedapp', 'layermap']
33 33
 
34 34
         test_suite_names += [
35 35
             'test_gdal_driver',
13  django/contrib/gis/tests/layermap/tests.py
... ...
@@ -1,8 +1,8 @@
1 1
 import os, unittest
2 2
 from copy import copy
3  
-from datetime import date
4 3
 from decimal import Decimal
5 4
 from models import City, County, CountyFeat, Interstate, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
  5
+from django.contrib.gis.db.backend import SpatialBackend
6 6
 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
7 7
 from django.contrib.gis.gdal import DataSource
8 8
 
@@ -85,7 +85,8 @@ def test03_layermap_strict(self):
85 85
             lm = LayerMapping(Interstate, inter_shp, inter_mapping)
86 86
             lm.save(silent=True, strict=True)
87 87
         except InvalidDecimal:
88  
-            pass
  88
+            # No transactions for geoms on MySQL; delete added features.
  89
+            if SpatialBackend.mysql: Interstate.objects.all().delete()
89 90
         else:
90 91
             self.fail('Should have failed on strict import with invalid decimal values.')
91 92
 
@@ -151,7 +152,8 @@ def test04_layermap_unique_multigeometry_fk(self):
151 152
             self.assertRaises(e, LayerMapping, County, co_shp, co_mapping, transform=False, unique=arg)
152 153
 
153 154
         # No source reference system defined in the shapefile, should raise an error.
154  
-        self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
  155
+        if not SpatialBackend.mysql:
  156
+            self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
155 157
 
156 158
         # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
157 159
         # mapping for the model the ForeignKey points to.
@@ -227,8 +229,9 @@ def clear_counties(): County.objects.all().delete()
227 229
         lm.save(fid_range=slice(None, 1), silent=True, strict=True) # layer[:1]
228 230
 
229 231
         # Only Pueblo & Honolulu counties should be present because of
230  
-        # the `unique` keyword.
231  
-        qs = County.objects.all()
  232
+        # the `unique` keyword.  Have to set `order_by` on this QuerySet 
  233
+        # or else MySQL will return a different ordering than the other dbs.
  234
+        qs = County.objects.order_by('name') 
232 235
         self.assertEqual(2, qs.count())
233 236
         hi, co = tuple(qs)
234 237
         hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
1  django/contrib/gis/tests/layermap/tests_mysql.py
... ...
@@ -0,0 +1 @@
  1
+from tests import *
11  django/contrib/gis/utils/layermapping.py
@@ -116,7 +116,6 @@ def __str__(self):
116 116
     OGRException, OGRGeometry, OGRGeomType, SpatialReference
117 117
 from django.contrib.gis.gdal.field import \
118 118
     OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime
119  
-from django.contrib.gis.models import GeometryColumns, SpatialRefSys
120 119
 from django.db import models, transaction
121 120
 from django.contrib.localflavor.us.models import USStateField
122 121
 
@@ -189,7 +188,10 @@ def __init__(self, model, data, mapping, layer=0,
189 188
 
190 189
         # Getting the geometry column associated with the model (an 
191 190
         # exception will be raised if there is no geometry column).
192  
-        self.geo_col = self.geometry_column()
  191
+        if SpatialBackend.mysql:
  192
+            transform = False
  193
+        else:
  194
+            self.geo_col = self.geometry_column()
193 195
 
194 196
         # Checking the source spatial reference system, and getting
195 197
         # the coordinate transformation object (unless the `transform`
@@ -327,6 +329,7 @@ def check_ogr_fld(ogr_map_fld):
327 329
 
328 330
     def check_srs(self, source_srs):
329 331
         "Checks the compatibility of the given spatial reference object."
  332
+        from django.contrib.gis.models import SpatialRefSys
330 333
         if isinstance(source_srs, SpatialReference):
331 334
             sr = source_srs
332 335
         elif isinstance(source_srs, SpatialRefSys):
@@ -498,6 +501,7 @@ def verify_geom(self, geom, model_field):
498 501
     #### Other model methods ####
499 502
     def coord_transform(self):
500 503
         "Returns the coordinate transformation object."
  504
+        from django.contrib.gis.models import SpatialRefSys
501 505
         try:
502 506
             # Getting the target spatial reference system
503 507
             target_srs = SpatialRefSys.objects.get(srid=self.geo_col.srid).srs
@@ -509,11 +513,12 @@ def coord_transform(self):
509 513
 
510 514
     def geometry_column(self):
511 515
         "Returns the GeometryColumn model associated with the geographic column."
  516
+        from django.contrib.gis.models import GeometryColumns
512 517
         # Getting the GeometryColumn object.
513 518
         try:
514 519
             db_table = self.model._meta.db_table
515 520
             geo_col = self.geom_field
516  
-            if SpatialBackend.name == 'oracle':
  521
+            if SpatialBackend.oracle:
517 522
                 # Making upper case for Oracle.
518 523
                 db_table = db_table.upper()
519 524
                 geo_col = geo_col.upper()

0 notes on commit d2c9c12

Please sign in to comment.
Something went wrong with that request. Please try again.