2015-04-24 17:24:07 +02:00
|
|
|
from django.contrib.gis.geos import Point
|
2018-11-27 16:22:13 +01:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2021-12-17 18:40:22 +01:00
|
|
|
from .models import City, site, site_gis, site_gis_custom
|
2011-09-10 05:04:30 +02:00
|
|
|
|
2013-05-11 05:08:45 +02:00
|
|
|
|
2014-04-05 08:04:46 +02:00
|
|
|
@override_settings(ROOT_URLCONF="django.contrib.gis.tests.geoadmin.urls")
|
2018-11-27 16:22:13 +01:00
|
|
|
class GeoAdminTest(SimpleTestCase):
|
2020-12-09 18:12:56 +01:00
|
|
|
admin_site = site # ModelAdmin
|
2011-09-10 05:04:30 +02:00
|
|
|
|
2020-12-09 18:12:56 +01:00
|
|
|
def test_widget_empty_string(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
2016-08-06 11:14:41 +02:00
|
|
|
form = geoadmin.get_changelist_form(None)({"point": ""})
|
2020-12-09 18:12:56 +01:00
|
|
|
with self.assertRaisesMessage(AssertionError, "no logs"):
|
|
|
|
with self.assertLogs("django.contrib.gis", "ERROR"):
|
|
|
|
output = str(form["point"])
|
2016-08-06 11:14:41 +02:00
|
|
|
self.assertInHTML(
|
2020-12-09 18:12:56 +01:00
|
|
|
'<textarea id="id_point" class="vSerializedField required" cols="150"'
|
2016-08-06 11:14:41 +02:00
|
|
|
' rows="10" name="point"></textarea>',
|
2020-12-09 18:12:56 +01:00
|
|
|
output,
|
2016-08-06 11:14:41 +02:00
|
|
|
)
|
|
|
|
|
2020-12-09 18:12:56 +01:00
|
|
|
def test_widget_invalid_string(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
2016-08-06 11:14:41 +02:00
|
|
|
form = geoadmin.get_changelist_form(None)({"point": "INVALID()"})
|
2018-04-28 15:20:27 +02:00
|
|
|
with self.assertLogs("django.contrib.gis", "ERROR") as cm:
|
2016-08-06 11:14:41 +02:00
|
|
|
output = str(form["point"])
|
|
|
|
self.assertInHTML(
|
2020-12-09 18:12:56 +01:00
|
|
|
'<textarea id="id_point" class="vSerializedField required" cols="150"'
|
2016-08-06 11:14:41 +02:00
|
|
|
' rows="10" name="point"></textarea>',
|
2020-12-09 18:12:56 +01:00
|
|
|
output,
|
2016-08-06 11:14:41 +02:00
|
|
|
)
|
2018-04-28 15:20:27 +02:00
|
|
|
self.assertEqual(len(cm.records), 1)
|
2016-08-06 11:14:41 +02:00
|
|
|
self.assertEqual(
|
2018-04-28 15:20:27 +02:00
|
|
|
cm.records[0].getMessage(),
|
2016-12-29 16:27:49 +01:00
|
|
|
"Error creating geometry from value 'INVALID()' (String input "
|
2020-12-09 18:12:56 +01:00
|
|
|
"unrecognized as WKT EWKT, and HEXEWKB.)",
|
2016-08-06 11:14:41 +02:00
|
|
|
)
|
2020-12-09 18:12:56 +01:00
|
|
|
|
|
|
|
def test_widget_has_changed(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
|
|
|
form = geoadmin.get_changelist_form(None)()
|
|
|
|
has_changed = form.fields["point"].has_changed
|
|
|
|
|
|
|
|
initial = Point(13.4197458572965953, 52.5194108501149799, srid=4326)
|
|
|
|
data_same = "SRID=3857;POINT(1493879.2754093995 6894592.019687599)"
|
|
|
|
data_almost_same = "SRID=3857;POINT(1493879.2754093990 6894592.019687590)"
|
|
|
|
data_changed = "SRID=3857;POINT(1493884.0527237 6894593.8111804)"
|
|
|
|
|
|
|
|
self.assertIs(has_changed(None, data_changed), True)
|
|
|
|
self.assertIs(has_changed(initial, ""), True)
|
|
|
|
self.assertIs(has_changed(None, ""), False)
|
|
|
|
self.assertIs(has_changed(initial, data_same), False)
|
|
|
|
self.assertIs(has_changed(initial, data_almost_same), False)
|
|
|
|
self.assertIs(has_changed(initial, data_changed), True)
|
|
|
|
|
|
|
|
|
|
|
|
class GISAdminTests(GeoAdminTest):
|
|
|
|
admin_site = site_gis # GISModelAdmin
|
2021-12-17 18:40:22 +01:00
|
|
|
|
|
|
|
def test_default_gis_widget_kwargs(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
|
|
|
form = geoadmin.get_changelist_form(None)()
|
|
|
|
widget = form["point"].field.widget
|
|
|
|
self.assertEqual(widget.attrs["default_lat"], 47)
|
|
|
|
self.assertEqual(widget.attrs["default_lon"], 5)
|
|
|
|
self.assertEqual(widget.attrs["default_zoom"], 12)
|
|
|
|
|
|
|
|
def test_custom_gis_widget_kwargs(self):
|
|
|
|
geoadmin = site_gis_custom._registry[City]
|
|
|
|
form = geoadmin.get_changelist_form(None)()
|
|
|
|
widget = form["point"].field.widget
|
|
|
|
self.assertEqual(widget.attrs["default_lat"], 55)
|
|
|
|
self.assertEqual(widget.attrs["default_lon"], 37)
|
|
|
|
self.assertEqual(widget.attrs["default_zoom"], 12)
|