Merged in patch from Aryeh Leib Taurog for #9877, adapting as necessary.
2 from pymutable_geometries import *
3 from django.contrib.gis.geos.error import GEOSIndexError
5 class GEOSPyMutableTest(unittest.TestCase):
7 Tests Pythonic Mutability of Python GEOS geometry wrappers
8 get/set/delitem on a slice, normal list methods
11 def test01_getitem(self):
12 'Test getting a single item from a geometry'
13 for g in slice_geometries():
15 self.assertEqual(g.coords[i], getcoords(g.geom[i]))
17 def test02_getslice(self):
18 'Test getting a slice from a geometry'
19 for f in getslice_functions():
20 for g in slice_geometries():
21 msg = "fcn %s, geom %s" % (f.__name__, str(g.geom_type))
22 self.assertEqual(f(g.coords), getcoords(f(g.geom)), msg)
24 def test03_getitem_indexException(self):
25 'Test get single item with out-of-bounds index'
26 for g in slice_geometries():
27 for i in SEQ_OUT_OF_BOUNDS:
28 self.assertRaises(GEOSIndexError, lambda: g.geom[i])
30 def test04_delitem_single(self):
31 'Test delete single item from a geometry'
33 for g in slice_geometries():
34 if g.geom.ring and i in SEQ_BOUNDS: continue
37 self.assertEqual(g.tuple_coords, g.geom.coords)
39 def test05_delitem_slice(self):
40 'Test delete slice from a geometry'
41 for f in delslice_functions():
42 for g in slice_geometries():
43 if g.geom.ring and not f.ring: continue
46 msg = "fcn %s, geom %s" % (f.__name__, str(g.geom_type))
47 self.assertEqual(g.tuple_coords, g.geom.coords, msg)
49 def test06_delitem_single_indexException(self):
50 'Test delete single item with out-of-bounds index'
51 def func(x, i): del x[i]
52 for g in slice_geometries():
53 for i in SEQ_OUT_OF_BOUNDS:
54 self.assertRaises(GEOSIndexError, func, g.geom, i)
56 def test07_setitem_single(self):
57 "Test set single item (make sure we didn't break this)"
59 for g in slice_geometries():
60 if g.geom.ring and i in SEQ_BOUNDS: continue
61 ag, ac = g.newitem(rng=(400,410))
64 self.assertEqual(g.tuple_coords, g.geom.coords)
66 def test08_setslice_simple(self):
67 'Test setting a simple slice of a geometry'
68 for g in slice_geometries():
69 for f in setslice_simple_functions(g):
70 if g.geom.ring and not f.ring: continue
73 msg = "fcn %s, geom %s" % (f.__name__, str(g.geom_type))
74 self.assertEqual(g.tuple_coords, g.geom.coords, msg)
76 def test09_setslice_extended(self):
77 'Test setting an extended slice of a geometry'
78 for g in slice_geometries():
79 for f in setslice_extended_functions(g):
80 if g.geom.ring and not f.ring: continue
83 msg = "fcn %s, geom %s" % (f.__name__, str(g.geom_type))
84 self.assertEqual(g.tuple_coords, g.geom.coords, msg)
86 def test10_setslice_extended_mismatched(self):
87 'Test setting extended slice with array of mismatched length'
88 for g in slice_geometries():
89 ag, ac = g.newitem(rng=(400,410))
90 def func(): g.geom[2:8:2] = [ ag, ]
91 self.assertRaises(ValueError, func)
93 def test11_setitem_single_indexException(self):
94 'Test set single item with out-of-bounds index'
95 for g in slice_geometries():
96 ag, ac = g.newitem(rng=(400,410))
97 def func(i): g.geom[i] = ag
98 for i in SEQ_OUT_OF_BOUNDS:
99 self.assertRaises(GEOSIndexError, func, i)
101 def test12_append(self):
102 'Test list method append'
103 for g in slice_geometries(ring=False):
104 ag, ac = g.newitem(rng=(400,410))
107 self.assertEqual(g.tuple_coords, g.geom.coords)
109 def test13_extend(self):
110 'Test list method extend'
111 for g in slice_geometries():
112 items = g.coords_fcn(5)
113 if g.geom.ring: items[-1] = g.coords[0]
114 g.geom.extend(map(g.subtype,items))
115 g.coords.extend(items)
116 self.assertEqual(g.tuple_coords, g.geom.coords)
118 def test14_insert(self):
119 'Test list method insert'
120 for i in xrange(*SEQ_OUT_OF_BOUNDS):
121 for g in slice_geometries():
122 if g.geom.ring and i in SEQ_BOUNDS + SEQ_OUT_OF_BOUNDS:
124 ag, ac = g.newitem(rng=(200,250))
126 g.coords.insert(i, ac)
127 self.assertEqual(g.tuple_coords, g.geom.coords)
129 def test15_insert_typeError(self):
130 'Test list method insert raises error on invalid index'
131 for g in slice_geometries():
132 ag, ac = g.newitem(rng=(200,250))
133 self.assertRaises(TypeError, g.geom.insert, 'hi', ag)
135 def test16_pop(self):
136 'Test list method pop'
138 for g in slice_geometries():
139 if g.geom.ring and i in SEQ_BOUNDS + SEQ_OUT_OF_BOUNDS:
141 self.assertEqual(g.coords.pop(i), getcoords(g.geom.pop(i)))
143 def test16_index(self):
144 'Test list method index'
145 for i in xrange(0, SEQ_LENGTH):
146 for g in slice_geometries():
147 if g.geom.ring and i in SEQ_BOUNDS: continue
148 ag, ac = g.newitem(rng=(400,410))
150 self.assertEqual(i, g.geom.index(ag))
152 def test17_index_ValueError(self):
153 'Test list method raises ValueError if value not found'
154 for g in slice_geometries():
155 ag, ac = g.newitem(rng=(400,410))
156 self.assertRaises(ValueError, g.geom.index, ag)
158 def test18_remove(self):
159 'Test list method remove'
160 for i in xrange(0, SEQ_LENGTH):
161 for g in slice_geometries():
162 if g.geom.ring and i in SEQ_BOUNDS: continue
163 ag, ac = g.newitem(rng=(400,410))
168 self.assertEqual(g.tuple_coords, g.geom.coords)
170 def test19_count(self):
171 'Test list method count'
172 for g in slice_geometries():
173 self.assertEqual(SEQ_LENGTH, g.geom.count())
175 def test20_setslice_geos_fcns(self):
176 'Test geos properties after setting a simple slice of a geometry'
177 for g in slice_geometries(ring=False):
178 for f in setslice_simple_functions(g):
181 if not len(g.coords): continue
182 for tf in test_geos_functions():
184 self.assertEqual(tf(cg) , tf(g.geom))
186 def test21_delslice_geos_fcns(self):
187 'Test geos properties after deleting a slice of a geometry'
188 for f in delslice_functions():
189 for g in slice_geometries(ring=False):
192 if not len(g.coords): continue
193 for tf in test_geos_functions():
195 self.assertEqual(tf(cg) , tf(g.geom))
198 s = unittest.TestSuite()
199 s.addTest(unittest.makeSuite(GEOSPyMutableTest))
202 def run(verbosity=2):
203 unittest.TextTestRunner(verbosity=verbosity).run(suite())
205 if __name__ == '__main__':