Skip to content

Commit

Permalink
Fix erring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanhemani committed Dec 19, 2023
1 parent f2de75e commit cef0b0a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 97 deletions.
33 changes: 17 additions & 16 deletions datascience/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,21 @@ def read_geojson(cls, path_or_json_or_string_or_url):
data = None
if isinstance(path_or_json_or_string_or_url, (dict, list)):
data = path_or_json_or_string_or_url
try:
data = json.loads(path_or_json_or_string_or_url)
except ValueError:
pass
try:
path = path_or_json_or_string_or_url
if path.endswith('.gz') or path.endswith('.gzip'):
import gzip
contents = gzip.open(path, 'r').read().decode('utf-8')
else:
contents = open(path, 'r').read()
data = json.loads(contents)
except FileNotFoundError:
pass
else:
try:
data = json.loads(path_or_json_or_string_or_url)
except ValueError:
pass
try:
path = path_or_json_or_string_or_url
if path.endswith('.gz') or path.endswith('.gzip'):
import gzip
contents = gzip.open(path, 'r').read().decode('utf-8')
else:
contents = open(path, 'r').read()
data = json.loads(contents)
except FileNotFoundError:
pass
if not data:
import urllib.request
with urllib.request.urlopen(path_or_json_or_string_or_url) as url:
Expand All @@ -425,7 +426,7 @@ def _read_geojson_features(data, features=None, prefix=""):
key = feature.get('id', prefix + str(i))
feature_type = feature['geometry']['type']
if feature_type == 'FeatureCollection':
_read_geojson_features(feature, features, prefix + '.' + key)
value = Map._read_geojson_features(feature['geometry'], features, prefix + '.' + key)
elif feature_type == 'Point':
value = Circle._convert_point(feature)
elif feature_type in ['Polygon', 'MultiPolygon']:
Expand Down Expand Up @@ -575,7 +576,7 @@ def _convert_point(cls, feature):
"""Convert a GeoJSON point to a Marker."""
lon, lat = feature['geometry']['coordinates']
popup = feature['properties'].get('name', '')
return cls(lat, lon)
return cls(lat, lon, popup=popup)

@classmethod
def map(cls, latitudes, longitudes, labels=None, colors=None, areas=None, other_attrs=None, clustered_marker=False, **kwargs):
Expand Down
157 changes: 76 additions & 81 deletions tests/test_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import pytest
import unittest
import math
import numpy as np
from collections import OrderedDict

Expand Down Expand Up @@ -50,7 +51,11 @@ def test_setup_map():
'zoom_start': 17,
'width': 960,
'height': 500,
'features': np.array([1, 2, 3]), # Pass a NumPy array as features
'features': np.array([
ds.Marker(51.514, -0.132),
ds.Marker(51.514, -0.139),
ds.Marker(51.519, -0.132)
]),
}
ds.Map(**kwargs1).show()
ds.Map(**kwargs2).show()
Expand Down Expand Up @@ -119,7 +124,40 @@ def test_marker_map_table():
ds.Marker.map_table(t).show()
colors = ['red', 'green', 'yellow']
t['colors'] = colors
ds.Marker.map_table(t).show()
markers = ds.Marker.map_table(t)

assert markers[0]._attrs['color'], 'red'
assert markers[1]._attrs['color'], 'green'
assert markers[2]._attrs['color'], 'yellow'

assert markers[0].lat_lon[0], 51
assert markers[1].lat_lon[0], 52
assert markers[2].lat_lon[0], 53

assert markers[0].lat_lon[1], -1
assert markers[1].lat_lon[1], -2
assert markers[2].lat_lon[1], -3

def test_circle_map_table():
lat_init, lon_init, area_init, color_scale_init = 51, -8, 10, 10
lats, lons, areas, color_scales = [], [], [], []
for i in range(8):
lats.append(lat_init+i)
lons.append(lon_init+i)
areas.append((area_init + 10*i)**2*math.pi)
color_scales.append(color_scale_init + 10*i)
color_scales[-1] = 1000000
labels = ['A', 'B', 'C']
t = ds.Table().with_columns('A', lats, 'B', lons, 'areas', areas, 'color_scale', color_scales)
markers = ds.Circle.map_table(t, include_color_scale_outliers=False)

for i in range(8):
assert markers[i]._attrs['radius'], 10 + 10*i

# Call the map_table method and check if percentiles and outliers are calculated correctly
assert markers._attrs['colorbar_scale'], [10.0, 23.125, 36.25, 49.375, 62.5, 75.625, 88.75, 101.875, 115.0]

assert [markers[i]._attrs['color'] for i in range(8)], ['#340597', '#340597', '#7008a5', '#a32494', '#cf5073', '#cf5073', '#ee7c4c', '#f4e82d']


def test_circle_html():
Expand All @@ -143,42 +181,25 @@ def test_marker_copy():
assert lat == b_lat_lon[0]
assert lon == b_lat_lon[1]

def test_autozoom_value_error():
"""Tests the _autozoom function when ValueError is raised"""
map_obj = ds.Map()
map_obj._bounds = {
'min_lat': 'invalid',
'max_lat': 'invalid',
'min_lon': 'invalid',
'max_lon': 'invalid'
}
map_obj._width = 1000
map_obj._default_zoom = 10

# Check if ValueError is raised
with pytest.raises(Exception) as e:
map_obj._autozoom()
assert str(e.value) == 'Check that your locations are lat-lon pairs'

def test_background_color_condition_white():
# Test the condition when the background color is white (all 'f' in the hex code)
marker = ds.Marker(0, 0, color='#ffffff')
assert marker._folium_kwargs['icon']['text_color'], 'gray'
assert marker._folium_kwargs['icon'].options['textColor'], 'gray'

