From e34ce79a6d087945bb4b5d63004f86c7ff01d5a3 Mon Sep 17 00:00:00 2001 From: akacarlyann Date: Mon, 16 Apr 2018 14:12:53 -0700 Subject: [PATCH] Add tests for numeric_map, LinestringViz --- tests/linestrings.geojson | 61 +++++++++++++++++++++++++++++++++++++ tests/test_html.py | 63 +++++++++++++++++++++++++++++++++++++++ tests/test_utils.py | 12 ++++++++ 3 files changed, 136 insertions(+) create mode 100644 tests/linestrings.geojson diff --git a/tests/linestrings.geojson b/tests/linestrings.geojson new file mode 100644 index 0000000..08ce04f --- /dev/null +++ b/tests/linestrings.geojson @@ -0,0 +1,61 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "id": "01", + "properties": {"sample": 50, "width": 1}, + "geometry": { + "type": "LineString", + "coordinates": [ + [-122.4833858013153, 37.829607404976734], + [-122.4830961227417, 37.82932776098012], + [-122.4830746650696, 37.82932776098012], + [-122.48218417167662, 37.82889558180985], + [-122.48218417167662, 37.82890193740421], + [-122.48221099376678, 37.82868372835086], + [-122.4822163581848, 37.82868372835086], + [-122.48205006122589, 37.82801003030873] + ] + } + }, { + "type": "Feature", + "id": "02", + "properties": {"sample": 500, "width": 2}, + "geometry": { + "type": "LineString", + "coordinates": [ + [-122.4833858013153, 37.929607404976734], + [-122.4830961227417, 37.83], + ] + } + }, { + "type": "Feature", + "properties": {"sample": 5000, "width": 1}, + "geometry": { + "type": "LineString", + "coordinates": [ + [-122.48369693756104, 37.83381888486939], + [-122.48348236083984, 37.83317489144141], + [-122.48339653015138, 37.83270036637107], + [-122.48356819152832, 37.832056363179625], + [-122.48404026031496, 37.83114119107971], + [-122.48404026031496, 37.83049717427869], + [-122.48348236083984, 37.829920943955045], + [-122.48356819152832, 37.82954808664175], + [-122.48507022857666, 37.82944639795659], + [-122.48610019683838, 37.82880236636284], + [-122.48695850372314, 37.82931081282506], + [-122.48700141906738, 37.83080223556934], + [-122.48751640319824, 37.83168351665737], + [-122.48803138732912, 37.832158048267786], + [-122.48888969421387, 37.83297152392784], + [-122.48987674713133, 37.83263257682617], + [-122.49043464660643, 37.832937629287755], + [-122.49125003814696, 37.832429207817725], + [-122.49163627624512, 37.832564787218985], + [-122.49223709106445, 37.83337825839438], + [-122.49378204345702, 37.83368330777276] + ] + } + }] +} \ No newline at end of file diff --git a/tests/test_html.py b/tests/test_html.py index 5887474..ce07c3f 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1,6 +1,7 @@ import os import json import base64 +import random from mock import patch @@ -24,6 +25,12 @@ def polygon_data(): return json.loads(fh.read()) +@pytest.fixture() +def linestring_data(): + with open('tests/linestrings.geojson') as fh: + return json.loads(fh.read()) + + TOKEN = 'pk.abc123' @@ -51,6 +58,14 @@ def test_secret_key_ChoroplethViz(polygon_data): ChoroplethViz(polygon_data, access_token=secret) +def test_secret_key_LinestringViz(linestring_data): + """Secret key raises a token error + """ + secret = 'sk.abc123' + with pytest.raises(TokenError): + LinestringViz(linestring_data, access_token=secret) + + def test_token_env_CircleViz(monkeypatch, data): """Viz can get token from environment if not specified """ @@ -77,6 +92,14 @@ def test_token_env_ChoroplethViz(monkeypatch, polygon_data): assert TOKEN in viz.create_html() +def test_token_env_LinestringViz(monkeypatch, linestring_data): + """Viz can get token from environment if not specified + """ + monkeypatch.setenv('MAPBOX_ACCESS_TOKEN', TOKEN) + viz = LinestringViz(linestring_data, color_property="sample") + assert TOKEN in viz.create_html() + + def test_html_color(data): viz = CircleViz(data, color_property="Avg Medicare Payments", @@ -100,6 +123,14 @@ def test_html_ChoroplethViz(polygon_data): assert "" in viz.create_html() +def test_html_LinestringViz(linestring_data): + viz = LinestringViz(linestring_data, + color_property="sample", + color_stops=[[0.0, "red"], [50.0, "gold"], [1000.0, "blue"]], + access_token=TOKEN) + assert "" in viz.create_html() + + @patch('mapboxgl.viz.display') def test_display_CircleViz(display, data): """Assert that show calls the mocked display function @@ -193,6 +224,38 @@ def test_display_vector_ChoroplethViz(display): display.assert_called_once() +@patch('mapboxgl.viz.display') +def test_display_LinestringViz(display, linestring_data): + """Assert that show calls the mocked display function + """ + viz = LinestringViz(linestring_data, + color_property="sample", + color_stops=[[0.0, "red"], [50.0, "gold"], [1000.0, "blue"]], + access_token=TOKEN) + viz.show() + display.assert_called_once() + + +@patch('mapboxgl.viz.display') +def test_display_vector_LinestringViz(display): + """Assert that show calls the mocked display function when using data-join technique + for LinestringViz. + """ + data = [{"elevation": x, "other": random.randint(0,100)} for x in range(0, 21000, 10)] + + viz = LinestringViz(data, + vector_url='mapbox://mapbox.mapbox-terrain-v2', + vector_layer_name='contour', + vector_join_property='ele', + data_join_property='elevation', + color_property="elevation", + color_stops=create_color_stops([0, 50, 100, 500, 1500], colors='YlOrRd'), + access_token=TOKEN + ) + viz.show() + display.assert_called_once() + + @patch('mapboxgl.viz.display') def test_min_zoom(display, data): viz = GraduatedCircleViz(data, diff --git a/tests/test_utils.py b/tests/test_utils.py index 9065707..e06aae0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -15,6 +15,7 @@ def df(): return pd.read_csv('tests/points.csv') + @pytest.fixture() def df_no_properties(): df = pd.read_csv('tests/points.csv') @@ -37,6 +38,7 @@ def test_df_no_properties(df_no_properties): 'features'] assert tuple(features[0]['properties'].keys()) == () + def test_df_geojson_file(df): features = df_to_geojson(df, filename='out.geojson') with open('out.geojson', 'r') as f: @@ -60,26 +62,31 @@ def test_scale_between_maxMin(): scale = scale_between(0,1,1) assert scale == [0,1] + def test_color_stops(): """Create color stops from breaks using colorBrewer""" stops = create_color_stops([0, 1, 2], colors='YlGn') assert stops == [[0,"rgb(247,252,185)"], [1,"rgb(173,221,142)"], [2,"rgb(49,163,84)"]] + def test_color_stops_custom(): """Create color stops from custom color breaks""" stops = create_color_stops([0, 1, 2], colors=['red', 'yellow', 'green']) assert stops == [[0,"red"], [1,"yellow"], [2,"green"]] + def test_color_stops_custom_invalid(): """Create invalid color stops from custom color breaks and throw value error""" with pytest.raises(ValueError): create_color_stops([0, 1, 2], colors=['x', 'yellow', 'green']) + def test_color_stops_custom_null(): """Create invalid number of color stops that do not match the number of breaks""" with pytest.raises(ValueError): create_color_stops([0, 1, 2], colors=['red', 'yellow', 'green', 'grey']) + def test_create_radius_stops(df): domain = [7678.214347826088, 5793.63142857143, 1200] radius_stops = create_radius_stops(domain, 1, 10) @@ -153,3 +160,8 @@ def test_color_map_interp_exact(): assert color_map(0.0, interp_stops, 'rgb(32,32,32)') == 'rgb(255,0,0)' +def test_numeric_map(): + """Map interpolated (or matched) value from numeric stops""" + stops = [[0.0, 0], [50.0, 5000.0], [1000.0, 100000.0]] + assert numeric_map(117.0, stops, default_height=0.0) == 11700.0 +