mirror of https://github.com/django/django.git
Modernized contrib.gis layermapping tests.
In particular, make tests independent of each other.
This commit is contained in:
parent
817535d73e
commit
f1ebcdc7c2
|
@ -4,11 +4,11 @@ import os
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.utils.unittest import TestCase
|
|
||||||
|
|
||||||
from django.contrib.gis.gdal import DataSource
|
from django.contrib.gis.gdal import DataSource
|
||||||
from django.contrib.gis.tests.utils import mysql
|
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 (
|
from .models import (
|
||||||
City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State,
|
City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State,
|
||||||
|
@ -28,7 +28,7 @@ STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
|
||||||
|
|
||||||
class LayerMapTest(TestCase):
|
class LayerMapTest(TestCase):
|
||||||
|
|
||||||
def test01_init(self):
|
def test_init(self):
|
||||||
"Testing LayerMapping initialization."
|
"Testing LayerMapping initialization."
|
||||||
|
|
||||||
# Model field that does not exist.
|
# Model field that does not exist.
|
||||||
|
@ -46,22 +46,14 @@ class LayerMapTest(TestCase):
|
||||||
# Incrementing through the bad mapping dictionaries and
|
# Incrementing through the bad mapping dictionaries and
|
||||||
# ensuring that a LayerMapError is raised.
|
# ensuring that a LayerMapError is raised.
|
||||||
for bad_map in (bad1, bad2, bad3):
|
for bad_map in (bad1, bad2, bad3):
|
||||||
try:
|
with self.assertRaises(LayerMapError):
|
||||||
lm = LayerMapping(City, city_shp, bad_map)
|
lm = LayerMapping(City, city_shp, bad_map)
|
||||||
except LayerMapError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
self.fail('Expected a LayerMapError.')
|
|
||||||
|
|
||||||
# A LookupError should be thrown for bogus encodings.
|
# A LookupError should be thrown for bogus encodings.
|
||||||
try:
|
with self.assertRaises(LookupError):
|
||||||
lm = LayerMapping(City, city_shp, city_mapping, encoding='foobar')
|
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."
|
"Test LayerMapping import of a simple point shapefile."
|
||||||
# Setting up for the LayerMapping.
|
# Setting up for the LayerMapping.
|
||||||
lm = LayerMapping(City, city_shp, city_mapping)
|
lm = LayerMapping(City, city_shp, city_mapping)
|
||||||
|
@ -85,18 +77,14 @@ class LayerMapTest(TestCase):
|
||||||
self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
|
self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
|
||||||
self.assertAlmostEqual(pnt1.y, pnt2.y, 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."
|
"Testing the `strict` keyword, and import of a LineString shapefile."
|
||||||
# When the `strict` keyword is set an error encountered will force
|
# When the `strict` keyword is set an error encountered will force
|
||||||
# the importation to stop.
|
# the importation to stop.
|
||||||
try:
|
with self.assertRaises(InvalidDecimal):
|
||||||
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
|
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
|
||||||
lm.save(silent=True, strict=True)
|
lm.save(silent=True, strict=True)
|
||||||
except InvalidDecimal:
|
Interstate.objects.all().delete()
|
||||||
# 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.')
|
|
||||||
|
|
||||||
# This LayerMapping should work b/c `strict` is not set.
|
# This LayerMapping should work b/c `strict` is not set.
|
||||||
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
|
lm = LayerMapping(Interstate, inter_shp, inter_mapping)
|
||||||
|
@ -137,7 +125,7 @@ class LayerMapTest(TestCase):
|
||||||
qs = CountyFeat.objects.filter(name=name)
|
qs = CountyFeat.objects.filter(name=name)
|
||||||
self.assertEqual(n, qs.count())
|
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."
|
"Testing the `unique`, and `transform`, geometry collection conversion, and ForeignKey mappings."
|
||||||
# All the following should work.
|
# All the following should work.
|
||||||
try:
|
try:
|
||||||
|
@ -176,8 +164,9 @@ class LayerMapTest(TestCase):
|
||||||
self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True)
|
self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True)
|
||||||
|
|
||||||
# Now creating the state models so the ForeignKey mapping may work.
|
# Now creating the state models so the ForeignKey mapping may work.
|
||||||
co, hi, tx = State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
|
State.objects.bulk_create([
|
||||||
co.save(), hi.save(), tx.save()
|
State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
|
||||||
|
])
|
||||||
|
|
||||||
# If a mapping is specified as a collection, all OGR fields that
|
# If a mapping is specified as a collection, all OGR fields that
|
||||||
# are not collections will be converted into them. For example,
|
# are not collections will be converted into them. For example,
|
||||||
|
@ -203,16 +192,19 @@ class LayerMapTest(TestCase):
|
||||||
# The county helper is called to ensure integrity of County models.
|
# The county helper is called to ensure integrity of County models.
|
||||||
self.county_helper()
|
self.county_helper()
|
||||||
|
|
||||||
def test05_test_fid_range_step(self):
|
def test_test_fid_range_step(self):
|
||||||
"Tests the `fid_range` keyword and the `step` keyword of .save()."
|
"Tests the `fid_range` keyword and the `step` keyword of .save()."
|
||||||
# Function for clearing out all the counties before testing.
|
# Function for clearing out all the counties before testing.
|
||||||
def clear_counties(): County.objects.all().delete()
|
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.
|
# Initializing the LayerMapping object to use in these tests.
|
||||||
lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
|
lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
|
||||||
|
|
||||||
# Bad feature id ranges should raise a type error.
|
# Bad feature id ranges should raise a type error.
|
||||||
clear_counties()
|
|
||||||
bad_ranges = (5.0, 'foo', co_shp)
|
bad_ranges = (5.0, 'foo', co_shp)
|
||||||
for bad in bad_ranges:
|
for bad in bad_ranges:
|
||||||
self.assertRaises(TypeError, lm.save, fid_range=bad)
|
self.assertRaises(TypeError, lm.save, fid_range=bad)
|
||||||
|
@ -241,8 +233,10 @@ class LayerMapTest(TestCase):
|
||||||
self.assertEqual(2, qs.count())
|
self.assertEqual(2, qs.count())
|
||||||
hi, co = tuple(qs)
|
hi, co = tuple(qs)
|
||||||
hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
|
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('Pueblo', co.name)
|
||||||
self.assertEqual('Honolulu', hi.name); self.assertEqual(NUMS[hi_idx], len(hi.mpoly))
|
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
|
# Testing the `step` keyword -- should get the same counties
|
||||||
# regardless of we use a step that divides equally, that is odd,
|
# regardless of we use a step that divides equally, that is odd,
|
||||||
|
@ -252,7 +246,7 @@ class LayerMapTest(TestCase):
|
||||||
lm.save(step=st, strict=True)
|
lm.save(step=st, strict=True)
|
||||||
self.county_helper(county_feat=False)
|
self.county_helper(county_feat=False)
|
||||||
|
|
||||||
def test06_model_inheritance(self):
|
def test_model_inheritance(self):
|
||||||
"Tests LayerMapping on inherited models. See #12093."
|
"Tests LayerMapping on inherited models. See #12093."
|
||||||
icity_mapping = {'name' : 'Name',
|
icity_mapping = {'name' : 'Name',
|
||||||
'population' : 'Population',
|
'population' : 'Population',
|
||||||
|
@ -272,7 +266,7 @@ class LayerMapTest(TestCase):
|
||||||
self.assertEqual(6, ICity1.objects.count())
|
self.assertEqual(6, ICity1.objects.count())
|
||||||
self.assertEqual(3, ICity2.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."
|
"Tests LayerMapping on invalid geometries. See #15378."
|
||||||
invalid_mapping = {'point': 'POINT'}
|
invalid_mapping = {'point': 'POINT'}
|
||||||
lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
|
lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
|
||||||
|
|
Loading…
Reference in New Issue