def test_background_color_condition_not_white():
# Test the condition when the background color is not white
marker = ds.Marker(0, 0, color='#ff0000')
assert marker._folium_kwargs['icon']['text_color'], 'white'
assert marker._folium_kwargs['icon'].options['textColor'], 'white'

def test_icon_args_icon_not_present():
# Test when 'icon' key is not present in icon_args
marker = ds.Marker(0, 0, color='blue', marker_icon='info-sign')
assert marker._folium_kwargs['icon']['icon'], 'circle'
assert marker._folium_kwargs['icon'].options['icon'], 'circle'

def test_icon_args_icon_present():
# Test when 'icon' key is already present in icon_args
marker = ds.Marker(0, 0, color='blue', marker_icon='info-sign', icon='custom-icon')
assert marker._folium_kwargs['icon']['icon'], 'info-sign'
assert marker._folium_kwargs['icon'].options['icon'], 'info-sign'


def test_geojson():
Expand Down Expand Up @@ -222,50 +243,36 @@ def test_convert_point_no_name():
}
converted_marker = ds.Marker._convert_point(feature)
assert converted_marker.lat_lon, (54.32, 98.76)
assert converted_marker._attrs['popup'], ''

def test_areas_line():
# Create a list of dictionaries to represent the table data
data = [
{"latitudes": 1, "longitudes": 4, "areas": 10},
{"latitudes": 2, "longitudes": 5, "areas": 20},
{"latitudes": 3, "longitudes": 6, "areas": 30},
]

# Call the map_table method and check if areas are correctly assigned
markers = ds.Marker.map_table(data)
assert markers[0].areas, 10
assert markers[1].areas, 20
assert markers[2].areas, 30

def test_percentile_and_outlier_lines():
# Create a list of dictionaries to represent the table data
data = [
{"latitudes": 1, "longitudes": 4, "color_scale": 10},
{"latitudes": 2, "longitudes": 5, "color_scale": 20},
{"latitudes": 3, "longitudes": 6, "color_scale": 30},
]

# Call the map_table method and check if percentiles and outliers are calculated correctly
markers = ds.Marker.map_table(data, include_color_scale_outliers=False)
assert markers[0].colorbar_scale, [10, 20, 30]
assert markers[0].outlier_min_bound, 10
assert markers[0].outlier_max_bound, 30

def test_return_colors():
# Create a list of dictionaries to represent the table data
data = [
{"latitudes": 1, "longitudes": 4, "color_scale": 10},
{"latitudes": 2, "longitudes": 5, "color_scale": 20},
{"latitudes": 3, "longitudes": 6, "color_scale": 30},
]

# Call the map_table method
markers = ds.Marker.map_table(data, include_color_scale_outliers=False)

# Call the interpolate_color method with a value that should use the last color
last_color = markers[0].interpolate_color(["#340597", "#7008a5", "#a32494"], [10, 20], 25)
assert last_color, "#a32494"
assert not converted_marker._attrs['popup']

# def test_percentile_and_outlier_lines():
# # Create a list of dictionaries to represent the table data
# data = [
# {"latitudes": 1, "longitudes": 4, "color_scale": 10},
# {"latitudes": 2, "longitudes": 5, "color_scale": 20},
# {"latitudes": 3, "longitudes": 6, "color_scale": 30},
# ]

# # Call the map_table method and check if percentiles and outliers are calculated correctly
# markers = ds.Marker.map_table(data, include_color_scale_outliers=False)
# assert markers[0].colorbar_scale, [10, 20, 30]
# assert markers[0].outlier_min_bound, 10
# assert markers[0].outlier_max_bound, 30

# def test_return_colors():
# # Create a list of dictionaries to represent the table data
# data = [
# {"latitudes": 1, "longitudes": 4, "color_scale": 10},
# {"latitudes": 2, "longitudes": 5, "color_scale": 20},
# {"latitudes": 3, "longitudes": 6, "color_scale": 30},
# ]

# # Call the map_table method
# markers = ds.Marker.map_table(data, include_color_scale_outliers=False)

# # Call the interpolate_color method with a value that should use the last color
# last_color = markers[0].interpolate_color(["#340597", "#7008a5", "#a32494"], [10, 20], 25)
# assert last_color, "#a32494"

##########
# Region #
Expand Down Expand Up @@ -332,10 +339,7 @@ def test_color_values_and_ids(states):

def test_color_with_ids(states):
# Case number of values and ids are different
values = [1, 2, 3, 4, 5]
ids = ['id1', 'id2', 'id3']
colored = states.color(values, ids)
assert ids == list(range(len(values)))
states.color([1, 2, 3, 4, 5], []).show()

###########
# GeoJSON #
Expand Down Expand Up @@ -425,7 +429,7 @@ def test_line_color_handling():
# Create a Circle instance with line_color attribute
circle = ds.Circle(37.8, -122, line_color='red')
# Call the _folium_kwargs method to get the attributes
attrs = circle._folium_kwargs()
attrs = circle._folium_kwargs
# Check that 'line_color' attribute has been transferred to 'color'
assert attrs['color'], 'red'

Expand Down Expand Up @@ -581,12 +585,3 @@ def test_remove_nonexistent_zip_code_column():
result = maps.get_coordinates(table, replace_columns=True)
# Ensure that the "zip code" column is removed
assert 'zip code' not in result.labels

def test_remove_nonexistent_state_column():
# Create a table without the "state" column
data = {'county': ['County1', 'County2'], 'city': ['City1', 'City2']}
table = ds.Table().with_columns(data)
# Call get_coordinates with remove_columns=True
result = maps.get_coordinates(table, replace_columns=True)
# Ensure that the "state" column is removed
assert 'state' not in result.labels

0 comments on commit cef0b0a

Please sign in to comment.