Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_encoded.py
1601 views
1
"""Test PolyLineFromEncoded Plugin."""
2
3
from folium import Map
4
from folium.plugins import PolygonFromEncoded, PolyLineFromEncoded
5
from folium.template import Template
6
from folium.utilities import normalize
7
8
9
def test_polyline_from_encoded():
10
"""Test `PolyLineFromEncoded` plugin.
11
12
The test ensures:
13
- The original JS script is present in the HTML file.
14
- The rendering from `PolyLineFromEncoded` and the original plugin gives the
15
same output.
16
"""
17
18
m = Map([35.0, -120.0], zoom_start=3)
19
20
encoded = r"_p~iF~cn~U_ulLn{vA_mqNvxq`@"
21
kwargs = {"color": "green"}
22
polyline = PolyLineFromEncoded(encoded=encoded, **kwargs)
23
24
polyline.add_to(m)
25
26
out = normalize(m._parent.render())
27
28
script = '<script src="https://cdn.jsdelivr.net/npm/[email protected]/Polyline.encoded.js"></script>'
29
assert script in out
30
31
tmpl = Template(
32
"""
33
var {{this.get_name()}} = L.Polyline.fromEncoded(
34
{{ this.encoded|tojson }},
35
{{ this.options|tojavascript }}
36
).addTo({{this._parent.get_name()}});
37
"""
38
)
39
40
expected_render = tmpl.render(this=polyline)
41
actual_render = polyline._template.module.script(polyline)
42
43
assert normalize(expected_render) == normalize(actual_render)
44
45
46
def test_polygon_from_encoded():
47
"""Test `PolygonFromEncoded` plugin.
48
49
The test ensures:
50
- The original JS script is present in the HTML file.
51
- The rendering from `PolygonFromEncoded` and the original plugin gives the
52
same output.
53
"""
54
55
m = Map([40.0, -80.0], zoom_start=3)
56
57
encoded = r"w`j~FpxivO}jz@qnnCd}~Bsa{@~f`C`lkH"
58
polygon = PolygonFromEncoded(encoded=encoded, kwargs={})
59
60
polygon.add_to(m)
61
62
out = normalize(m._parent.render())
63
64
script = '<script src="https://cdn.jsdelivr.net/npm/[email protected]/Polyline.encoded.js"></script>'
65
assert script in out
66
67
tmpl = Template(
68
"""
69
var {{this.get_name()}} = L.Polygon.fromEncoded(
70
{{ this.encoded|tojson }},
71
{{ this.options|tojavascript }}
72
)
73
.addTo({{this._parent.get_name()}});
74
"""
75
)
76
77
expected_render = tmpl.render(this=polygon)
78
79
actual_render = polygon._template.module.script(polygon)
80
81
assert normalize(expected_render) == normalize(actual_render)
82
83