web/lib/django/contrib/gis/tests/layermap/tests.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
     1 import os, unittest
     1 import os
     2 from copy import copy
     2 import unittest
     3 from decimal import Decimal
     3 from decimal import Decimal
     4 from models import City, County, CountyFeat, Interstate, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
     4 
     5 from django.contrib.gis.db.backend import SpatialBackend
     5 from django.utils.copycompat import copy
       
     6 
       
     7 from django.contrib.gis.gdal import DataSource
       
     8 from django.contrib.gis.tests.utils import mysql
     6 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
     9 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
     7 from django.contrib.gis.gdal import DataSource
    10 
     8 
    11 from models import City, County, CountyFeat, Interstate, ICity1, ICity2, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
     9 shp_path = os.path.dirname(__file__)
    12 
    10 city_shp = os.path.join(shp_path, '../data/cities/cities.shp')
    13 shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data'))
    11 co_shp = os.path.join(shp_path, '../data/counties/counties.shp')
    14 city_shp = os.path.join(shp_path, 'cities', 'cities.shp')
    12 inter_shp = os.path.join(shp_path, '../data/interstates/interstates.shp')
    15 co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
       
    16 inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp')
    13 
    17 
    14 # Dictionaries to hold what's expected in the county shapefile.  
    18 # Dictionaries to hold what's expected in the county shapefile.  
    15 NAMES  = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
    19 NAMES  = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
    16 NUMS   = [1, 2, 1, 19, 1] # Number of polygons for each.                                                                                                                                                  
    20 NUMS   = [1, 2, 1, 19, 1] # Number of polygons for each.                                                                                                                                                  
    17 STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
    21 STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
    82         try:
    86         try:
    83             lm = LayerMapping(Interstate, inter_shp, inter_mapping)
    87             lm = LayerMapping(Interstate, inter_shp, inter_mapping)
    84             lm.save(silent=True, strict=True)
    88             lm.save(silent=True, strict=True)
    85         except InvalidDecimal:
    89         except InvalidDecimal:
    86             # No transactions for geoms on MySQL; delete added features.
    90             # No transactions for geoms on MySQL; delete added features.
    87             if SpatialBackend.mysql: Interstate.objects.all().delete()
    91             if mysql: Interstate.objects.all().delete()
    88         else:
    92         else:
    89             self.fail('Should have failed on strict import with invalid decimal values.')
    93             self.fail('Should have failed on strict import with invalid decimal values.')
    90 
    94 
    91         # This LayerMapping should work b/c `strict` is not set.
    95         # This LayerMapping should work b/c `strict` is not set.
    92         lm = LayerMapping(Interstate, inter_shp, inter_mapping)
    96         lm = LayerMapping(Interstate, inter_shp, inter_mapping)
   147         # Testing invalid params for the `unique` keyword.
   151         # Testing invalid params for the `unique` keyword.
   148         for e, arg in ((TypeError, 5.0), (ValueError, 'foobar'), (ValueError, ('name', 'mpolygon'))):
   152         for e, arg in ((TypeError, 5.0), (ValueError, 'foobar'), (ValueError, ('name', 'mpolygon'))):
   149             self.assertRaises(e, LayerMapping, County, co_shp, co_mapping, transform=False, unique=arg)
   153             self.assertRaises(e, LayerMapping, County, co_shp, co_mapping, transform=False, unique=arg)
   150 
   154 
   151         # No source reference system defined in the shapefile, should raise an error.
   155         # No source reference system defined in the shapefile, should raise an error.
   152         if not SpatialBackend.mysql:
   156         if not mysql:
   153             self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
   157             self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
   154 
   158 
   155         # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
   159         # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
   156         # mapping for the model the ForeignKey points to.
   160         # mapping for the model the ForeignKey points to.
   157         bad_fk_map1 = copy(co_mapping); bad_fk_map1['state'] = 'name'
   161         bad_fk_map1 = copy(co_mapping); bad_fk_map1['state'] = 'name'
   240         for st in (4,7,1000):
   244         for st in (4,7,1000):
   241             clear_counties()
   245             clear_counties()
   242             lm.save(step=st, strict=True)
   246             lm.save(step=st, strict=True)
   243             self.county_helper(county_feat=False)
   247             self.county_helper(county_feat=False)
   244 
   248 
       
   249     def test06_model_inheritance(self):
       
   250         "Tests LayerMapping on inherited models.  See #12093."
       
   251         icity_mapping = {'name' : 'Name',
       
   252                          'population' : 'Population',
       
   253                          'density' : 'Density',
       
   254                          'point' : 'POINT',
       
   255                          'dt' : 'Created',
       
   256                          }
       
   257 
       
   258         # Parent model has geometry field.
       
   259         lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
       
   260         lm1.save()
       
   261 
       
   262         # Grandparent has geometry field.
       
   263         lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
       
   264         lm2.save()
       
   265 
       
   266         self.assertEqual(6, ICity1.objects.count())
       
   267         self.assertEqual(3, ICity2.objects.count())
       
   268         
   245 def suite():
   269 def suite():
   246     s = unittest.TestSuite()
   270     s = unittest.TestSuite()
   247     s.addTest(unittest.makeSuite(LayerMapTest))
   271     s.addTest(unittest.makeSuite(LayerMapTest))
   248     return s
   272     return s