Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modernized contrib.gis layermapping tests.

In particular, make tests independent of each other.
  • Loading branch information...
commit f1ebcdc7c2140eca632ed64296ec4e0893feb136 1 parent 817535d
@claudep claudep authored
Showing with 25 additions and 31 deletions.
  1. +25 −31 django/contrib/gis/tests/layermap/tests.py
View
56 django/contrib/gis/tests/layermap/tests.py
@@ -4,11 +4,11 @@
from copy import copy
from decimal import Decimal
-from django.utils.unittest import TestCase
-
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.tests.utils import mysql
-from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
+from django.contrib.gis.utils.layermapping import (LayerMapping, LayerMapError,
+ InvalidDecimal, MissingForeignKey)
+from django.test import TestCase
from .models import (
City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State,
@@ -28,7 +28,7 @@
class LayerMapTest(TestCase):
- def test01_init(self):
+ def test_init(self):
"Testing LayerMapping initialization."
# Model field that does not exist.
@@ -46,22 +46,14 @@ def test01_init(self):
# Incrementing through the bad mapping dictionaries and
# ensuring that a LayerMapError is raised.
for bad_map in (bad1, bad2, bad3):
- try:
+ with self.assertRaises(LayerMapError):
lm = LayerMapping(City, city_shp, bad_map)
- except LayerMapError:
- pass
- else:
- self.fail('Expected a LayerMapError.')
# A LookupError should be thrown for bogus encodings.
- try:
+ with self.assertRaises(LookupError):
lm = LayerMapping(City, city_shp, city_mapping, encoding='foobar')
- except LookupError:
- pass
- else:
- self.fail('Expected a LookupError')
- def test02_simple_layermap(self):
+ def test_simple_layermap(self):
"Test LayerMapping import of a simple point shapefile."
# Setting up for the LayerMapping.
lm = LayerMapping(City, city_shp, city_mapping)
@@ -85,18 +77,14 @@ def test02_simple_layermap(self):
self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
self.assertAlmostEqual(pnt1.y, pnt2.y, 5)
- def test03_layermap_strict(self):
+ def test_layermap_strict(self):
"Testing the `strict` keyword, and import of a LineString shapefile."
# When the `strict` keyword is set an error encountered will force
# the importation to stop.
- try:
+ with self.assertRaises(InvalidDecimal):
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
lm.save(silent=True, strict=True)
- except InvalidDecimal:
- # No transactions for geoms on MySQL; delete added features.
- if mysql: Interstate.objects.all().delete()
- else:
- self.fail('Should have failed on strict import with invalid decimal values.')
+ Interstate.objects.all().delete()
# This LayerMapping should work b/c `strict` is not set.
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
@@ -137,7 +125,7 @@ def county_helper(self, county_feat=True):
qs = CountyFeat.objects.filter(name=name)
self.assertEqual(n, qs.count())
- def test04_layermap_unique_multigeometry_fk(self):
+ def test_layermap_unique_multigeometry_fk(self):
"Testing the `unique`, and `transform`, geometry collection conversion, and ForeignKey mappings."
# All the following should work.
try:
@@ -176,8 +164,9 @@ def test04_layermap_unique_multigeometry_fk(self):
self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True)
# Now creating the state models so the ForeignKey mapping may work.
- co, hi, tx = State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
- co.save(), hi.save(), tx.save()
+ State.objects.bulk_create([
+ State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
+ ])
# If a mapping is specified as a collection, all OGR fields that
# are not collections will be converted into them. For example,
@@ -203,16 +192,19 @@ def test04_layermap_unique_multigeometry_fk(self):
# The county helper is called to ensure integrity of County models.
self.county_helper()
- def test05_test_fid_range_step(self):
+ def test_test_fid_range_step(self):

doubletest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"Tests the `fid_range` keyword and the `step` keyword of .save()."
# Function for clearing out all the counties before testing.
def clear_counties(): County.objects.all().delete()
+ State.objects.bulk_create([
+ State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
+ ])
+
# Initializing the LayerMapping object to use in these tests.
lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
# Bad feature id ranges should raise a type error.
- clear_counties()
bad_ranges = (5.0, 'foo', co_shp)
for bad in bad_ranges:
self.assertRaises(TypeError, lm.save, fid_range=bad)
@@ -241,8 +233,10 @@ def clear_counties(): County.objects.all().delete()
self.assertEqual(2, qs.count())
hi, co = tuple(qs)
hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
- self.assertEqual('Pueblo', co.name); self.assertEqual(NUMS[co_idx], len(co.mpoly))
- self.assertEqual('Honolulu', hi.name); self.assertEqual(NUMS[hi_idx], len(hi.mpoly))
+ self.assertEqual('Pueblo', co.name)
+ self.assertEqual(NUMS[co_idx], len(co.mpoly))
+ self.assertEqual('Honolulu', hi.name)
+ self.assertEqual(NUMS[hi_idx], len(hi.mpoly))
# Testing the `step` keyword -- should get the same counties
# regardless of we use a step that divides equally, that is odd,
@@ -252,7 +246,7 @@ def clear_counties(): County.objects.all().delete()
lm.save(step=st, strict=True)
self.county_helper(county_feat=False)
- def test06_model_inheritance(self):
+ def test_model_inheritance(self):
"Tests LayerMapping on inherited models. See #12093."
icity_mapping = {'name' : 'Name',
'population' : 'Population',
@@ -272,7 +266,7 @@ def test06_model_inheritance(self):
self.assertEqual(6, ICity1.objects.count())
self.assertEqual(3, ICity2.objects.count())
- def test07_invalid_layer(self):
+ def test_invalid_layer(self):
"Tests LayerMapping on invalid geometries. See #15378."
invalid_mapping = {'point': 'POINT'}
lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
Please sign in to comment.
Something went wrong with that request. Please try again.