Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ async def test_query_min_max_year_range(self, qbr):
},
ValueError,
),
(
{
"min_year": "not an int",
},
TypeError,
),
(
{
"max_year": "not an int",
},
TypeError,
),
],
)
async def test_query_exception(
Expand Down
158 changes: 154 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,78 @@
import pytest

import qbreader._api_utils as api_utils
from qbreader.types import Category, Difficulty, Subcategory
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",
[
Expand Down Expand Up @@ -177,7 +242,62 @@ def test_normalize_subcat_warning(self, subcat, warning):
assert assert_warning(api_utils.normalize_cat, warning, subcat) == ""

@pytest.mark.parametrize(
"dict, expected",
"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}),
Expand All @@ -188,5 +308,35 @@ def test_normalize_subcat_warning(self, subcat, warning):
),
],
)
def test_prune_none(self, dict, expected):
assert api_utils.prune_none(dict) == expected
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)
Loading