-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtest_utilities.py
More file actions
278 lines (235 loc) · 7.15 KB
/
test_utilities.py
File metadata and controls
278 lines (235 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import numpy as np
import pandas as pd
import pytest
from js_loader import JsCode as external_JsCode
from folium import FeatureGroup, Map, Marker, Popup
from folium.utilities import (
JsCode,
TypeJsCode,
_is_url,
camelize,
deep_copy,
escape_double_quotes,
get_obj_in_upper_tree,
if_pandas_df_convert_to_numpy,
javascript_identifier_path_to_array_notation,
normalize_bounds_type,
parse_font_size,
parse_options,
validate_location,
validate_locations,
validate_multi_locations,
)
@pytest.mark.parametrize(
"location",
[
(5, 3),
[5.0, 3.0],
np.array([5, 3]),
np.array([[5, 3]]),
pd.Series([5, 3]),
pd.DataFrame([5, 3]),
pd.DataFrame([[5, 3]]),
("5.0", "3.0"),
("5", "3"),
],
)
def test_validate_location(location):
outcome = validate_location(location)
assert outcome == [5.0, 3.0]
@pytest.mark.parametrize(
"location",
[
None,
[None, None],
(),
[0],
["hi"],
"hi",
("lat", "lon"),
Marker,
(Marker, Marker),
(3.0, np.nan),
{3.0, 5.0},
{"lat": 5.0, "lon": 3.0},
range(4),
[0, 1, 2],
[(0,), (1,)],
],
)
def test_validate_location_exceptions(location):
"""Test input that should raise an exception."""
with pytest.raises((TypeError, ValueError)):
validate_location(location)
@pytest.mark.parametrize(
"locations",
[
[(0, 5), (1, 6), (2, 7)],
[[0, 5], [1, 6], [2, 7]],
np.array([[0, 5], [1, 6], [2, 7]]),
pd.DataFrame([[0, 5], [1, 6], [2, 7]]),
],
)
def test_validate_locations(locations):
outcome = validate_locations(locations)
assert outcome == [[0.0, 5.0], [1.0, 6.0], [2.0, 7.0]]
@pytest.mark.parametrize(
"locations",
[
[[(0, 5), (1, 6), (2, 7)], [(3, 8), (4, 9)]],
],
)
def test_validate_multi_locations(locations):
outcome = validate_multi_locations(locations)
assert outcome == [[[0, 5], [1, 6], [2, 7]], [[3, 8], [4, 9]]]
@pytest.mark.parametrize(
"locations",
[
None,
[None, None],
(),
[0],
["hi"],
"hi",
("lat", "lon"),
Marker,
(Marker, Marker),
(3.0, np.nan),
{3.0, 5.0},
{"lat": 5.0, "lon": 3.0},
range(4),
[0, 1, 2],
[(0,), (1,)],
],
)
def test_validate_locations_exceptions(locations):
"""Test input that should raise an exception."""
with pytest.raises((TypeError, ValueError)):
validate_locations(locations)
def test_if_pandas_df_convert_to_numpy():
data = [[0, 5, "red"], [1, 6, "blue"], [2, 7, "something"]]
df = pd.DataFrame(data, columns=["lat", "lng", "color"])
res = if_pandas_df_convert_to_numpy(df)
assert isinstance(res, np.ndarray)
expected = np.array(data)
assert all(
[
[all([i == j]) for i, j in zip(row1, row2)]
for row1, row2 in zip(res, expected)
]
)
# Also check if it ignores things that are not Pandas DataFrame:
assert if_pandas_df_convert_to_numpy(data) is data
assert if_pandas_df_convert_to_numpy(expected) is expected
@pytest.mark.parametrize(
"bounds, expected",
[
([[1, 2], [3, 4]], [[1.0, 2.0], [3.0, 4.0]]),
([[None, 2], [3, None]], [[None, 2.0], [3.0, None]]),
([[1.1, 2.2], [3.3, 4.4]], [[1.1, 2.2], [3.3, 4.4]]),
([[None, None], [None, None]], [[None, None], [None, None]]),
([[0, -1], [-2, 3]], [[0.0, -1.0], [-2.0, 3.0]]),
],
)
def test_normalize_bounds_type(bounds, expected):
assert normalize_bounds_type(bounds) == expected
def test_camelize():
assert camelize("variable_name") == "variableName"
assert camelize("variableName") == "variableName"
assert camelize("name") == "name"
assert camelize("very_long_variable_name") == "veryLongVariableName"
def test_deep_copy():
m = Map()
fg = FeatureGroup().add_to(m)
Marker(location=(0, 0)).add_to(fg)
m_copy = deep_copy(m)
def check(item, item_copy):
assert type(item) is type(item_copy)
assert item._name == item_copy._name
for attr in item.__dict__.keys():
if not attr.startswith("_"):
assert getattr(item, attr) == getattr(item_copy, attr)
assert item is not item_copy
assert item._id != item_copy._id
for child, child_copy in zip(
item._children.values(), item_copy._children.values()
):
check(child, child_copy)
check(m, m_copy)
def test_get_obj_in_upper_tree():
m = Map()
fg = FeatureGroup().add_to(m)
marker = Marker(location=(0, 0)).add_to(fg)
assert get_obj_in_upper_tree(marker, FeatureGroup) is fg
assert get_obj_in_upper_tree(marker, Map) is m
# The search should only go up, not down:
with pytest.raises(ValueError):
assert get_obj_in_upper_tree(fg, Marker)
with pytest.raises(ValueError):
assert get_obj_in_upper_tree(marker, Popup)
def test_parse_options():
assert parse_options(thing=42) == {"thing": 42}
assert parse_options(thing=None) == {}
assert parse_options(long_thing=42) == {"longThing": 42}
assert parse_options(thing=42, lst=[1, 2]) == {"thing": 42, "lst": [1, 2]}
@pytest.mark.parametrize(
"url",
[
"https://example.com/img.png",
"http://example.com/img.png",
"ftp://example.com/img.png",
"file:///t.jpg",
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
],
)
def test_is_url(url):
assert _is_url(url) is True
@pytest.mark.parametrize(
"text,result",
[
("bla", "bla"),
('bla"bla', r"bla\"bla"),
('"bla"bla"', r"\"bla\"bla\""),
],
)
def test_escape_double_quotes(text, result):
assert escape_double_quotes(text) == result
@pytest.mark.parametrize(
"text,result",
[
("bla", '["bla"]'),
("obj-1.obj2", '["obj-1"]["obj2"]'),
('obj-1.obj"2', r'["obj-1"]["obj\"2"]'),
],
)
def test_javascript_identifier_path_to_array_notation(text, result):
assert javascript_identifier_path_to_array_notation(text) == result
def test_js_code_init_str():
js_code = JsCode("hi")
assert isinstance(js_code, JsCode)
assert isinstance(js_code.js_code, str)
def test_js_code_init_js_code():
js_code = JsCode("hi")
js_code_2 = JsCode(js_code)
assert isinstance(js_code_2, JsCode)
assert isinstance(js_code_2.js_code, str)
def test_external_js_code():
js_code = external_JsCode("hi")
assert isinstance(js_code, TypeJsCode)
@pytest.mark.parametrize(
"value,expected",
[
(10, "10px"),
(12.5, "12.5px"),
("1rem", "1rem"),
("1em", "1em"),
],
)
def test_parse_font_size_valid(value, expected):
assert parse_font_size(value) == expected
invalid_values = ["1", "1unit"]
expected_errors = "The font size must be expressed in rem, em, or px."
@pytest.mark.parametrize("value,error_message", zip(invalid_values, expected_errors))
def test_parse_font_size_invalid(value, error_message):
with pytest.raises(ValueError, match=error_message):
parse_font_size(value)