Ticket #15271: 0001-Tests-for-GeometryField.to_python-method.patch

File 0001-Tests-for-GeometryField.to_python-method.patch, 2.1 KB (added by Daniel Barreto, 14 years ago)
  • django/contrib/gis/tests/test_geoforms.py

    From 15b7bcf24d16cb023f6bf7024bc220377f97cbfa Mon Sep 17 00:00:00 2001
    From: Daniel Barreto <daniel@gia.usb.ve>
    Date: Fri, 11 Feb 2011 15:11:25 -0430
    Subject: [PATCH] Tests for GeometryField.to_python method
    
    ---
     django/contrib/gis/tests/test_geoforms.py |   16 +++++++++++++++-
     1 files changed, 15 insertions(+), 1 deletions(-)
    
    diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py
    index 4d55bf5..5b4084d 100644
    a b class GeometryFieldTest(unittest.TestCase):  
    4242        fld = forms.GeometryField(required=False)
    4343        self.assertEqual(None, fld.clean(None))
    4444
    45     def test03_geom_type(self):
     45    def test03_to_python(self):
     46        """Testing to_python returns a correct GEOSGeometry object or
     47        a ValidationError"""
     48        fld = forms.GeometryField()
     49        # to_python returns the same GEOSGeometry for a WKT
     50        for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
     51            self.assertEqual(GEOSGeometry(wkt), fld.to_python(wkt))
     52        # but raises a ValidationError for any other string
     53        for wkt in ('POINT(5)', 'MULTI   POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
     54            self.assertRaises(forms.ValidationError, fld.to_python, wkt)
     55
     56    def test04_geom_type(self):
    4657        "Testing GeometryField's handling of different geometry types."
    4758        # By default, all geometry types are allowed.
    4859        fld = forms.GeometryField()
    class GeometryFieldTest(unittest.TestCase):  
    5162
    5263        pnt_fld = forms.GeometryField(geom_type='POINT')
    5364        self.assertEqual(GEOSGeometry('POINT(5 23)'), pnt_fld.clean('POINT(5 23)'))
     65        # a WKT for any other geom_type will be properly transformed by `to_python`
     66        self.assertEqual(GEOSGeometry('LINESTRING(0 0, 1 1)'), pnt_fld.to_python('LINESTRING(0 0, 1 1)'))
     67        # but rejected by `clean`
    5468        self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)')
    5569
    5670def suite():
Back to Top