Path: blob/main/tests/plugins/test_realtime.py
2529 views
"""1Test Realtime2------------------3"""45import folium6from folium.plugins import MarkerCluster, Realtime7from folium.template import Template8from folium.utilities import JsCode, normalize91011def test_realtime():12m = folium.Map(location=[40.73, -73.94], zoom_start=12)13source = "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson"1415container = MarkerCluster().add_to(m)1617rt = Realtime(18source,19get_feature_id=JsCode("(f) => { return f.properties.objectid }"),20point_to_layer=JsCode(21"(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}"22),23container=container,24interval=10000,25)26rt.add_to(m)2728tmpl_for_expected = Template("""29{% macro script(this, kwargs) %}30var {{ this.get_name() }}_options = {{ this.options|tojavascript }};31{% for key, value in this.functions.items() %}32{{ this.get_name() }}_options["{{key}}"] = {{ value }};33{% endfor %}3435{% if this.container -%}36{{ this.get_name() }}_options["container"]37= {{ this.container.get_name() }};38{% endif -%}3940var {{ this.get_name() }} = new L.realtime(41{% if this.src is string or this.src is mapping -%}42{{ this.src|tojson }},43{% else -%}44{{ this.src.js_code }},45{% endif -%}46{{ this.get_name() }}_options47);48{{ this._parent.get_name() }}.addLayer(49{{ this.get_name() }}._container);50{% endmacro %}51""")52expected = normalize(tmpl_for_expected.render(this=rt))5354out = normalize(m._parent.render())5556# We verify that imports57assert (58'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.2.0/leaflet-realtime.js"></script>' # noqa59in out60) # noqa6162assert expected in out636465