2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis import admin
|
|
|
|
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
|
|
|
|
2015-04-24 23:24:07 +08:00
|
|
|
from .admin import UnmodifiableAdmin
|
2016-06-14 22:18:33 +08:00
|
|
|
from .models import City, site
|
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):
|
2011-09-10 11:04:30 +08:00
|
|
|
|
2012-06-06 16:40:11 +08:00
|
|
|
def test_ensure_geographic_media(self):
|
2015-03-31 00:33:26 +08:00
|
|
|
geoadmin = site._registry[City]
|
2011-09-10 11:04:30 +08:00
|
|
|
admin_js = geoadmin.media.render_js()
|
2013-08-30 07:20:00 +08:00
|
|
|
self.assertTrue(any(geoadmin.openlayers_url in js for js in admin_js))
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2012-05-26 03:00:44 +08:00
|
|
|
def test_olmap_OSM_rendering(self):
|
2014-08-05 00:14:45 +08:00
|
|
|
delete_all_btn = """<a href="javascript:geodjango_point.clearFeatures()">Delete all Features</a>"""
|
|
|
|
|
2015-03-31 00:33:26 +08:00
|
|
|
original_geoadmin = site._registry[City]
|
2014-08-05 00:14:45 +08:00
|
|
|
params = original_geoadmin.get_map_widget(City._meta.get_field('point')).params
|
|
|
|
result = original_geoadmin.get_map_widget(City._meta.get_field('point'))(
|
|
|
|
).render('point', Point(-79.460734, 40.18476), params)
|
2012-05-26 03:00:44 +08:00
|
|
|
self.assertIn(
|
|
|
|
"""geodjango_point.layers.base = new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)");""",
|
|
|
|
result)
|
|
|
|
|
2014-08-05 00:14:45 +08:00
|
|
|
self.assertIn(delete_all_btn, result)
|
|
|
|
|
2015-03-31 00:33:26 +08:00
|
|
|
site.unregister(City)
|
|
|
|
site.register(City, UnmodifiableAdmin)
|
2014-08-05 00:14:45 +08:00
|
|
|
try:
|
2015-03-31 00:33:26 +08:00
|
|
|
geoadmin = site._registry[City]
|
2014-08-05 00:14:45 +08:00
|
|
|
params = geoadmin.get_map_widget(City._meta.get_field('point')).params
|
|
|
|
result = geoadmin.get_map_widget(City._meta.get_field('point'))(
|
|
|
|
).render('point', Point(-79.460734, 40.18476), params)
|
|
|
|
|
|
|
|
self.assertNotIn(delete_all_btn, result)
|
|
|
|
finally:
|
2015-03-31 00:33:26 +08:00
|
|
|
site.unregister(City)
|
|
|
|
site.register(City, original_geoadmin.__class__)
|
2014-08-05 00:14:45 +08:00
|
|
|
|
2012-05-26 03:00:44 +08:00
|
|
|
def test_olmap_WMS_rendering(self):
|
2015-03-31 00:33:26 +08:00
|
|
|
geoadmin = admin.GeoModelAdmin(City, site)
|
2012-05-26 03:00:44 +08:00
|
|
|
result = geoadmin.get_map_widget(City._meta.get_field('point'))(
|
2013-10-18 17:02:43 +08:00
|
|
|
).render('point', Point(-79.460734, 40.18476))
|
2012-05-26 03:00:44 +08:00
|
|
|
self.assertIn(
|
2014-09-04 20:15:09 +08:00
|
|
|
"""geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", """
|
|
|
|
""""http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic', format: 'image/jpeg'});""",
|
2012-05-26 03:00:44 +08:00
|
|
|
result)
|
2012-06-06 16:40:11 +08:00
|
|
|
|
|
|
|
def test_olwidget_has_changed(self):
|
2013-07-22 15:55:45 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Changes are accurately noticed by OpenLayersWidget.
|
2013-07-22 15:55:45 +08:00
|
|
|
"""
|
2015-03-31 00:33:26 +08:00
|
|
|
geoadmin = site._registry[City]
|
2012-06-06 16:40:11 +08:00
|
|
|
form = geoadmin.get_changelist_form(None)()
|
2014-08-07 10:56:23 +08:00
|
|
|
has_changed = form.fields['point'].has_changed
|
2012-06-06 16:40:11 +08:00
|
|
|
|
|
|
|
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.assertTrue(has_changed(None, data_changed))
|
|
|
|
self.assertTrue(has_changed(initial, ""))
|
|
|
|
self.assertFalse(has_changed(None, ""))
|
|
|
|
self.assertFalse(has_changed(initial, data_same))
|
|
|
|
self.assertFalse(has_changed(initial, data_almost_same))
|
|
|
|
self.assertTrue(has_changed(initial, data_changed))
|
2016-08-06 17:14:41 +08:00
|
|
|
|
|
|
|
def test_olwidget_empty_string(self):
|
|
|
|
geoadmin = site._registry[City]
|
|
|
|
form = geoadmin.get_changelist_form(None)({'point': ''})
|
2021-01-08 00:54:40 +08:00
|
|
|
with self.assertNoLogs('django.contrib.gis', 'ERROR'):
|
|
|
|
output = str(form['point'])
|
2016-08-06 17:14:41 +08:00
|
|
|
self.assertInHTML(
|
|
|
|
'<textarea id="id_point" class="vWKTField required" cols="150"'
|
|
|
|
' rows="10" name="point"></textarea>',
|
|
|
|
output
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_olwidget_invalid_string(self):
|
|
|
|
geoadmin = site._registry[City]
|
|
|
|
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(
|
|
|
|
'<textarea id="id_point" class="vWKTField required" cols="150"'
|
|
|
|
' rows="10" name="point"></textarea>',
|
|
|
|
output
|
|
|
|
)
|
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 "
|
2016-08-06 17:14:41 +08:00
|
|
|
"unrecognized as WKT EWKT, and HEXEWKB.)"
|
|
|
|
)
|