-
Notifications
You must be signed in to change notification settings - Fork 282
/
tests.py
319 lines (244 loc) · 10.9 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import django
from django.contrib.admin import ModelAdmin, StackedInline
from django.contrib.admin.options import BaseModelAdmin, InlineModelAdmin
from django.contrib.gis.db import models as gismodels
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.test import SimpleTestCase
from .. import PLUGINS, PLUGIN_FORMS, _normalize_plugins_config
from ..templatetags import leaflet_tags
from ..admin import LeafletGeoAdmin, LeafletGeoAdminMixin
from ..forms.widgets import LeafletWidget
from ..forms import fields
class DummyStaticFilesStorage(StaticFilesStorage):
def url(self, name):
raise ValueError
class PluginListingTest(SimpleTestCase):
def test_default_resources(self):
PLUGINS.update({
'a': {'css': 'a'},
'b': {'css': 'b', 'auto-include': True},
'c': {'css': 'c'},
})
PLUGINS.pop('__is_normalized__')
_normalize_plugins_config()
names = leaflet_tags._get_plugin_names(None)
resources = leaflet_tags._get_all_resources_for_plugins(names, 'css')
self.assertEqual(['/static/b'], resources)
def test_all_resources(self):
PLUGINS.update({
'a': {'css': 'a'},
'b': {'css': 'b'},
'c': {'css': 'c'},
})
PLUGINS.pop('ALL')
PLUGINS.pop(PLUGIN_FORMS)
PLUGINS.pop('__is_normalized__')
_normalize_plugins_config()
names = leaflet_tags._get_plugin_names('ALL')
resources = leaflet_tags._get_all_resources_for_plugins(names, 'css')
self.assertEqual(['/static/a', '/static/b', '/static/c'],
sorted(resources))
def test_explicit_resources(self):
PLUGINS.update({
'a': {'css': 'a'},
'b': {'css': 'b'},
'c': {'css': 'c'},
})
PLUGINS.pop('__is_normalized__')
_normalize_plugins_config()
names = leaflet_tags._get_plugin_names('a,c')
resources = leaflet_tags._get_all_resources_for_plugins(names, 'css')
self.assertEqual(['/static/a', '/static/c'], sorted(resources))
class TemplateTagTest(SimpleTestCase):
def test_default_layer_in_leaflet_map(self):
context = leaflet_tags.leaflet_map('map')
self.assertEqual('map', context['name'])
self.assertTrue('"OSM", "//tile.openstreetmap.org/{z}/{x}/{y}.png"' in
context['djoptions'])
class LeafletWidgetRenderingTest(SimpleTestCase):
def test_default_media(self):
widget = LeafletWidget()
media = widget.media
self.assertEqual([], media.render_js())
self.assertEqual([], list(media.render_css()))
def test_included_media(self):
class LeafletWidgetMedia(LeafletWidget):
include_media = True
widget = LeafletWidgetMedia()
media = widget.media
media_js = "".join(media.render_js())
media_css = "".join(media.render_css())
self.assertIn('leaflet/leaflet.js', media_js)
self.assertIn('leaflet/leaflet.extras.js', media_js)
self.assertIn('leaflet/leaflet.forms.js', media_js)
self.assertIn('leaflet/draw/leaflet.draw.js', media_js)
self.assertIn('leaflet/leaflet.css', media_css)
self.assertIn('leaflet/draw/leaflet.draw.css', media_css)
def test_widget_geometry_is_empty_string(self):
widget = LeafletWidget()
widget.render('geom', '', {'id': 'geom'})
self.assertTrue(True, 'We should\'t accept blank geometry in value.')
def test_widget_get_attrs_deprecation(self):
widget = LeafletWidget()
with self.assertWarns(DeprecationWarning):
priv_result = widget._get_attrs("foo")
pub_result = widget.get_attrs("foo")
self.assertEqual(
priv_result, pub_result,
msg=(
'Deprecated \'LeafletWidget._get_attrs\' does not have the the same '
'output as public method \'LeafletWidget.get_attrs\'.'
)
)
class LeafletFieldsWidgetsTest(SimpleTestCase):
def test_default_widget(self):
for typ in ['Geometry', 'Point', 'MultiPoint', 'LineString', 'Polygon',
'MultiLineString', 'MultiPolygon', 'GeometryCollection']:
f = getattr(fields, typ + 'Field')()
self.assertTrue(isinstance(f.widget, LeafletWidget))
self.assertEqual(f.widget.geom_type, typ.upper())
class DummyAdminSite:
"""
Mock adminsite, which is required by InlineModelAdmin.__init__
"""
def is_registered(self, model):
return True
class DummyModel(gismodels.Model):
geom = gismodels.PointField()
class Meta:
app_label = "leaflet"
class DummyInlineModel(gismodels.Model):
geom = gismodels.PointField()
class Meta:
app_label = "leaflet"
class BaseLeafletGeoAdminTest:
modeladmin_class = None # type: BaseModelAdmin
leafletgeoadmin_class = None # type: LeafletGeoAdmin
def setUp(self):
self.modeladmin = self.leafletgeoadmin_class(DummyModel, DummyAdminSite())
self.geomfield = DummyModel._meta.get_field('geom')
self.formfield = self.modeladmin.formfield_for_dbfield(self.geomfield, None)
def test_widget_for_field(self):
widget = self.formfield.widget
self.assertTrue(issubclass(widget.__class__, LeafletWidget))
def test_widget_parameters(self):
widget = self.formfield.widget
self.assertEqual(widget.geom_type, 'POINT')
self.assertEqual(widget.settings_overrides, {'DEFAULT_CENTER': (8.0, 3.15), })
self.assertTrue(widget.modifiable)
def test_widget_media(self):
widget = self.formfield.widget
media = widget.media
media_js = "".join(media.render_js())
media_css = "".join(media.render_css())
self.assertIn('leaflet/leaflet.js', media_js)
self.assertIn('leaflet/leaflet.extras.js', media_js)
self.assertIn('leaflet/leaflet.forms.js', media_js)
self.assertIn('leaflet/draw/leaflet.draw.js', media_js)
self.assertIn('leaflet/leaflet.css', media_css)
self.assertIn('leaflet/draw/leaflet.draw.css', media_css)
def test_is_subclass_of_modeladmin(self):
self.assertTrue(issubclass(self.leafletgeoadmin_class, self.modeladmin_class))
class DummyAdminSettingsOverridesMixin:
settings_overrides = {
'DEFAULT_CENTER': (8.0, 3.15),
}
class DummyMixinModelAdmin(DummyAdminSettingsOverridesMixin, LeafletGeoAdminMixin, ModelAdmin):
pass
class LeafletGeoAdminMixinTest(BaseLeafletGeoAdminTest, SimpleTestCase):
modeladmin_class = ModelAdmin
leafletgeoadmin_class = DummyMixinModelAdmin
def test_is_not_subclass_of_modeladmin(self):
self.assertFalse(issubclass(LeafletGeoAdminMixin, BaseModelAdmin))
class DummyModelAdmin(DummyAdminSettingsOverridesMixin, LeafletGeoAdmin):
pass
class LeafletGeoAdminTest(BaseLeafletGeoAdminTest, SimpleTestCase):
modeladmin_class = ModelAdmin
leafletgeoadmin_class = DummyModelAdmin
class DummyMixinStackedInline(DummyAdminSettingsOverridesMixin, LeafletGeoAdminMixin, StackedInline):
model = DummyInlineModel
class LeafletGeoAdminStackedInlineTest(BaseLeafletGeoAdminTest, SimpleTestCase):
modeladmin_class = InlineModelAdmin
leafletgeoadmin_class = DummyMixinStackedInline
class LeafletWidgetMapTest(SimpleTestCase):
def test_default_parameters(self):
widget = LeafletWidget()
output = widget.render('geom', '', {'id': 'geom'})
self.assertIn(".fieldid = 'geom'", output)
self.assertIn(".srid = 4326", output)
self.assertIn(".geom_type = 'Geometry'", output)
self.assertIn('#geom { display: none; }', output)
self.assertIn('function geom_map_callback(map, options)', output)
def test_overriden_parameters(self):
class PolygonWidget(LeafletWidget):
geom_type = 'POLYGON'
widget = PolygonWidget()
output = widget.render('geometry', '', {'id': 'geometry'})
self.assertIn(".fieldid = 'geometry'", output)
self.assertIn(".geom_type = 'Polygon'", output)
self.assertIn('#geometry { display: none; }', output)
self.assertIn('function geometry_map_callback(map, options)', output)
def test_javascript_safe_callback_name(self):
widget = LeafletWidget()
output = widget.render('prefix-geom', '')
self.assertIn('function prefix_geom_map_callback(map, options)', output)
output = widget.render('geom', '', {'id': 'prefix-geom'})
self.assertIn('function prefix_geom_map_callback(map, options)', output)
class SettingsOverridesTest(SimpleTestCase):
def test_settings_overrides(self):
widget = LeafletWidget(attrs={
'settings_overrides': {
'DEFAULT_CENTER': (8.0, 3.14),
}
})
output = widget.render('geom', '', {'id': 'geom'})
self.assertIn('"center": [8.0, 3.14]', output)
def test_spatial_extent_settings_overrides(self):
widget = LeafletWidget(attrs={
'settings_overrides': {
'SPATIAL_EXTENT': (
3.812255859375,
50.387507803003146,
4.0869140625,
50.523904629228625,
),
'DEFAULT_ZOOM': None,
'DEFAULT_CENTER': None,
}
})
output = widget.render('geom', '', {'id': 'geom'})
self.assertIn('"extent": [[50.387507803003146, 3.812255859375], [50.523904629228625, 4.0869140625]]', output)
self.assertIn('"center": null', output)
self.assertIn('"zoom": null', output)
class LeafletModelFormTest(SimpleTestCase):
def test_modelform_field_conformity(self):
class DummyForm(django.forms.ModelForm):
geom = fields.PointField()
class Meta:
model = DummyModel
fields = ['geom']
form = DummyForm()
output = form.as_p()
self.assertIn(".geom_type = 'Point'", output)
def test_modelform_widget_conformity(self):
class DummyForm(django.forms.ModelForm):
class Meta:
model = DummyModel
fields = ['geom']
widgets = {'geom': LeafletWidget()}
form = DummyForm()
output = form.as_p()
self.assertIn(".geom_type = 'Point'", output)
class LeafletGeoAdminMapTest(LeafletGeoAdminTest):
def test_widget_template_overriden(self):
widget = self.formfield.widget
output = widget.render('geom', '', {'id': 'geom'})
self.assertIn('<div id="geom-div-map">', output)
self.assertEqual(
list(widget.media.render_css()),
[
'<link href="/static/leaflet/leaflet.css" media="screen" rel="stylesheet">',
'<link href="/static/leaflet/leaflet_django.css" media="screen" rel="stylesheet">',
'<link href="/static/leaflet/draw/leaflet.draw.css" media="screen" rel="stylesheet">',
]
)