1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #27024 -- Prevented logging error with empty string as geometry widget value

Thanks Gavin Wahl for the report, and Tim Graham for the review.
This commit is contained in:
Claude Paroz
2016-08-06 11:14:41 +02:00
parent 2a11d2d7a7
commit a7863c78b7
4 changed files with 75 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ from django.contrib.gis import forms
from django.contrib.gis.geos import GEOSGeometry
from django.forms import ValidationError
from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
from django.test.utils import patch_logger
from django.utils.html import escape
@@ -84,6 +85,48 @@ class GeometryFieldTest(SimpleTestCase):
form = PointForm(data={'pt': 'POINT(5 23)'}, initial={'pt': point})
self.assertFalse(form.has_changed())
def test_field_string_value(self):
"""
Initialization of a geometry field with a valid/empty/invalid string.
Only the invalid string should trigger an error log entry.
"""
class PointForm(forms.Form):
pt1 = forms.PointField(srid=4326)
pt2 = forms.PointField(srid=4326)
pt3 = forms.PointField(srid=4326)
form = PointForm({
'pt1': 'SRID=4326;POINT(7.3 44)', # valid
'pt2': '', # empty
'pt3': 'PNT(0)', # invalid
})
with patch_logger('django.contrib.gis', 'error') as logger_calls:
output = str(form)
self.assertInHTML(
'<textarea id="id_pt1" class="vSerializedField required" cols="150"'
' rows="10" name="pt1">POINT (7.3 44)</textarea>',
output
)
self.assertInHTML(
'<textarea id="id_pt2" class="vSerializedField required" cols="150"'
' rows="10" name="pt2"></textarea>',
output
)
self.assertInHTML(
'<textarea id="id_pt3" class="vSerializedField required" cols="150"'
' rows="10" name="pt3"></textarea>',
output
)
# Only the invalid PNT(0) should trigger an error log entry
self.assertEqual(len(logger_calls), 1)
self.assertEqual(
logger_calls[0],
"Error creating geometry from value 'PNT(0)' (String or unicode input "
"unrecognized as WKT EWKT, and HEXEWKB.)"
)
@skipUnlessDBFeature("gis_enabled")
class SpecializedFieldTest(SimpleTestCase):