summaryrefslogtreecommitdiffstatshomepage
path: root/tests/api/test_pagination.py
blob: 67854c3ba53dd136dff539fcb44d1c2b260e097e (plain) (blame)
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
import pytest
from conftest import DB, Response


@pytest.fixture
def query_pagination(schema_execute):
    query = """
    query artists($pagination: Pagination) {
        artists(pagination: $pagination) {
            __typename
            count
            edges {
                id
            }
        }
    }
    """

    async def _execute(pagination=None):
        return await schema_execute(
            query, {"pagination": pagination} if pagination else None
        )

    return _execute


@pytest.mark.parametrize(
    "pagination,count,length",
    [
        (None, 4, 4),
        ({"items": 3, "page": 1}, 4, 3),
        ({"items": 3, "page": 2}, 4, 1),
        ({"items": 3, "page": 3}, 0, 0),
        ({"items": 10, "page": 1}, 4, 4),
        ({"items": 0, "page": 1}, 0, 0),
        ({"items": 2, "page": 0}, 0, 0),
        ({"items": -1, "page": 0}, 0, 0),
        ({"items": 0, "page": -1}, 0, 0),
    ],
    ids=[
        "is missing and lists all",
        "lists first page",
        "lists last page",
        "lists none (no more items)",
        "lists all",
        "lists none (zero items)",
        "lists none (page zero)",
        "lists none (negative items)",
        "lists none (negative page)",
    ],
)
@pytest.mark.anyio
async def test_pagination(query_pagination, gen_artist, pagination, count, length):
    await DB.add_all(*gen_artist)

    response = Response(await query_pagination(pagination))
    response.assert_is("ArtistFilterResult")

    assert response.count == count
    assert isinstance((response.edges), list)
    assert len(response.edges) == length