/
test_features.py
304 lines (242 loc) · 8.74 KB
/
test_features.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
""""
Folium Features Tests
---------------------
"""
import json
import os
import warnings
import pytest
from branca.element import Element
import folium
from folium import ClickForMarker, GeoJson, Map, Popup
@pytest.fixture
def tmpl():
yield (
"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
</body>
<script>
</script>
</html>
"""
) # noqa
# Root path variable
rootpath = os.path.abspath(os.path.dirname(__file__))
# Figure
def test_figure_creation():
f = folium.Figure()
assert isinstance(f, Element)
bounds = f.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def test_figure_rendering():
f = folium.Figure()
out = f.render()
assert type(out) is str
bounds = f.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def test_figure_html(tmpl):
f = folium.Figure()
out = f.render()
out = os.linesep.join([s.strip() for s in out.splitlines() if s.strip()])
tmpl = os.linesep.join([s.strip() for s in tmpl.splitlines() if s.strip()])
assert out == tmpl, "\n" + out + "\n" + "-" * 80 + "\n" + tmpl
bounds = f.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def test_figure_double_rendering():
f = folium.Figure()
out = f.render()
out2 = f.render()
assert out == out2
bounds = f.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def test_marker_popups():
m = Map()
folium.Marker([45, -180], popup="-180").add_to(m)
folium.Marker([45, -120], popup=Popup("-120")).add_to(m)
folium.RegularPolygonMarker([45, -60], popup="-60").add_to(m)
folium.RegularPolygonMarker([45, 0], popup=Popup("0")).add_to(m)
folium.CircleMarker([45, 60], popup="60").add_to(m)
folium.CircleMarker([45, 120], popup=Popup("120")).add_to(m)
folium.CircleMarker([45, 90], popup=Popup("90"), weight=0).add_to(m)
m._repr_html_()
bounds = m.get_bounds()
assert bounds == [[45, -180], [45, 120]], bounds
# DivIcon.
def test_divicon():
html = """<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>""" # noqa
div = folium.DivIcon(html=html)
assert isinstance(div, Element)
assert div.options["className"] == "empty"
assert div.options["html"] == html
# ColorLine.
def test_color_line():
m = Map([22.5, 22.5], zoom_start=3)
color_line = folium.ColorLine(
[[0, 0], [0, 45], [45, 45], [45, 0], [0, 0]],
[0, 1, 2, 3],
colormap=["b", "g", "y", "r"],
nb_steps=4,
weight=10,
opacity=1,
)
m.add_child(color_line)
m._repr_html_()
@pytest.fixture
def vegalite_spec(version):
file_version = "v1" if version == 1 else "vlater"
file = os.path.join(rootpath, "vegalite_data", f"vegalite_{file_version}.json")
if not os.path.exists(file):
raise FileNotFoundError(f"The vegalite data {file} does not exist.")
with open(file) as f:
spec = json.load(f)
if version is None or "$schema" in spec:
return spec
# Sample versions that might show up
schema_version = {2: "v2.6.0", 3: "v3.6.0", 4: "v4.6.0", 5: "v5.1.0"}[version]
spec["$schema"] = f"https://vega.github.io/schema/vega-lite/{schema_version}.json"
return spec
@pytest.mark.parametrize("version", [1, 2, 3, 4, 5, None])
def test_vegalite_major_version(vegalite_spec, version):
vegalite = folium.features.VegaLite(vegalite_spec)
if version is None:
assert vegalite.vegalite_major_version is None
else:
assert vegalite.vegalite_major_version == version
# GeoJsonTooltip GeometryCollection
def test_geojson_tooltip():
m = folium.Map([30.5, -97.5], zoom_start=10)
folium.GeoJson(
os.path.join(rootpath, "kuntarajat.geojson"),
tooltip=folium.GeoJsonTooltip(fields=["code", "name"]),
).add_to(m)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
m._repr_html_()
assert issubclass(
w[-1].category, UserWarning
), "GeoJsonTooltip GeometryCollection test failed."
# GeoJsonMarker type validation.
def test_geojson_marker():
m = folium.Map([30.4, -97.5], zoom_start=10)
with pytest.raises(TypeError):
folium.GeoJson(
os.path.join(rootpath, "subwaystations.geojson"), marker=ClickForMarker()
).add_to(m)
def test_geojson_find_identifier():
def _create(*properties):
return {
"type": "FeatureCollection",
"features": [
{"type": "Feature", "properties": item} for item in properties
],
}
def _assert_id_got_added(data):
_geojson = GeoJson(data)
assert _geojson.find_identifier() == "feature.id"
assert _geojson.data["features"][0]["id"] == "0"
data_with_id = _create(None, None)
data_with_id["features"][0]["id"] = "this-is-an-id"
data_with_id["features"][1]["id"] = "this-is-another-id"
geojson = GeoJson(data_with_id)
assert geojson.find_identifier() == "feature.id"
assert geojson.data["features"][0]["id"] == "this-is-an-id"
data_with_unique_properties = _create(
{"property-key": "some-value"},
{"property-key": "another-value"},
)
geojson = GeoJson(data_with_unique_properties)
assert geojson.find_identifier() == "feature.properties.property-key"
data_with_unique_properties = _create(
{"property-key": 42},
{"property-key": 43},
{"property-key": "or a string"},
)
geojson = GeoJson(data_with_unique_properties)
assert geojson.find_identifier() == "feature.properties.property-key"
# The test cases below have no id field or unique property,
# so an id will be added to the data.
data_with_identical_ids = _create(None, None)
data_with_identical_ids["features"][0]["id"] = "identical-ids"
data_with_identical_ids["features"][1]["id"] = "identical-ids"
_assert_id_got_added(data_with_identical_ids)
data_with_some_missing_ids = _create(None, None)
data_with_some_missing_ids["features"][0]["id"] = "this-is-an-id"
# the second feature doesn't have an id
_assert_id_got_added(data_with_some_missing_ids)
data_with_identical_properties = _create(
{"property-key": "identical-value"},
{"property-key": "identical-value"},
)
_assert_id_got_added(data_with_identical_properties)
data_bare = _create(None)
_assert_id_got_added(data_bare)
data_empty_dict = _create({})
_assert_id_got_added(data_empty_dict)
data_without_properties = _create(None)
del data_without_properties["features"][0]["properties"]
_assert_id_got_added(data_without_properties)
data_some_without_properties = _create({"key": "value"}, "will be deleted")
# the first feature has properties, but the second doesn't
del data_some_without_properties["features"][1]["properties"]
_assert_id_got_added(data_some_without_properties)
data_with_nested_properties = _create(
{
"summary": {"distance": 343.2},
"way_points": [3, 5],
}
)
_assert_id_got_added(data_with_nested_properties)
data_with_incompatible_properties = _create(
{
"summary": {"distances": [0, 6], "durations": None},
"way_points": [3, 5],
}
)
_assert_id_got_added(data_with_incompatible_properties)
data_loose_geometry = {
"type": "LineString",
"coordinates": [
[3.961389, 43.583333],
[3.968056, 43.580833],
[3.974722, 43.578333],
[3.986389, 43.575278],
[3.998333, 43.5725],
[4.163333, 43.530556],
],
}
geojson = GeoJson(data_loose_geometry)
geojson.convert_to_feature_collection()
assert geojson.find_identifier() == "feature.id"
assert geojson.data["features"][0]["id"] == "0"
def test_geojson_empty_features_with_styling():
# test we don't fail style function validation when there are no features
m = Map()
data = {"type": "FeatureCollection", "features": []}
GeoJson(data, style_function=lambda x: {}).add_to(m)
m.get_root().render()
def test_geometry_collection_get_bounds():
"""Assert #1599 is fixed"""
geojson_data = {
"geometries": [
{
"coordinates": [
[
[-1, 1],
[0, 2],
[-3, 4],
[2, 0],
]
],
"type": "Polygon",
},
],
"type": "GeometryCollection",
}
assert folium.GeoJson(geojson_data).get_bounds() == [[0, -3], [4, 2]]