PageRenderTime 167ms CodeModel.GetById 53ms app.highlight 73ms RepoModel.GetById 22ms app.codeStats 1ms

/django/contrib/gis/tests/test_measure.py

https://code.google.com/p/mango-py/
Python | 337 lines | 270 code | 63 blank | 4 comment | 9 complexity | 497f6cf85029898ac59f61eed39ebac8 MD5 | raw file
  1"""
  2Distance and Area objects to allow for sensible and convienient calculation
  3and conversions. Here are some tests.
  4"""
  5
  6from django.contrib.gis.measure import Distance, Area, D, A
  7from django.utils import unittest
  8
  9
 10class DistanceTest(unittest.TestCase):
 11    "Testing the Distance object"
 12
 13    def testInit(self):
 14        "Testing initialisation from valid units"
 15        d = Distance(m=100)
 16        self.assertEqual(d.m, 100)
 17
 18        d1, d2, d3 = D(m=100), D(meter=100), D(metre=100)
 19        for d in (d1, d2, d3):
 20            self.assertEqual(d.m, 100)
 21
 22        d = D(nm=100)
 23        self.assertEqual(d.m, 185200)
 24
 25        y1, y2, y3 = D(yd=100), D(yard=100), D(Yard=100)
 26        for d in (y1, y2, y3):
 27            self.assertEqual(d.yd, 100)
 28
 29        mm1, mm2 = D(millimeter=1000), D(MiLLiMeTeR=1000)
 30        for d in (mm1, mm2):
 31            self.assertEqual(d.m, 1.0)
 32            self.assertEqual(d.mm, 1000.0)
 33
 34
 35    def testInitInvalid(self):
 36        "Testing initialisation from invalid units"
 37        self.assertRaises(AttributeError, D, banana=100)
 38
 39    def testAccess(self):
 40        "Testing access in different units"
 41        d = D(m=100)
 42        self.assertEqual(d.km, 0.1)
 43        self.assertAlmostEqual(d.ft, 328.084, 3)
 44
 45    def testAccessInvalid(self):
 46        "Testing access in invalid units"
 47        d = D(m=100)
 48        self.failIf(hasattr(d, 'banana'))
 49
 50    def testAddition(self):
 51        "Test addition & subtraction"
 52        d1 = D(m=100)
 53        d2 = D(m=200)
 54
 55        d3 = d1 + d2
 56        self.assertEqual(d3.m, 300)
 57        d3 += d1
 58        self.assertEqual(d3.m, 400)
 59
 60        d4 = d1 - d2
 61        self.assertEqual(d4.m, -100)
 62        d4 -= d1
 63        self.assertEqual(d4.m, -200)
 64
 65        try:
 66            d5 = d1 + 1
 67        except TypeError, e:
 68            pass
 69        else:
 70            self.fail('Distance + number should raise TypeError')
 71
 72        try:
 73            d5 = d1 - 1
 74        except TypeError, e:
 75            pass
 76        else:
 77            self.fail('Distance - number should raise TypeError')
 78
 79        try:
 80            d1 += 1
 81        except TypeError, e:
 82            pass
 83        else:
 84            self.fail('Distance += number should raise TypeError')
 85
 86        try:
 87            d1 -= 1
 88        except TypeError, e:
 89            pass
 90        else:
 91            self.fail('Distance -= number should raise TypeError')
 92
 93    def testMultiplication(self):
 94        "Test multiplication & division"
 95        d1 = D(m=100)
 96
 97        d3 = d1 * 2
 98        self.assertEqual(d3.m, 200)
 99        d3 = 2 * d1
