Skip to content

Commit

Permalink
Add tests for numeric_map, LinestringViz
Browse files Browse the repository at this point in the history
  • Loading branch information
akacarlyann committed Apr 16, 2018
1 parent 03db8aa commit e34ce79
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/linestrings.geojson
Original file line number Diff line number Diff line change
@@ -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]
]
}
}]
}
63 changes: 63 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import json
import base64
import random

from mock import patch

Expand All @@ -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'


Expand Down Expand Up @@ -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
"""
Expand All @@ -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",
Expand All @@ -100,6 +123,14 @@ def test_html_ChoroplethViz(polygon_data):
assert "<html>" 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 "<html>" in viz.create_html()


@patch('mapboxgl.viz.display')
def test_display_CircleViz(display, data):
"""Assert that show calls the mocked display function
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit e34ce79

Please sign in to comment.