Skip to content

Commit 0c02b72

Browse files
committed
Merge pull request #304 from BibMartin/plugin_tests
Moved plugins test to tests/plugins
2 parents 7ec30b7 + 3e9dd99 commit 0c02b72

11 files changed

+411
-148
lines changed

folium/plugins/boat_marker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def __init__(self, position=None, heading=0,
5353
{% endmacro %}
5454
""") # noqa
5555

56-
57-
def render(self, **kwargs):
56+
def render(self, **kwargs):
5857
super(BoatMarker, self).render(**kwargs)
5958

6059
figure = self.get_root()

tests/plugins/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Plugins tests
4+
-------------
5+
6+
Create at least one test per plugin.
7+
"""

tests/plugins/test_boat_marker.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test BoatMarker
4+
---------------
5+
6+
"""
7+
from jinja2 import Template
8+
9+
import folium
10+
from folium import plugins
11+
12+
def test_boat_marker():
13+
m = folium.Map([30., 0.], zoom_start=3)
14+
bm1 = plugins.BoatMarker(
15+
(34, -43),
16+
heading=45,
17+
wind_heading=150,
18+
wind_speed=45,
19+
color="#8f8")
20+
bm2 = plugins.BoatMarker(
21+
(46, -30),
22+
heading=-20,
23+
wind_heading=46,
24+
wind_speed=25,
25+
color="#88f")
26+
27+
m.add_children(bm1)
28+
m.add_children(bm2)
29+
m._repr_html_()
30+
31+
out = m._parent.render()
32+
33+
# We verify that the script import is present
34+
assert ('<script src="https://thomasbrueggemann.github.io/leaflet.boatmarker/'
35+
'js/leaflet.boatmarker.min.js"></script>'
36+
) in out
37+
38+
# We verify that the script part is correct
39+
tmpl = Template("""
40+
var {{this.get_name()}} = L.boatMarker(
41+
[{{this.position[0]}},{{this.position[1]}}],
42+
{{this.kwargs}}).addTo({{this._parent.get_name()}});
43+
{{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
44+
""")
45+
46+
assert tmpl.render(this=bm1) in out
47+
assert tmpl.render(this=bm2) in out

tests/plugins/test_heat_map.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test HeatMap
4+
------------
5+
"""
6+
7+
from jinja2 import Template
8+
import numpy as np
9+
10+
import folium
11+
from folium import plugins
12+
13+
def test_heat_map():
14+
data = (np.random.normal(size=(100, 2)) * np.array([[1, 1]]) +
15+
np.array([[48, 5]])).tolist()
16+
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
17+
hm = plugins.HeatMap(data)
18+
m.add_children(hm)
19+
m._repr_html_()
20+
21+
out = m._parent.render()
22+
23+
# We verify that the script import is present
24+
assert ('<script src="https://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"></script>'
25+
) in out
26+
# We verify that the script part is correct
27+
tmpl = Template("""
28+
var {{this.get_name()}} = L.heatLayer(
29+
{{this.data}},
30+
{
31+
minOpacity: {{this.min_opacity}},
32+
maxZoom: {{this.max_zoom}},
33+
max: {{this.max_val}},
34+
radius: {{this.radius}},
35+
blur: {{this.blur}},
36+
gradient: {{this.gradient}}
37+
})
38+
.addTo({{this._parent.get_name()}});
39+
""")
40+
41+
assert tmpl.render(this=hm)

tests/plugins/test_image_overlay.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test ImageOverlay
4+
-----------------
5+
6+
"""
7+
8+
from jinja2 import Template
9+
10+
import folium
11+
from folium import plugins
12+
13+
def test_image_overlay():
14+
"""Test image overlay."""
15+
data = [[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
16+
[[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]]]
17+
18+
m = folium.Map()
19+
io = plugins.ImageOverlay(data, [[0, -180], [90, 180]], mercator_project=True)
20+
io.add_to(m)
21+
m._repr_html_()
22+
23+
out = m._parent.render()
24+
25+
# Verify the url generation
26+
assert io.url == ('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAA'
27+
'AF0lEQVR42mP4z8AARFDw/z/DeiA5H4QBV60H6ABl9ZIAAAAASUVORK5CYII=')
28+
29+
# Verify the script part is okay.
30+
tmpl = Template("""
31+
var {{this.get_name()}} = L.imageOverlay(
32+
'{{ this.url }}',
33+
{{ this.bounds }},
34+
{{ this.options }}
35+
).addTo({{this._parent.get_name()}});
36+
""")
37+
assert tmpl.render(this=io) in out

tests/plugins/test_marker_cluster.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test MarkerCluster
4+
------------------
5+
"""
6+
7+
from jinja2 import Template
8+
import numpy as np
9+
10+
import folium
11+
from folium import plugins
12+
13+
def test_marker_cluster():
14+
N = 100
15+
data = np.array([
16+
np.random.uniform(low=35, high=60, size=N), # Random latitudes.
17+
np.random.uniform(low=-12, high=30, size=N), # Random longitudes.
18+
range(N), # Popups.
19+
]).T
20+
m = folium.Map([45., 3.], zoom_start=4)
21+
mc = plugins.MarkerCluster(data)
22+
m.add_children(mc)
23+
m._repr_html_()
24+
25+
out = m._parent.render()
26+
27+
# We verify that imports
28+
assert ('<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.'
29+
'markercluster/0.4.0/leaflet.markercluster.js"></script>') in out
30+
assert ('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/'
31+
'libs/leaflet.markercluster/0.4.0/MarkerCluster.css" />') in out
32+
assert ('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/'
33+
'libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css" />'
34+
) in out
35+
36+
# Verify the script part is okay.
37+
tmpl = Template("""
38+
var {{this.get_name()}} = L.markerClusterGroup();
39+
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
40+
41+
{% for marker in this._children.values() %}
42+
var {{marker.get_name()}} = L.marker(
43+
[{{marker.location[0]}},{{marker.location[1]}}],
44+
{
45+
icon: new L.Icon.Default()
46+
}
47+
)
48+
.addTo({{this.get_name()}});
49+
{% endfor %}
50+
""")
51+
assert ''.join(tmpl.render(this=mc).split()) in ''.join(out.split())
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test ScrollZoomToggler
4+
----------------------
5+
"""
6+
7+
from jinja2 import Template
8+
9+
import folium
10+
from folium import plugins
11+
12+
def test_scroll_zoom_toggler():
13+
m = folium.Map([45., 3.], zoom_start=4)
14+
szt = plugins.ScrollZoomToggler()
15+
m.add_children(szt)
16+
m._repr_html_()
17+
18+
out = m._parent.render()
19+
20+
# Verify that the div has been created.
21+
tmpl = Template("""
22+
<img id="{{this.get_name()}}" alt="scroll"
23+
src="https://cdnjs.cloudflare.com/ajax/libs/ionicons/1.5.2/png/512/arrow-move.png"
24+
onclick="{{this._parent.get_name()}}.toggleScroll()"></img>
25+
""")
26+
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())
27+
28+
# Verify that the style has been created
29+
tmpl = Template("""
30+
<style>
31+
#{{this.get_name()}} {
32+
position:absolute;
33+
width:35px;
34+
bottom:10px;
35+
height:35px;
36+
left:10px;
37+
background-color:#fff;
38+
text-align:center;
39+
line-height:35px;
40+
vertical-align: middle;
41+
}
42+
</style>
43+
""")
44+
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())
45+
46+
# Verify that the script is okay.
47+
tmpl = Template("""
48+
{{this._parent.get_name()}}.scrollEnabled = true;
49+
50+
{{this._parent.get_name()}}.toggleScroll = function() {
51+
if (this.scrollEnabled) {
52+
this.scrollEnabled = false;
53+
this.scrollWheelZoom.disable();
54+
}
55+
else {
56+
this.scrollEnabled = true;
57+
this.scrollWheelZoom.enable();
58+
}
59+
};
60+
61+
{{this._parent.get_name()}}.toggleScroll();
62+
""")
63+
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())

tests/plugins/test_terminator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test Terminator
4+
---------------
5+
"""
6+
7+
from jinja2 import Template
8+
9+
import folium
10+
from folium import plugins
11+
12+
def test_terminator():
13+
m = folium.Map([45., 3.], zoom_start=1)
14+
t = plugins.Terminator()
15+
m.add_children(t)
16+
m._repr_html_()
17+
18+
out = m._parent.render()
19+
20+
# Verify that the script is okay.
21+
tmpl = Template('L.terminator().addTo({{this._parent.get_name()}});')
22+
assert ''.join(tmpl.render(this=t).split()) in ''.join(out.split())

0 commit comments

Comments
 (0)