Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_encoded.py
2532 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
var {{this.get_name()}} = L.Polyline.fromEncoded(
33
{{ this.encoded|tojson }},
34
{{ this.options|tojavascript }}
35
).addTo({{this._parent.get_name()}});
36
""")
37
38
expected_render = tmpl.render(this=polyline)
39
actual_render = polyline._template.module.script(polyline)
40
41
assert normalize(expected_render) == normalize(actual_render)
42
43
44
def test_polygon_from_encoded():
45
"""Test `PolygonFromEncoded` plugin.
46
47
The test ensures:
48
- The original JS script is present in the HTML file.
49
- The rendering from `PolygonFromEncoded` and the original plugin gives the
50
same output.
51
"""
52
53
m = Map([40.0, -80.0], zoom_start=3)
54
55
encoded = r"w`j~FpxivO}jz@qnnCd}~Bsa{@~f`C`lkH"
56
polygon = PolygonFromEncoded(encoded=encoded, kwargs={})
57
58
polygon.add_to(m)
59
60
out = normalize(m._parent.render())
61
62
script = '<script src="https://cdn.jsdelivr.net/npm/[email protected]/Polyline.encoded.js"></script>'
63
assert script in out
64
65
tmpl = Template("""
66
var {{this.get_name()}} = L.Polygon.fromEncoded(
67
{{ this.encoded|tojson }},
68
{{ this.options|tojavascript }}
69
)
70
.addTo({{this._parent.get_name()}});
71
""")
72
73
expected_render = tmpl.render(this=polygon)
74
75
actual_render = polygon._template.module.script(polygon)
76
77
assert normalize(expected_render) == normalize(actual_render)
78
79