/django/contrib/gis/tests/test_geoforms.py
Python | 65 lines | 44 code | 12 blank | 9 comment | 5 complexity | 9a895fa66cc7bf2078101058ef0a7fe4 MD5 | raw file
Possible License(s): BSD-3-Clause
1from django.forms import ValidationError 2from django.contrib.gis import forms 3from django.contrib.gis.geos import GEOSGeometry 4from django.utils import unittest 5 6 7class GeometryFieldTest(unittest.TestCase): 8 9 def test00_init(self): 10 "Testing GeometryField initialization with defaults." 11 fld = forms.GeometryField() 12 for bad_default in ('blah', 3, 'FoO', None, 0): 13 self.assertRaises(ValidationError, fld.clean, bad_default) 14 15 def test01_srid(self): 16 "Testing GeometryField with a SRID set." 17 # Input that doesn't specify the SRID is assumed to be in the SRID 18 # of the input field. 19 fld = forms.GeometryField(srid=4326) 20 geom = fld.clean('POINT(5 23)') 21 self.assertEqual(4326, geom.srid) 22 # Making the field in a different SRID from that of the geometry, and 23 # asserting it transforms. 24 fld = forms.GeometryField(srid=32140) 25 tol = 0.0000001 26 xform_geom = GEOSGeometry('POINT (951640.547328465 4219369.26171664)', srid=32140) 27 # The cleaned geometry should be transformed to 32140. 28 cleaned_geom = fld.clean('SRID=4326;POINT (-95.363151 29.763374)') 29 self.assertTrue(xform_geom.equals_exact(cleaned_geom, tol)) 30 31 def test02_null(self): 32 "Testing GeometryField's handling of null (None) geometries." 33 # Form fields, by default, are required (`required=True`) 34 fld = forms.GeometryField() 35 self.assertRaises(forms.ValidationError, fld.clean, None) 36 37 # Still not allowed if `null=False`. 38 fld = forms.GeometryField(required=False, null=False) 39 self.assertRaises(forms.ValidationError, fld.clean, None) 40 41 # This will clean None as a geometry (See #10660). 42 fld = forms.GeometryField(required=False) 43 self.assertEqual(None, fld.clean(None)) 44 45 def test03_geom_type(self): 46 "Testing GeometryField's handling of different geometry types." 47 # By default, all geometry types are allowed. 48 fld = forms.GeometryField() 49 for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'): 50 self.assertEqual(GEOSGeometry(wkt), fld.clean(wkt)) 51 52 pnt_fld = forms.GeometryField(geom_type='POINT') 53 self.assertEqual(GEOSGeometry('POINT(5 23)'), pnt_fld.clean('POINT(5 23)')) 54 self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)') 55 56def suite(): 57 s = unittest.TestSuite() 58 s.addTest(unittest.makeSuite(GeometryFieldTest)) 59 return s 60 61def run(verbosity=2): 62 unittest.TextTestRunner(verbosity=verbosity).run(suite()) 63 64if __name__=="__main__": 65 run()