~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/gis/tests/test_geoforms.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
 
 
3
from django.forms import ValidationError
 
4
from django.contrib.gis import forms
 
5
from django.contrib.gis.geos import GEOSGeometry
 
6
 
 
7
class 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.failUnless(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
 
 
56
def suite():
 
57
    s = unittest.TestSuite()
 
58
    s.addTest(unittest.makeSuite(GeometryFieldTest))
 
59
    return s
 
60
 
 
61
def run(verbosity=2):
 
62
    unittest.TextTestRunner(verbosity=verbosity).run(suite())
 
63
 
 
64
if __name__=="__main__":
 
65
    run()