aboutsummaryrefslogtreecommitdiff
path: root/tests/test_srht_client.py
blob: b4586e59e18ec0c689676b313472ca41e024637a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import httpx
import pytest

from srht_contrib.services.srht_client import SourceHutClientError, SourceHutGraphQLClient


def test_graphql_client_retries_http_5xx_and_succeeds() -> None:
    attempts = {"count": 0}

    def handler(request: httpx.Request) -> httpx.Response:
        attempts["count"] += 1
        if attempts["count"] == 1:
            return httpx.Response(502, json={"error": "bad gateway"})
        return httpx.Response(200, json={"data": {"ok": True}})

    client = SourceHutGraphQLClient(
        "https://todo.sr.ht/query",
        "token",
        transport=httpx.MockTransport(handler),
    )

    data = client.execute("query Ping { ping }")

    assert data == {"ok": True}
    assert attempts["count"] == 2
    client.close()


def test_graphql_client_raises_for_graphql_errors() -> None:
    client = SourceHutGraphQLClient(
        "https://todo.sr.ht/query",
        "token",
        transport=httpx.MockTransport(lambda request: httpx.Response(200, json={"errors": [{"message": "nope"}]})),
    )

    with pytest.raises(SourceHutClientError):
        client.execute("query Ping { ping }")

    client.close()


def test_graphql_client_raises_for_network_errors() -> None:
    def handler(request: httpx.Request) -> httpx.Response:
        raise httpx.ConnectError("offline", request=request)

    client = SourceHutGraphQLClient(
        "https://todo.sr.ht/query",
        "token",
        max_retries=0,
        transport=httpx.MockTransport(handler),
    )

    with pytest.raises(SourceHutClientError):
        client.execute("query Ping { ping }")

    client.close()


def test_graphql_client_applies_request_delay_and_retry_backoff(monkeypatch: pytest.MonkeyPatch) -> None:
    attempts = {"count": 0}
    sleeps: list[float] = []

    def handler(request: httpx.Request) -> httpx.Response:
        attempts["count"] += 1
        if attempts["count"] < 3:
            return httpx.Response(502, json={"error": "bad gateway"})
        return httpx.Response(200, json={"data": {"ok": True}})

    monkeypatch.setattr("srht_contrib.services.srht_client.time.sleep", sleeps.append)
    client = SourceHutGraphQLClient(
        "https://todo.sr.ht/query",
        "token",
        max_retries=2,
        request_delay=0.5,
        transport=httpx.MockTransport(handler),
    )

    data = client.execute("query Ping { ping }")

    assert data == {"ok": True}
    assert sleeps == [0.5, 1, 2]
    client.close()