Skip to content

Moved plugins test to tests/plugins #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions folium/plugins/boat_marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def __init__(self, position=None, heading=0,
{% endmacro %}
""") # noqa


def render(self, **kwargs):
def render(self, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that the tests found this bug 😜

super(BoatMarker, self).render(**kwargs)

figure = self.get_root()
Expand Down
7 changes: 7 additions & 0 deletions tests/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
Plugins tests
-------------

Create at least one test per plugin.
"""
47 changes: 47 additions & 0 deletions tests/plugins/test_boat_marker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""
Test BoatMarker
---------------

"""
from jinja2 import Template

import folium
from folium import plugins

def test_boat_marker():
m = folium.Map([30., 0.], zoom_start=3)
bm1 = plugins.BoatMarker(
(34, -43),
heading=45,
wind_heading=150,
wind_speed=45,
color="#8f8")
bm2 = plugins.BoatMarker(
(46, -30),
heading=-20,
wind_heading=46,
wind_speed=25,
color="#88f")

m.add_children(bm1)
m.add_children(bm2)
m._repr_html_()

out = m._parent.render()

# We verify that the script import is present
assert ('<script src="https://thomasbrueggemann.github.io/leaflet.boatmarker/'
'js/leaflet.boatmarker.min.js"></script>'
) in out

# We verify that the script part is correct
tmpl = Template("""
var {{this.get_name()}} = L.boatMarker(
[{{this.position[0]}},{{this.position[1]}}],
{{this.kwargs}}).addTo({{this._parent.get_name()}});
{{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
""")

assert tmpl.render(this=bm1) in out
assert tmpl.render(this=bm2) in out
41 changes: 41 additions & 0 deletions tests/plugins/test_heat_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
Test HeatMap
------------
"""

from jinja2 import Template
import numpy as np

import folium
from folium import plugins

def test_heat_map():
data = (np.random.normal(size=(100, 2)) * np.array([[1, 1]]) +
np.array([[48, 5]])).tolist()
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
hm = plugins.HeatMap(data)
m.add_children(hm)
m._repr_html_()

out = m._parent.render()

# We verify that the script import is present
assert ('<script src="https://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"></script>'
) in out
# We verify that the script part is correct
tmpl = Template("""
var {{this.get_name()}} = L.heatLayer(
{{this.data}},
{
minOpacity: {{this.min_opacity}},
maxZoom: {{this.max_zoom}},
max: {{this.max_val}},
radius: {{this.radius}},
blur: {{this.blur}},
gradient: {{this.gradient}}
})
.addTo({{this._parent.get_name()}});
""")

assert tmpl.render(this=hm)
37 changes: 37 additions & 0 deletions tests/plugins/test_image_overlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
"""
Test ImageOverlay
-----------------

"""

from jinja2 import Template

import folium
from folium import plugins

def test_image_overlay():
"""Test image overlay."""
data = [[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]]]

m = folium.Map()
io = plugins.ImageOverlay(data, [[0, -180], [90, 180]], mercator_project=True)
io.add_to(m)
m._repr_html_()

out = m._parent.render()

# Verify the url generation
assert io.url == ('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAA'
'AF0lEQVR42mP4z8AARFDw/z/DeiA5H4QBV60H6ABl9ZIAAAAASUVORK5CYII=')

# Verify the script part is okay.
tmpl = Template("""
var {{this.get_name()}} = L.imageOverlay(
'{{ this.url }}',
{{ this.bounds }},
{{ this.options }}
).addTo({{this._parent.get_name()}});
""")
assert tmpl.render(this=io) in out
51 changes: 51 additions & 0 deletions tests/plugins/test_marker_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
Test MarkerCluster
------------------
"""

from jinja2 import Template
import numpy as np

import folium
from folium import plugins

def test_marker_cluster():
N = 100
data = np.array([
np.random.uniform(low=35, high=60, size=N), # Random latitudes.
np.random.uniform(low=-12, high=30, size=N), # Random longitudes.
range(N), # Popups.
]).T
m = folium.Map([45., 3.], zoom_start=4)
mc = plugins.MarkerCluster(data)
m.add_children(mc)
m._repr_html_()

out = m._parent.render()

# We verify that imports
assert ('<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.'
'markercluster/0.4.0/leaflet.markercluster.js"></script>') in out
assert ('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/'
'libs/leaflet.markercluster/0.4.0/MarkerCluster.css" />') in out
assert ('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/'
'libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css" />'
) in out

# Verify the script part is okay.
tmpl = Template("""
var {{this.get_name()}} = L.markerClusterGroup();
{{this._parent.get_name()}}.addLayer({{this.get_name()}});

{% for marker in this._children.values() %}
var {{marker.get_name()}} = L.marker(
[{{marker.location[0]}},{{marker.location[1]}}],
{
icon: new L.Icon.Default()
}
)
.addTo({{this.get_name()}});
{% endfor %}
""")
assert ''.join(tmpl.render(this=mc).split()) in ''.join(out.split())
63 changes: 63 additions & 0 deletions tests/plugins/test_scroll_zoom_toggler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
"""
Test ScrollZoomToggler
----------------------
"""

from jinja2 import Template

import folium
from folium import plugins

def test_scroll_zoom_toggler():
m = folium.Map([45., 3.], zoom_start=4)
szt = plugins.ScrollZoomToggler()
m.add_children(szt)
m._repr_html_()

out = m._parent.render()

# Verify that the div has been created.
tmpl = Template("""
<img id="{{this.get_name()}}" alt="scroll"
src="https://cdnjs.cloudflare.com/ajax/libs/ionicons/1.5.2/png/512/arrow-move.png"
onclick="{{this._parent.get_name()}}.toggleScroll()"></img>
""")
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())

# Verify that the style has been created
tmpl = Template("""
<style>
#{{this.get_name()}} {
position:absolute;
width:35px;
bottom:10px;
height:35px;
left:10px;
background-color:#fff;
text-align:center;
line-height:35px;
vertical-align: middle;
}
</style>
""")
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())

# Verify that the script is okay.
tmpl = Template("""
{{this._parent.get_name()}}.scrollEnabled = true;

{{this._parent.get_name()}}.toggleScroll = function() {
if (this.scrollEnabled) {
this.scrollEnabled = false;
this.scrollWheelZoom.disable();
}
else {
this.scrollEnabled = true;
this.scrollWheelZoom.enable();
}
};

{{this._parent.get_name()}}.toggleScroll();
""")
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())
22 changes: 22 additions & 0 deletions tests/plugins/test_terminator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
Test Terminator
---------------
"""

from jinja2 import Template

import folium
from folium import plugins

def test_terminator():
m = folium.Map([45., 3.], zoom_start=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Virtually all tests start by importing, jinja2.Template, folium, and folium.plugins and creating a map. What do you think of an external util.py with this a simple function to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be okay, but I would keep it for another PR (maybe v0.3).

At the cost of doing this, I'd like to have an object like PluginTester so that we do something like :

class TestTerminator(PluginTester):
    def test_main(self):
        ...

but PluginTester would also have a method we could use to generate all the test examples for the doc. I think it's too bad to implement nice maps in the test and have to re-implement it in notebooks for docs and examples.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a plan. Can I merge it then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appart from this, I've applied you comments (spaces), and rebased.
Ready for merge.
Thanks @ocefpaf

t = plugins.Terminator()
m.add_children(t)
m._repr_html_()

out = m._parent.render()

# Verify that the script is okay.
tmpl = Template('L.terminator().addTo({{this._parent.get_name()}});')
assert ''.join(tmpl.render(this=t).split()) in ''.join(out.split())
Loading