PageRenderTime 1139ms CodeModel.GetById 90ms app.highlight 15ms RepoModel.GetById 116ms app.codeStats 0ms

/django/contrib/gis/gdal/tests/test_envelope.py

https://code.google.com/p/mango-py/
Python | 95 lines | 80 code | 15 blank | 0 comment | 0 complexity | 2d51986464d56cd38bac0cf153ccbe26 MD5 | raw file
 1from django.contrib.gis.gdal import Envelope, OGRException
 2from django.utils import unittest
 3
 4
 5class TestPoint(object):
 6    def __init__(self, x, y):
 7        self.x = x
 8        self.y = y
 9
10class EnvelopeTest(unittest.TestCase):
11
12    def setUp(self):
13        self.e = Envelope(0, 0, 5, 5)
14
15    def test01_init(self):
16        "Testing Envelope initilization."
17        e1 = Envelope((0, 0, 5, 5))
18        e2 = Envelope(0, 0, 5, 5)
19        e3 = Envelope(0, '0', '5', 5) # Thanks to ww for this
20        e4 = Envelope(e1._envelope)
21        self.assertRaises(OGRException, Envelope, (5, 5, 0, 0))
22        self.assertRaises(OGRException, Envelope, 5, 5, 0, 0)
23        self.assertRaises(OGRException, Envelope, (0, 0, 5, 5, 3))
24        self.assertRaises(OGRException, Envelope, ())
25        self.assertRaises(ValueError, Envelope, 0, 'a', 5, 5)
26        self.assertRaises(TypeError, Envelope, u'foo')
27        self.assertRaises(OGRException, Envelope, (1, 1, 0, 0))
28        try:
29            Envelope(0, 0, 0, 0)
30        except OGRException:
31            self.fail("shouldn't raise an exception for min_x == max_x or min_y == max_y")
32
33    def test02_properties(self):
34        "Testing Envelope properties."
35        e = Envelope(0, 0, 2, 3)
36        self.assertEqual(0, e.min_x)
37        self.assertEqual(0, e.min_y)
38        self.assertEqual(2, e.max_x)
39        self.assertEqual(3, e.max_y)
40        self.assertEqual((0, 0), e.ll)
41        self.assertEqual((2, 3), e.ur)
42        self.assertEqual((0, 0, 2, 3), e.tuple)
43        self.assertEqual('POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))', e.wkt)
44        self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))
45
46    def test03_equivalence(self):
47        "Testing Envelope equivalence."
48        e1 = Envelope(0.523, 0.217, 253.23, 523.69)
49        e2 = Envelope((0.523, 0.217, 253.23, 523.69))
50        self.assertEqual(e1, e2)
51        self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
52
53    def test04_expand_to_include_pt_2_params(self):
54        "Testing Envelope expand_to_include -- point as two parameters."
55        self.e.expand_to_include(2, 6)
56        self.assertEqual((0, 0, 5, 6), self.e)
57        self.e.expand_to_include(-1, -1)
58        self.assertEqual((-1, -1, 5, 6), self.e)
59
60    def test05_expand_to_include_pt_2_tuple(self):
61        "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
62        self.e.expand_to_include((10, 10))
63        self.assertEqual((0, 0, 10, 10), self.e)
64        self.e.expand_to_include((-10, -10))
65        self.assertEqual((-10, -10, 10, 10), self.e)
66
67    def test06_expand_to_include_extent_4_params(self):
68        "Testing Envelope expand_to_include -- extent as 4 parameters."
69        self.e.expand_to_include(-1, 1, 3, 7)
70        self.assertEqual((-1, 0, 5, 7), self.e)
71
72    def test06_expand_to_include_extent_4_tuple(self):
73        "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
74        self.e.expand_to_include((-1, 1, 3, 7))
75        self.assertEqual((-1, 0, 5, 7), self.e)
76
77    def test07_expand_to_include_envelope(self):
78        "Testing Envelope expand_to_include with Envelope as parameter."
79        self.e.expand_to_include(Envelope(-1, 1, 3, 7))
80        self.assertEqual((-1, 0, 5, 7), self.e)
81
82    def test08_expand_to_include_point(self):
83        "Testing Envelope expand_to_include with Point as parameter."
84        self.e.expand_to_include(TestPoint(-1, 1))
85        self.assertEqual((-1, 0, 5, 5), self.e)
86        self.e.expand_to_include(TestPoint(10, 10))
87        self.assertEqual((-1, 0, 10, 10), self.e)
88
89def suite():
90    s = unittest.TestSuite()
91    s.addTest(unittest.makeSuite(EnvelopeTest))
92    return s
93
94def run(verbosity=2):
95    unittest.TextTestRunner(verbosity=verbosity).run(suite())