PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 21ms RepoModel.GetById 39ms app.codeStats 0ms

/django/contrib/gis/geos/tests/test_geos_mutation.py

https://code.google.com/p/mango-py/
Python | 137 lines | 100 code | 25 blank | 12 comment | 25 complexity | 715d240b2b20783c29940bd5af0aa160 MD5 | raw file
  1# Copyright (c) 2008-2009 Aryeh Leib Taurog, all rights reserved.
  2# Modified from original contribution by Aryeh Leib Taurog, which was
  3# released under the New BSD license.
  4
  5import django.utils.copycompat as copy
  6
  7from django.contrib.gis.geos import *
  8from django.contrib.gis.geos.error import GEOSIndexError
  9from django.utils import unittest
 10
 11def getItem(o,i): return o[i]
 12def delItem(o,i): del o[i]
 13def setItem(o,i,v): o[i] = v
 14
 15def api_get_distance(x): return x.distance(Point(-200,-200))
 16def api_get_buffer(x): return x.buffer(10)
 17def api_get_geom_typeid(x): return x.geom_typeid
 18def api_get_num_coords(x): return x.num_coords
 19def api_get_centroid(x): return x.centroid
 20def api_get_empty(x): return x.empty
 21def api_get_valid(x): return x.valid
 22def api_get_simple(x): return x.simple
 23def api_get_ring(x): return x.ring
 24def api_get_boundary(x): return x.boundary
 25def api_get_convex_hull(x): return x.convex_hull
 26def api_get_extent(x): return x.extent
 27def api_get_area(x): return x.area
 28def api_get_length(x): return x.length
 29
 30geos_function_tests =  [ val for name, val in vars().items()
 31                        if hasattr(val, '__call__')
 32                        and name.startswith('api_get_') ]
 33
 34class GEOSMutationTest(unittest.TestCase):
 35    """
 36    Tests Pythonic Mutability of Python GEOS geometry wrappers
 37    get/set/delitem on a slice, normal list methods
 38    """
 39
 40    def test00_GEOSIndexException(self):
 41        'Testing Geometry GEOSIndexError'
 42        p = Point(1,2)
 43        for i in range(-2,2): p._checkindex(i)
 44        self.assertRaises(GEOSIndexError, p._checkindex, 2)
 45        self.assertRaises(GEOSIndexError, p._checkindex, -3)
 46
 47    def test01_PointMutations(self):
 48        'Testing Point mutations'
 49        for p in (Point(1,2,3), fromstr('POINT (1 2 3)')):
 50            self.assertEqual(p._get_single_external(1), 2.0, 'Point _get_single_external')
 51
 52            # _set_single
 53            p._set_single(0,100)
 54            self.assertEqual(p.coords, (100.0,2.0,3.0), 'Point _set_single')
 55
 56            # _set_list
 57            p._set_list(2,(50,3141))
 58            self.assertEqual(p.coords, (50.0,3141.0), 'Point _set_list')
 59
 60    def test02_PointExceptions(self):
 61        'Testing Point exceptions'
 62        self.assertRaises(TypeError, Point, range(1))
 63        self.assertRaises(TypeError, Point, range(4))
 64
 65    def test03_PointApi(self):
 66        'Testing Point API'
 67        q = Point(4,5,3)
 68        for p in (Point(1,2,3), fromstr('POINT (1 2 3)')):
 69            p[0:2] = [4,5]
 70            for f in geos_function_tests:
 71                self.assertEqual(f(q), f(p), 'Point ' + f.__name__)
 72
 73    def test04_LineStringMutations(self):
 74        'Testing LineString mutations'
 75        for ls in (LineString((1,0),(4,1),(6,-1)),
 76                    fromstr('LINESTRING (1 0,4 1,6 -1)')):
 77            self.assertEqual(ls._get_single_external(1), (4.0,1.0), 'LineString _get_single_external')
 78
 79            # _set_single
 80            ls._set_single(0,(-50,25))
 81            self.assertEqual(ls.coords, ((-50.0,25.0),(4.0,1.0),(6.0,-1.0)), 'LineString _set_single')
 82
 83            # _set_list
 84            ls._set_list(2, ((-50.0,25.0),(6.0,-1.0)))
 85            self.assertEqual(ls.coords, ((-50.0,25.0),(6.0,-1.0)), 'LineString _set_list')
 86
 87            lsa = LineString(ls.coords)
 88            for f in geos_function_tests:
 89                self.assertEqual(f(lsa), f(ls), 'LineString ' + f.__name__)
 90
 91    def test05_Polygon(self):
 92        'Testing Polygon mutations'
 93        for pg in (Polygon(((1,0),(4,1),(6,-1),(8,10),(1,0)),
 94                            ((5,4),(6,4),(6,3),(5,4))),
 95                    fromstr('POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))')):
 96            self.assertEqual(pg._get_single_external(0),
 97                            LinearRing((1,0),(4,1),(6,-1),(8,10),(1,0)),
 98                            'Polygon _get_single_external(0)')
 99            self.assertEqual(pg._get_single_external(1),
100                            LinearRing((5,4),(6,4),(6,3),(5,4)),
101                            'Polygon _get_single_external(1)')
102
103            # _set_list
104            pg._set_list(2, (((1,2),(10,0),(12,9),(-1,15),(1,2)),
105                            ((4,2),(5,2),(5,3),(4,2))))
106            self.assertEqual(pg.coords,
107                    (((1.0,2.0),(10.0,0.0),(12.0,9.0),(-1.0,15.0),(1.0,2.0)),
108                        ((4.0,2.0),(5.0,2.0),(5.0,3.0),(4.0,2.0))),
109                    'Polygon _set_list')
110
111            lsa = Polygon(*pg.coords)
112            for f in geos_function_tests:
113                self.assertEqual(f(lsa), f(pg), 'Polygon ' + f.__name__)
114
115    def test06_Collection(self):
116        'Testing Collection mutations'
117        for mp in (MultiPoint(*map(Point,((3,4),(-1,2),(5,-4),(2,8)))),
118                    fromstr('MULTIPOINT (3 4,-1 2,5 -4,2 8)')):
119            self.assertEqual(mp._get_single_external(2), Point(5,-4), 'Collection _get_single_external')
120
121            mp._set_list(3, map(Point,((5,5),(3,-2),(8,1))))
122            self.assertEqual(mp.coords, ((5.0,5.0),(3.0,-2.0),(8.0,1.0)), 'Collection _set_list')
123
124            lsa = MultiPoint(*map(Point,((5,5),(3,-2),(8,1))))
125            for f in geos_function_tests:
126                self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__)
127
128def suite():
129    s = unittest.TestSuite()
130    s.addTest(unittest.makeSuite(GEOSMutationTest))
131    return s
132
133def run(verbosity=2):
134    unittest.TextTestRunner(verbosity=verbosity).run(suite())
135
136if __name__ == '__main__':
137    run()