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
|
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]
|