diff options
Diffstat (limited to 'tests/api/test_sort.py')
-rw-r--r-- | tests/api/test_sort.py | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/tests/api/test_sort.py b/tests/api/test_sort.py new file mode 100644 index 0000000..b3c8562 --- /dev/null +++ b/tests/api/test_sort.py @@ -0,0 +1,137 @@ +import pytest +from conftest import DB, Response +from hircine.db.models import Namespace + + +@pytest.fixture +def query_comic_sort(execute_sort): + query = """ + query comics($sort: ComicSortInput) { + comics(sort: $sort) { + __typename + count + edges { + id + title + } + } + } + """ + + return execute_sort(query) + + +@pytest.fixture +def query_namespace_sort(execute_sort): + query = """ + query namespaces($sort: NamespaceSortInput) { + namespaces(sort: $sort) { + __typename + count + edges { + id + name + } + } + } + """ + + return execute_sort(query) + + +@pytest.mark.parametrize( + "sort,reverse", + [ + ({"on": "DATE"}, False), + ({"on": "DATE", "direction": "DESCENDING"}, True), + ({"on": "DATE", "direction": "ASCENDING"}, False), + ], + ids=[ + "ascending (default)", + "descending", + "ascending", + ], +) +@pytest.mark.anyio +async def test_query_comics_sort_date(gen_comic, query_comic_sort, sort, reverse): + comics = await DB.add_all(*gen_comic) + ids = [c.id for c in sorted(comics, key=lambda c: c.date, reverse=reverse)] + + response = Response(await query_comic_sort(sort)) + response.assert_is("ComicFilterResult") + + assert ids == [edge["id"] for edge in response.edges] + + +@pytest.mark.parametrize( + "sort,reverse", + [ + ({"on": "TAG_COUNT"}, False), + ({"on": "TAG_COUNT", "direction": "DESCENDING"}, True), + ({"on": "TAG_COUNT", "direction": "ASCENDING"}, False), + ], + ids=[ + "ascending (default)", + "descending", + "ascending", + ], +) +@pytest.mark.anyio +async def test_query_comics_sort_tag_count(gen_comic, query_comic_sort, sort, reverse): + comics = await DB.add_all(*gen_comic) + ids = [c.id for c in sorted(comics, key=lambda c: len(c.tags), reverse=reverse)] + + response = Response(await query_comic_sort(sort)) + response.assert_is("ComicFilterResult") + + assert ids == [edge["id"] for edge in response.edges] + + +@pytest.mark.anyio +async def test_query_comics_sort_random(gen_comic, query_comic_sort): + comics = await DB.add_all(*gen_comic) + ids = set([c.id for c in comics]) + + response = Response(await query_comic_sort({"on": "RANDOM"})) + response.assert_is("ComicFilterResult") + + assert ids == set(edge["id"] for edge in response.edges) + + +@pytest.mark.anyio +async def test_query_comics_sort_random_seed_direction(gen_comic, query_comic_sort): + comics = await DB.add_all(*gen_comic) + ids = set([c.id for c in comics]) + + response = Response( + await query_comic_sort( + {"on": "RANDOM", "seed": 42069, "direction": "ASCENDING"} + ) + ) + response.assert_is("ComicFilterResult") + + ascending_ids = [edge["id"] for edge in response.edges] + + assert ids == set(ascending_ids) + + response = Response( + await query_comic_sort( + {"on": "RANDOM", "seed": 42069, "direction": "DESCENDING"} + ) + ) + response.assert_is("ComicFilterResult") + + descending_ids = [edge["id"] for edge in response.edges] + + assert ascending_ids == descending_ids[::-1] + + +@pytest.mark.anyio +async def test_query_namespace_sort_sort_name(query_namespace_sort): + await DB.add(Namespace(name="one", sort_name="2")) + await DB.add(Namespace(name="two", sort_name="1")) + + response = Response(await query_namespace_sort({"on": "SORT_NAME"})) + response.assert_is("NamespaceFilterResult") + + assert ["two", "one"] == [edge["name"] for edge in response.edges] |