Path: blob/main/tests/plugins/test_heat_map.py
2542 views
"""1Test HeatMap2------------3"""45import numpy as np6import pytest78import folium9from folium.plugins import HeatMap10from folium.template import Template11from folium.utilities import normalize121314def test_heat_map():15np.random.seed(3141592)16data = np.random.normal(size=(100, 2)) * np.array([[1, 1]]) + np.array([[48, 5]])17m = folium.Map([48.0, 5.0], zoom_start=6)18hm = HeatMap(data)19m.add_child(hm)20m._repr_html_()2122out = normalize(m._parent.render())2324# We verify that the script import is present.25script = '<script src="https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js"></script>' # noqa26assert script in out2728# We verify that the script part is correct.29tmpl = Template("""30var {{this.get_name()}} = L.heatLayer(31{{this.data}},32{33minOpacity: {{this.min_opacity}},34maxZoom: {{this.max_zoom}},35radius: {{this.radius}},36blur: {{this.blur}},37gradient: {{this.gradient}}38})39.addTo({{this._parent.get_name()}});40""")4142assert tmpl.render(this=hm)4344bounds = m.get_bounds()45np.testing.assert_allclose(46bounds,47[48[46.218566840847025, 3.0302801394447734],49[50.75345011431167, 7.132453997672826],50],51)525354def test_heatmap_data():55data = HeatMap(np.array([[3, 4, 1], [5, 6, 1], [7, 8, 0.5]])).data56assert isinstance(data, list)57assert len(data) == 358for i in range(len(data)):59assert isinstance(data[i], list)60assert len(data[i]) == 3616263def test_heat_map_exception():64with pytest.raises(ValueError):65HeatMap(np.array([[4, 5, 1], [3, 6, np.nan]]))66with pytest.raises(Exception):67HeatMap(np.array([3, 4, 5]))686970