Initialization of a geometry field with a valid/empty/invalid string. Only the invalid string should trigger an error log entry.
(self)
| 139 | self.assertFalse(form.has_changed()) |
| 140 | |
| 141 | def test_field_string_value(self): |
| 142 | """ |
| 143 | Initialization of a geometry field with a valid/empty/invalid string. |
| 144 | Only the invalid string should trigger an error log entry. |
| 145 | """ |
| 146 | |
| 147 | class PointForm(forms.Form): |
| 148 | pt1 = forms.PointField(srid=4326) |
| 149 | pt2 = forms.PointField(srid=4326) |
| 150 | pt3 = forms.PointField(srid=4326) |
| 151 | |
| 152 | form = PointForm( |
| 153 | { |
| 154 | "pt1": "SRID=4326;POINT(7.3 44)", # valid |
| 155 | "pt2": "", # empty |
| 156 | "pt3": "PNT(0)", # invalid |
| 157 | } |
| 158 | ) |
| 159 | |
| 160 | with self.assertLogs("django.contrib.gis", "ERROR") as logger_calls: |
| 161 | output = str(form) |
| 162 | |
| 163 | # The first point can't use assertInHTML() due to non-deterministic |
| 164 | # ordering of the rendered dictionary. |
| 165 | pt1_serialized = re.search(r"<textarea [^>]*>({[^<]+})<", output)[1] |
| 166 | pt1_json = pt1_serialized.replace(""", '"') |
| 167 | pt1_expected = GEOSGeometry(form.data["pt1"]).transform(3857, clone=True) |
| 168 | self.assertJSONEqual(pt1_json, pt1_expected.json) |
| 169 | |
| 170 | self.assertInHTML( |
| 171 | '<textarea id="id_pt2" class="vSerializedField required" cols="150"' |
| 172 | ' rows="10" name="pt2" hidden></textarea>', |
| 173 | output, |
| 174 | ) |
| 175 | self.assertInHTML( |
| 176 | '<textarea id="id_pt3" class="vSerializedField required" cols="150"' |
| 177 | ' rows="10" name="pt3" hidden></textarea>', |
| 178 | output, |
| 179 | ) |
| 180 | # Only the invalid PNT(0) triggers an error log entry. |
| 181 | # Deserialization is called in form clean and in widget rendering. |
| 182 | self.assertEqual(len(logger_calls.records), 2) |
| 183 | self.assertEqual( |
| 184 | logger_calls.records[0].getMessage(), |
| 185 | "Error creating geometry from value 'PNT(0)' (String input " |
| 186 | "unrecognized as WKT EWKT, and HEXEWKB.)", |
| 187 | ) |
| 188 | |
| 189 | def test_override_attrs(self): |
| 190 | self.assertIsNone(forms.BaseGeometryWidget.base_layer) |
nothing calls this directly
no test coverage detected