From 9f1dabc98403a9cbdd5ba640f18fd5a6d4f1fa70 Mon Sep 17 00:00:00 2001 From: Matthew Perry Date: Mon, 13 Nov 2017 09:21:07 -0700 Subject: [PATCH] tests for heatmaps --- mapboxgl/__init__.py | 4 ++-- mapboxgl/utils.py | 2 ++ mapboxgl/viz.py | 5 +++-- tests/test_html.py | 17 ++++++++++++++++- tests/test_utils.py | 7 ++++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/mapboxgl/__init__.py b/mapboxgl/__init__.py index 3262b5f..8d54768 100644 --- a/mapboxgl/__init__.py +++ b/mapboxgl/__init__.py @@ -1,4 +1,4 @@ -from .viz import CircleViz, GraduatedCircleViz +from .viz import CircleViz, GraduatedCircleViz, HeatmapViz __version__ = "0.0.3" -__all__ = ['CircleViz', 'GraduatedCircleViz'] +__all__ = ['CircleViz', 'GraduatedCircleViz', 'HeatmapViz'] diff --git a/mapboxgl/utils.py b/mapboxgl/utils.py index f9e860c..e694233 100644 --- a/mapboxgl/utils.py +++ b/mapboxgl/utils.py @@ -27,6 +27,7 @@ def df_to_geojson(df, properties=None, lat='lat', lon='lon', precision=None): return geojson + def scale_between(minval, maxval, numStops): """ Scale a min and max value to equal interval domain with numStops discrete values @@ -57,6 +58,7 @@ def create_radius_stops(breaks, min_radius, max_radius): stops.append([b, radius_breaks[i]]) return stops + def create_weight_stops(breaks): """Convert data breaks into a heatmap-weight ramp """ diff --git a/mapboxgl/viz.py b/mapboxgl/viz.py index d89caee..e745267 100644 --- a/mapboxgl/viz.py +++ b/mapboxgl/viz.py @@ -188,6 +188,7 @@ def create_html(self): return templates.format('graduated_circle', **options) + class HeatmapViz(object): """Create a heatmap viz""" @@ -210,8 +211,8 @@ def __init__(self, :param weight_property: property to determine heatmap weight. EX. "population" :param weight_stops: stops to determine heatmap weight. EX. [[10, 0], [100, 1]] - :param color_stops: stops to determine heatmap color. EX. [[0, "red"], [0.5, "blue"], [1, "green"]]] - :param radius_stops: stops to determine heatmap radius based on zoom. EX: [[0, 1], [12, 30]]] + :param color_stops: stops to determine heatmap color. EX. [[0, "red"], [0.5, "blue"], [1, "green"]] + :param radius_stops: stops to determine heatmap radius based on zoom. EX: [[0, 1], [12, 30]] :param style_url: url to mapbox style :param access_token: Mapbox GL JS access token. diff --git a/tests/test_html.py b/tests/test_html.py index cc8ff9a..da3c5ed 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -3,7 +3,7 @@ import pytest -from mapboxgl.viz import CircleViz, GraduatedCircleViz +from mapboxgl.viz import CircleViz, GraduatedCircleViz, HeatmapViz from mapboxgl.errors import TokenError @@ -23,6 +23,7 @@ def test_secret_key_CircleViz(data): with pytest.raises(TokenError): CircleViz(data, access_token=secret) + def test_secret_key_GraduatedCircleViz(data): """Secret key raises a token error """ @@ -87,3 +88,17 @@ def test_display_GraduatedCircleViz(display, data): access_token=TOKEN) viz.show() display.assert_called_once() + + +@patch('mapboxgl.viz.display') +def test_display_HeatmapViz(display, data): + """Assert that show calls the mocked display function + """ + viz = HeatmapViz(data, + weight_property="Avg Medicare Payments", + weight_stops=[[10, 0], [100, 1]], + color_stops=[[0, "red"], [0.5, "blue"], [1, "green"]], + radius_stops=[[0, 1], [12, 30]], + access_token=TOKEN) + viz.show() + display.assert_called_once() diff --git a/tests/test_utils.py b/tests/test_utils.py index d267e5b..2910156 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,7 +2,7 @@ import pytest -from mapboxgl.utils import df_to_geojson, scale_between, create_radius_stops +from mapboxgl.utils import df_to_geojson, scale_between, create_radius_stops, create_weight_stops class MockDataframe(object): @@ -85,3 +85,8 @@ def test_create_radius_stops(df): domain = [7678.214347826088, 5793.63142857143, 1200] radius_stops = create_radius_stops(domain, 1, 10) assert radius_stops == [[7678.214347826088, 1.0], [5793.63142857143, 4.0], [1200, 7.0]] + + +def test_create_weight_stops(df): + res = create_weight_stops([1, 2, 3, 4]) + assert res == [[1, 0.0], [2, 0.25], [3, 0.5], [4, 0.75]]