Path: blob/main/tests/plugins/test_fast_marker_cluster.py
2538 views
"""1Test FastMarkerCluster2----------------------3"""45import numpy as np6import pandas as pd7import pytest89import folium10from folium.plugins import FastMarkerCluster11from folium.template import Template12from folium.utilities import normalize131415def test_fast_marker_cluster():16n = 10017np.random.seed(seed=26082009)18data = np.array(19[20np.random.uniform(low=35, high=60, size=n),21np.random.uniform(low=-12, high=30, size=n),22]23).T24m = folium.Map([45.0, 3.0], zoom_start=4)25mc = FastMarkerCluster(data).add_to(m)2627out = normalize(m._parent.render())2829# We verify that imports30assert (31'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/leaflet.markercluster.js"></script>' # noqa32in out33) # noqa34assert (35'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.css"/>' # noqa36in out37) # noqa38assert (39'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.Default.css"/>' # noqa40in out41) # noqa4243# Verify the script part is okay.44tmpl = Template("""45var {{ this.get_name() }} = (function(){46{{ this.callback }}4748var data = {{ this.data|tojson }};49var cluster = L.markerClusterGroup({{ this.options|tojavascript }});50{%- if this.icon_create_function is not none %}51cluster.options.iconCreateFunction =52{{ this.icon_create_function.strip() }};53{%- endif %}5455for (var i = 0; i < data.length; i++) {56var row = data[i];57var marker = callback(row);58marker.addTo(cluster);59}6061cluster.addTo({{ this._parent.get_name() }});62return cluster;63})();64""")65expected = normalize(tmpl.render(this=mc))66assert expected in out676869@pytest.mark.parametrize(70"case",71[72np.array([[0, 5, 1], [1, 6, 1], [2, 7, 0.5]]),73[[0, 5, "red"], (1, 6, "blue"), [2, 7, {"this": "also works"}]],74pd.DataFrame(75[[0, 5, "red"], [1, 6, "blue"], [2, 7, "something"]],76columns=["lat", "lng", "color"],77),78],79)80def test_fast_marker_cluster_data(case):81data = FastMarkerCluster(case).data82assert isinstance(data, list)83assert len(data) == 384for i in range(len(data)):85assert isinstance(data[i], list)86assert len(data[i]) == 387assert data[i][0] == float(i)88assert data[i][1] == float(i + 5)899091