100        self.assertEqual(d3.m, 200)
101        d3 *= 5
102        self.assertEqual(d3.m, 1000)
103
104        d4 = d1 / 2
105        self.assertEqual(d4.m, 50)
106        d4 /= 5
107        self.assertEqual(d4.m, 10)
108
109        a5 = d1 * D(m=10)
110        self.assertTrue(isinstance(a5, Area))
111        self.assertEqual(a5.sq_m, 100*10)
112
113        try:
114            d1 *= D(m=1)
115        except TypeError, e:
116            pass
117        else:
118            self.fail('Distance *= Distance should raise TypeError')
119
120        try:
121            d5 = d1 / D(m=1)
122        except TypeError, e:
123            pass
124        else:
125            self.fail('Distance / Distance should raise TypeError')
126
127        try:
128            d1 /= D(m=1)
129        except TypeError, e:
130            pass
131        else:
132            self.fail('Distance /= Distance should raise TypeError')
133
134    def testUnitConversions(self):
135        "Testing default units during maths"
136        d1 = D(m=100)
137        d2 = D(km=1)
138
139        d3 = d1 + d2
140        self.assertEqual(d3._default_unit, 'm')
141        d4 = d2 + d1
142        self.assertEqual(d4._default_unit, 'km')
143        d5 = d1 * 2
144        self.assertEqual(d5._default_unit, 'm')
145        d6 = d1 / 2
146        self.assertEqual(d6._default_unit, 'm')
147
148    def testComparisons(self):
149        "Testing comparisons"
150        d1 = D(m=100)
151        d2 = D(km=1)
152        d3 = D(km=0)
153
154        self.assertTrue(d2 > d1)
155        self.assertTrue(d1 == d1)
156        self.assertTrue(d1 < d2)
157        self.failIf(d3)
158
159    def testUnitsStr(self):
160        "Testing conversion to strings"
161        d1 = D(m=100)
162        d2 = D(km=3.5)
163
164        self.assertEqual(str(d1), '100.0 m')
165        self.assertEqual(str(d2), '3.5 km')
166        self.assertEqual(repr(d1), 'Distance(m=100.0)')
167        self.assertEqual(repr(d2), 'Distance(km=3.5)')
168
169    def testUnitAttName(self):
170        "Testing the `unit_attname` class method"
171        unit_tuple = [('Yard', 'yd'), ('Nautical Mile', 'nm'), ('German legal metre', 'german_m'),
172                      ('Indian yard', 'indian_yd'), ('Chain (Sears)', 'chain_sears'), ('Chain', 'chain')]
173        for nm, att in unit_tuple:
174            self.assertEqual(att, D.unit_attname(nm))
175
176class AreaTest(unittest.TestCase):
177    "Testing the Area object"
178
179    def testInit(self):
180        "Testing initialisation from valid units"
181        a = Area(sq_m=100)
182        self.assertEqual(a.sq_m, 100)
183
184        a = A(sq_m=100)
185        self.assertEqual(a.sq_m, 100)
186
187        a = A(sq_mi=100)
188        self.assertEqual(a.sq_m, 258998811.0336)
189
190    def testInitInvaliA(self):
191        "Testing initialisation from invalid units"
192        self.assertRaises(AttributeError, A, banana=100)
193
194    def testAccess(self):
195        "Testing access in different units"
196        a = A(sq_m=100)
197        self.assertEqual(a.sq_km, 0.0001)
198        self.assertAlmostEqual(a.sq_ft, 1076.391, 3)
199
200    def testAccessInvaliA(self):
201        "Testing access in invalid units"
202        a = A(sq_m=100)
203        self.failIf(hasattr(a, 'banana'))
204
205    def testAddition(self):
206        "Test addition & subtraction"
207        a1 = A(sq_m=100)
208        a2 = A(sq_m=200)
209
210        a3 = a1 + a2
211        self.assertEqual(a3.sq_m, 300)
212        a3 += a1
213        self.assertEqual(a3.sq_m, 400)
214
215        a4 = a1 - a2
216        self.assertEqual(a4.sq_m, -100)
217        a4 -= a1
218        self.assertEqual(a4.sq_m, -200)
219
220        try:
221            a5 = a1 + 1
222        except TypeError, e:
223            pass
224        else:
225            self.fail('Area + number should raise TypeError')
226
227        try:
228            a5 = a1 - 1
229        except TypeError, e:
230            pass
231        else:
232            self.fail('Area - number should raise TypeError')
233
234        try:
235            a1 += 1
236        except TypeError, e:
237            pass
238        else:
239            self.fail('Area += number should raise TypeError')
240
241        try:
242            a1 -= 1
243        except TypeError, e:
244            pass
245        else:
246            self.fail('Area -= number should raise TypeError')
247
248    def testMultiplication(self):
249        "Test multiplication & division"
250        a1 = A(sq_m=100)
251
252        a3 = a1 * 2
253        self.assertEqual(a3.sq_m, 200)
254        a3 = 2 * a1
255        self.assertEqual(a3.sq_m, 200)
256        a3 *= 5
257        self.assertEqual(a3.sq_m, 1000)
258
259        a4 = a1 / 2
260        self.assertEqual(a4.sq_m, 50)
261        a4 /= 5
262        self.assertEqual(a4.sq_m, 10)
263
264        try:
265            a5 = a1 * A(sq_m=1)
266        except TypeError, e:
267            pass
268        else:
269            self.fail('Area * Area should raise TypeError')
270
271        try:
272            a1 *= A(sq_m=1)
273        except TypeError, e:
274            pass
275        else:
276            self.fail('Area *= Area should raise TypeError')
277
278        try:
279            a5 = a1 / A(sq_m=1)
280        except TypeError, e:
281            pass
282        else:
283            self.fail('Area / Area should raise TypeError')
284
285        try:
286            a1 /= A(sq_m=1)
287        except TypeError, e:
288            pass
289        else:
290            self.fail('Area /= Area should raise TypeError')
291
292    def testUnitConversions(self):
293        "Testing default units during maths"
294        a1 = A(sq_m=100)
295        a2 = A(sq_km=1)
296
297        a3 = a1 + a2
298        self.assertEqual(a3._default_unit, 'sq_m')
299        a4 = a2 + a1
300        self.assertEqual(a4._default_unit, 'sq_km')
301        a5 = a1 * 2
302        self.assertEqual(a5._default_unit, 'sq_m')
303        a6 = a1 / 2
304        self.assertEqual(a6._default_unit, 'sq_m')
305
306    def testComparisons(self):
307        "Testing comparisons"
308        a1 = A(sq_m=100)
309        a2 = A(sq_km=1)
310        a3 = A(sq_km=0)
311
312        self.assertTrue(a2 > a1)
313        self.assertTrue(a1 == a1)
314        self.assertTrue(a1 < a2)
315        self.failIf(a3)
316
317    def testUnitsStr(self):
318        "Testing conversion to strings"
319        a1 = A(sq_m=100)
320        a2 = A(sq_km=3.5)
321
322        self.assertEqual(str(a1), '100.0 sq_m')
323        self.assertEqual(str(a2), '3.5 sq_km')
324        self.assertEqual(repr(a1), 'Area(sq_m=100.0)')
325        self.assertEqual(repr(a2), 'Area(sq_km=3.5)')
326
327def suite():
328    s = unittest.TestSuite()
329    s.addTest(unittest.makeSuite(DistanceTest))
330    s.addTest(unittest.makeSuite(AreaTest))
331    return s
332
333def run(verbosity=2):
334    unittest.TextTestRunner(verbosity=verbosity).run(suite())
335
336if __name__=="__main__":
337    run()