web/lib/django/contrib/gis/tests/test_geoforms.py
changeset 38 77b6da96e6f1
parent 0 0d40e90630ef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/django/contrib/gis/tests/test_geoforms.py	Wed Jun 02 18:57:35 2010 +0200
@@ -0,0 +1,65 @@
+import unittest
+
+from django.forms import ValidationError
+from django.contrib.gis import forms
+from django.contrib.gis.geos import GEOSGeometry
+
+class GeometryFieldTest(unittest.TestCase):
+
+    def test00_init(self):
+        "Testing GeometryField initialization with defaults."
+        fld = forms.GeometryField()
+        for bad_default in ('blah', 3, 'FoO', None, 0):
+            self.assertRaises(ValidationError, fld.clean, bad_default)
+
+    def test01_srid(self):
+        "Testing GeometryField with a SRID set."
+        # Input that doesn't specify the SRID is assumed to be in the SRID
+        # of the input field.
+        fld = forms.GeometryField(srid=4326)
+        geom = fld.clean('POINT(5 23)')
+        self.assertEqual(4326, geom.srid)
+        # Making the field in a different SRID from that of the geometry, and
+        # asserting it transforms.
+        fld = forms.GeometryField(srid=32140)
+        tol = 0.0000001
+        xform_geom = GEOSGeometry('POINT (951640.547328465 4219369.26171664)', srid=32140)
+        # The cleaned geometry should be transformed to 32140.
+        cleaned_geom = fld.clean('SRID=4326;POINT (-95.363151 29.763374)')
+        self.failUnless(xform_geom.equals_exact(cleaned_geom, tol))
+
+    def test02_null(self):
+        "Testing GeometryField's handling of null (None) geometries."
+        # Form fields, by default, are required (`required=True`)
+        fld = forms.GeometryField()
+        self.assertRaises(forms.ValidationError, fld.clean, None)
+
+        # Still not allowed if `null=False`.
+        fld = forms.GeometryField(required=False, null=False)
+        self.assertRaises(forms.ValidationError, fld.clean, None)
+
+        # This will clean None as a geometry (See #10660).
+        fld = forms.GeometryField(required=False)
+        self.assertEqual(None, fld.clean(None))
+
+    def test03_geom_type(self):
+        "Testing GeometryField's handling of different geometry types."
+        # By default, all geometry types are allowed.
+        fld = forms.GeometryField()
+        for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
+            self.assertEqual(GEOSGeometry(wkt), fld.clean(wkt))
+
+        pnt_fld = forms.GeometryField(geom_type='POINT')
+        self.assertEqual(GEOSGeometry('POINT(5 23)'), pnt_fld.clean('POINT(5 23)'))
+        self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)')
+
+def suite():
+    s = unittest.TestSuite()
+    s.addTest(unittest.makeSuite(GeometryFieldTest))
+    return s
+
+def run(verbosity=2):
+    unittest.TextTestRunner(verbosity=verbosity).run(suite())
+
+if __name__=="__main__":
+    run()