Repository navigation
Expand file tree
/
Copy pathtest_utils.py
More file actions
342 lines (318 loc) · 12.2 KB
/
Copy pathtest_utils.py
File metadata and controls
342 lines (318 loc) · 12.2 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import pytest
import qbreader._api_utils as api_utils
from qbreader.types import AlternateSubcategory, Category, Difficulty, Subcategory
from tests import assert_exception, assert_warning
class TestNormalization:
"""Test normalization functions."""
@pytest.mark.parametrize(
"unnormalized, enum_type, expected",
[
(None, Category, ""),
(Difficulty.OPEN, Difficulty, "10"),
("Long Fiction", AlternateSubcategory, "Long Fiction"),
("0", Difficulty, "0"),
(1, Difficulty, "1"),
([Category.FINE_ARTS, Category.RELIGION], Category, "Religion,Fine Arts"),
(
["Religion", "Pop Culture", "Mythology"],
Category,
"Mythology,Pop Culture,Religion",
),
(["0", "1", "2"], Difficulty, "2,1,0"),
([0, 1, 2], Difficulty, "1,2,0"),
(["History", "History"], Category, "History"),
("Science", Subcategory, "Science"),
],
)
def test_normalize_enumlike(self, unnormalized, enum_type, expected):
result = api_utils.normalize_enumlike(unnormalized, enum_type)
sorted_result = sorted(result.split(","))
sorted_expected = sorted(expected.split(","))
assert sorted_result == sorted_expected
@pytest.mark.parametrize(
"unnormalized, enum_type, exception",
[
(True, Category, TypeError),
(3.5, Category, TypeError),
(["Social Science", 3.5], Category, TypeError),
],
)
def test_normalize_enumlike_exception(self, unnormalized, enum_type, exception):
assert_exception(
api_utils.normalize_enumlike, exception, unnormalized, enum_type
)
@pytest.mark.parametrize(
"unnormalized, enum_type, expected, warning",
[
(11, Difficulty, "", UserWarning),
("0.5", Difficulty, "", UserWarning),
("History", AlternateSubcategory, "", UserWarning),
(AlternateSubcategory.JAZZ, Category, "", UserWarning),
(
[Category.SCIENCE, Category.FINE_ARTS, Subcategory.MUSIC],
Category,
"Science,Fine Arts",
UserWarning,
),
([Difficulty.OPEN, 999], Difficulty, "10", UserWarning),
],
)
def test_normalize_enumlike_warning(
self, unnormalized, enum_type, expected, warning
):
result = assert_warning(
api_utils.normalize_enumlike, warning, unnormalized, enum_type
)
sorted_result = sorted(result.split(","))
sorted_expected = sorted(expected.split(","))
assert sorted_result == sorted_expected
@pytest.mark.parametrize(
"boolean, expected",
[
(True, "true"),
(False, "false"),
("true", "true"),
("false", "false"),
],
)
def test_normalize_bool(self, boolean, expected):
assert api_utils.normalize_bool(boolean) == expected
@pytest.mark.parametrize(
"boolean, exception", [(None, TypeError), (1, TypeError), ("1", ValueError)]
)
def test_normalize_bool_exception(self, boolean, exception):
assert_exception(api_utils.normalize_bool, exception, boolean)
@pytest.mark.parametrize(
"diff, expected",
[
(Difficulty.HS_REGS, "3"),
(10, "10"),
("2", "2"),
([Difficulty.HS_REGS], "3"),
([Difficulty.HS_REGS, Difficulty.HS_HARD], "3,4"),
([Difficulty.HS_REGS, "3", "4"], "3,4"),
(["3", "4"], "3,4"),
(["1", "4", 7], "1,4,7"),
(["3", "2", 5, Difficulty.HS_HARD], "2,3,4,5"),
(list(range(11)), "0,1,2,3,4,5,6,7,8,9,10"),
(None, ""),
([], ""),
],
)
def test_normalize_diff(self, diff, expected):
assert set(api_utils.normalize_diff(diff).split(",")) == set(
expected.split(",")
) # contains the same elements
@pytest.mark.parametrize(
"diff, exception",
[
(3.14, TypeError),
(3 + 4j, TypeError),
(True, TypeError),
([3.14], TypeError),
],
)
def test_normalize_diff_exception(self, diff, exception):
assert_exception(api_utils.normalize_diff, exception, diff)
@pytest.mark.parametrize(
"diff, warning",
[
("3.14", UserWarning),
("11", UserWarning),
(["-1"], UserWarning),
(1000, UserWarning),
],
)
def test_normalize_diff_warning(self, diff, warning):
assert assert_warning(api_utils.normalize_diff, warning, diff) == ""
@pytest.mark.parametrize(
"cat, expected",
[
(Category.SCIENCE, "Science"),
(Category.SCIENCE.value, "Science"),
("Science", "Science"),
(["Science"], "Science"),
([Category.SCIENCE], "Science"),
([Category.SCIENCE, Category.LITERATURE], "Science,Literature"),
([Category.SCIENCE, "Literature"], "Science,Literature"),
(["Science", "Literature"], "Science,Literature"),
(["Science", "Literature", "Literature"], "Science,Literature"),
(
[
"Science",
"Literature",
"Literature",
Category.SCIENCE,
Category.HISTORY,
],
"Science,Literature,History",
),
],
)
def test_normalize_cat(self, cat, expected):
assert set(api_utils.normalize_cat(cat).split(",")) == set(expected.split(","))
@pytest.mark.parametrize(
"cat, exception",
[
(3.14, TypeError),
(3 + 4j, TypeError),
(True, TypeError),
([3.14], TypeError),
],
)
def test_normalize_cat_exception(self, cat, exception):
assert_exception(api_utils.normalize_cat, exception, cat)
@pytest.mark.parametrize(
"cat, warning",
[
("3.14", UserWarning),
("11", UserWarning),
(["-1"], UserWarning),
(1000, UserWarning),
],
)
def test_normalize_cat_warning(self, cat, warning):
assert assert_warning(api_utils.normalize_cat, warning, cat) == ""
@pytest.mark.parametrize(
"subcat, expected",
[ # reused because subcat is a superset of cat
(Subcategory.SCIENCE, "Science"),
(Subcategory.SCIENCE.value, "Science"),
("Science", "Science"),
(["Science"], "Science"),
([Subcategory.SCIENCE], "Science"),
([Subcategory.SCIENCE, Subcategory.LITERATURE], "Science,Literature"),
([Subcategory.SCIENCE, "Literature"], "Science,Literature"),
(["Science", "Literature"], "Science,Literature"),
(["Science", "Literature", "Literature"], "Science,Literature"),
(
[
"Science",
"Literature",
"Literature",
Subcategory.SCIENCE,
Subcategory.HISTORY,
],
"Science,Literature,History",
),
],
)
def test_normalize_subcat(self, subcat, expected):
assert set(api_utils.normalize_subcat(subcat).split(",")) == set(
expected.split(",")
)
@pytest.mark.parametrize(
"subcat, exception",
[
(3.14, TypeError),
(3 + 4j, TypeError),
(True, TypeError),
([3.14], TypeError),
],
)
def test_normalize_subcat_exception(self, subcat, exception):
assert_exception(api_utils.normalize_subcat, exception, subcat)
@pytest.mark.parametrize(
"subcat, warning",
[
("3.14", UserWarning),
("11", UserWarning),
(["-1"], UserWarning),
(1000, UserWarning),
],
)
def test_normalize_subcat_warning(self, subcat, warning):
assert assert_warning(api_utils.normalize_cat, warning, subcat) == ""
@pytest.mark.parametrize(
"cats, subcats, alt_subcats, expected",
[
(None, None, None, ("", "", "")),
([], [], [], ("", "", "")),
(None, None, "Beliefs", ("", "", "Beliefs")),
(None, None, "Drama", ("Literature", "", "Drama")),
(None, None, "Jazz", ("", "Other Fine Arts", "Jazz")),
("History", None, None, ("History", "", "")),
(["History", "Literature"], None, None, ("History,Literature", "", "")),
(None, "Other Science", None, ("", "Other Science", "")),
(None, ["Other Science", "Sports"], None, ("", "Other Science,Sports", "")),
(
None,
None,
["Astronomy", "Earth Science"],
("", "Other Science", "Astronomy,Earth Science"),
),
(
["Social Science"],
["Television"],
["Beliefs", "Drama", "Misc Science"],
(
"Social Science,Literature",
"Television,Other Science",
"Beliefs,Drama,Misc Science",
),
),
(["History", "History", "History", "History"], [], [], ("History", "", "")),
],
)
def test_normalize_cats(self, cats, subcats, alt_subcats, expected):
result = api_utils.normalize_cats(cats, subcats, alt_subcats)
sorted_result = [sorted(x.split(",")) for x in result]
sorted_expected = [sorted(x.split(",")) for x in expected]
assert sorted_result == sorted_expected
@pytest.mark.parametrize(
"cats, subcats, alt_subcats, exception",
[
(None, None, "", ValueError),
("Science ", None, [], ValueError),
("Geography", [None], [], ValueError),
(["History", ""], [], [], ValueError),
],
)
def test_normalize_cats_exception(self, cats, subcats, alt_subcats, exception):
assert_exception(
api_utils.normalize_cats, exception, cats, subcats, alt_subcats
)
class TestMiscUtil:
"""Test miscellaneous util functions"""
@pytest.mark.parametrize(
"dictionary, expected",
[
({"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 2, "c": 3}),
({"a": 1, "b": None, "c": 3}, {"a": 1, "c": 3}),
({"a": 1, "b": None, None: ""}, {"a": 1}),
(
{"a": 1, "b": None, None: None, "c": True, "d": False},
{"a": 1, "c": True, "d": False},
),
],
)
def test_prune_none(self, dictionary, expected):
assert api_utils.prune_none(dictionary) == expected
@pytest.mark.parametrize(
"typed_alt_subcat, expected",
[
(AlternateSubcategory.ASTRONOMY, (None, Subcategory.OTHER_SCIENCE)),
(AlternateSubcategory.ARCHITECTURE, (None, Subcategory.OTHER_FINE_ARTS)),
(AlternateSubcategory.ANTHROPOLOGY, (None, Subcategory.SOCIAL_SCIENCE)),
(AlternateSubcategory.DRAMA, (Category.LITERATURE, None)),
(AlternateSubcategory.BELIEFS, (None, None)),
(AlternateSubcategory.PRACTICES, (None, None)),
(AlternateSubcategory.MISC_ARTS, (None, Subcategory.OTHER_FINE_ARTS)),
(AlternateSubcategory.MISC_LITERATURE, (Category.LITERATURE, None)),
(AlternateSubcategory.MISC_SCIENCE, (None, Subcategory.OTHER_SCIENCE)),
],
)
def test_category_correspondence(self, typed_alt_subcat, expected):
assert api_utils.category_correspondence(typed_alt_subcat) == expected
@pytest.mark.parametrize(
"typed_alt_subcat, exception",
[
("hey this isn't an alternate subca", ValueError),
(15, ValueError),
([AlternateSubcategory.ASTRONOMY], ValueError),
({AlternateSubcategory.MATH, AlternateSubcategory.FILM}, ValueError),
((AlternateSubcategory.DRAMA, AlternateSubcategory.JAZZ), ValueError),
],
)
def test_category_correspondence_exception(self, typed_alt_subcat, exception):
assert_exception(api_utils.category_correspondence, exception, typed_alt_subcat)