2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis.geos import Point
|
2018-11-27 23:22:13 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2021-12-18 01:40:22 +08:00
|
|
|
from .models import City, site, site_gis, site_gis_custom
|
2011-09-10 11:04:30 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="django.contrib.gis.tests.geoadmin.urls")
|
2018-11-27 23:22:13 +08:00
|
|
|
class GeoAdminTest(SimpleTestCase):
|
2020-12-10 01:12:56 +08:00
|
|
|
admin_site = site # ModelAdmin
|
2011-09-10 11:04:30 +08:00
|
|
|
|
2020-12-10 01:12:56 +08:00
|
|
|
def test_widget_empty_string(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
2016-08-06 17:14:41 +08:00
|
|
|
form = geoadmin.get_changelist_form(None)({"point": ""})
|
2020-12-10 01:12:56 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "no logs"):
|
|
|
|
with self.assertLogs("django.contrib.gis", "ERROR"):
|
|
|
|
output = str(form["point"])
|
2016-08-06 17:14:41 +08:00
|
|
|
self.assertInHTML(
|
2020-12-10 01:12:56 +08:00
|
|
|
'<textarea id="id_point" class="vSerializedField required" cols="150"'
|
2016-08-06 17:14:41 +08:00
|
|
|
' rows="10" name="point"></textarea>',
|
2020-12-10 01:12:56 +08:00
|
|
|
output,
|
2016-08-06 17:14:41 +08:00
|
|
|
)
|
|
|
|
|
2020-12-10 01:12:56 +08:00
|
|
|
def test_widget_invalid_string(self):
|
|
|
|
geoadmin = self.admin_site._registry[City]
|
2016-08-06 17:14:41 +08:00
|
|
|
form = geoadmin.get_changelist_form(None)({"point": "INVALID()"})
|
2018-04-28 21:20:27 +08:00
|
|
|
with self.assertLogs("django.contrib.gis", "ERROR") as cm:
|
2016-08-06 17:14:41 +08:00
|
|
|
output = str(form["point"])
|
|
|
|
self.assertInHTML(
|
2020-12-10 01:12:56 +08:00
|
|
|
'<textarea id="id_point" class="vSerializedField required" cols="150"'
|
2016-08-06 17:14:41 +08:00
|
|
|
' rows="10" name="point"></textarea>',
|
2020-12-10 01:12:56 +08:00
|
|
|
output,
|
2016-08-06 17:14:41 +08:00
|
|
|
)
|
2018-04-28 21:20:27 +08:00
|
|
|
self.assertEqual(len(cm.records), 1)
|
2016-08-06 17:14:41 +08:00
|
|
|
self.assertEqual(
|
2018-04-28 21:20:27 +08:00
|
|
|
cm.records[0].getMessage(),
|
2016-12-29 23:27:49 +08:00
|
|
|
"Error creating geometry from value 'INVALID()' (String input "
|
2020-12-10 01:12:56 +08:00
|
|
|
"unrecognized as WKT EWKT, and HEXEWKB.)",
|
2016-08-06 17:14:41 +08:00
|
|
|
)
|
2020-12-10 01:12:56 +08: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-18 01:40:22 +08: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)
|