summaryrefslogtreecommitdiff
path: root/tests/test_srht_client.py
diff options
context:
space:
mode:
authorChristian Cleberg <[email protected]>2026-04-09 19:45:45 -0500
committerChristian Cleberg <[email protected]>2026-04-09 19:45:45 -0500
commitacbff854f2da96bddcaede1385e7fefeba0fb34b (patch)
tree48d4707cd3276d2825370f0793008a6ffcfff936 /tests/test_srht_client.py
downloadhutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.tar.gz
hutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.tar.bz2
hutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.zip
initial commit
Diffstat (limited to 'tests/test_srht_client.py')
-rw-r--r--tests/test_srht_client.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/test_srht_client.py b/tests/test_srht_client.py
new file mode 100644
index 0000000..e7ece0d
--- /dev/null
+++ b/tests/test_srht_client.py
@@ -0,0 +1,56 @@
+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()