MCPcopy
hub / github.com/django/django / test_to_python

Method test_to_python

tests/gis_tests/test_geoforms.py:87–117  ·  view source on GitHub ↗

to_python() either returns a correct GEOSGeometry object or a ValidationError.

(self)

Source from the content-addressed store, hash-verified

85 pnt_fld.clean("LINESTRING(0 0, 1 1)")
86
87 def test_to_python(self):
88 """
89 to_python() either returns a correct GEOSGeometry object or
90 a ValidationError.
91 """
92 good_inputs = [
93 "POINT(5 23)",
94 "MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
95 "LINESTRING(0 0, 1 1)",
96 ]
97 bad_inputs = [
98 "POINT(5)",
99 "MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
100 "BLAH(0 0, 1 1)",
101 '{"type": "FeatureCollection", "features": ['
102 '{"geometry": {"type": "Point", "coordinates": [508375, 148905]}, '
103 '"type": "Feature"}]}',
104 ]
105 fld = forms.GeometryField()
106 # to_python returns the same GEOSGeometry for a WKT
107 for geo_input in good_inputs:
108 with self.subTest(geo_input=geo_input):
109 self.assertEqual(
110 GEOSGeometry(geo_input, srid=fld.widget.map_srid),
111 fld.to_python(geo_input),
112 )
113 # but raises a ValidationError for any other string
114 for geo_input in bad_inputs:
115 with self.subTest(geo_input=geo_input):
116 with self.assertRaises(ValidationError):
117 fld.to_python(geo_input)
118
119 def test_to_python_different_map_srid(self):
120 f = forms.GeometryField(widget=OpenLayersWidget)

Callers

nothing calls this directly

Calls 2

to_pythonMethod · 0.95
GEOSGeometryClass · 0.90

Tested by

no test coverage detected