/django/contrib/gis/tests/test_geoforms.py

https://code.google.com/p/mango-py/ · Python · 65 lines · 44 code · 12 blank · 9 comment · 3 complexity · 9a895fa66cc7bf2078101058ef0a7fe4 MD5 · raw file

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