Path: blob/main/tests/plugins/test_fast_marker_cluster.py
1601 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(45"""46var {{ this.get_name() }} = (function(){47{{ this.callback }}4849var data = {{ this.data|tojson }};50var cluster = L.markerClusterGroup({{ this.options|tojavascript }});51{%- if this.icon_create_function is not none %}52cluster.options.iconCreateFunction =53{{ this.icon_create_function.strip() }};54{%- endif %}5556for (var i = 0; i < data.length; i++) {57var row = data[i];58var marker = callback(row);59marker.addTo(cluster);60}6162cluster.addTo({{ this._parent.get_name() }});63return cluster;64})();65"""66)67expected = normalize(tmpl.render(this=mc))68assert expected in out697071@pytest.mark.parametrize(72"case",73[74np.array([[0, 5, 1], [1, 6, 1], [2, 7, 0.5]]),75[[0, 5, "red"], (1, 6, "blue"), [2, 7, {"this": "also works"}]],76pd.DataFrame(77[[0, 5, "red"], [1, 6, "blue"], [2, 7, "something"]],78columns=["lat", "lng", "color"],79),80],81)82def test_fast_marker_cluster_data(case):83data = FastMarkerCluster(case).data84assert isinstance(data, list)85assert len(data) == 386for i in range(len(data)):87assert isinstance(data[i], list)88assert len(data[i]) == 389assert data[i][0] == float(i)90assert data[i][1] == float(i + 5)919293