feat(tests): implement dynamic GraphQL response handling for tests

This commit is contained in:
shay
2026-05-04 12:56:52 -04:00
parent 4a1d31ffdd
commit 673eb5b39d
3 changed files with 118 additions and 116 deletions
+32 -22
View File
@@ -32,31 +32,41 @@ def test_endpoints_require_internal_token(client):
@resp_lib.activate
def test_data_auto_fetches_on_miss(client, monkeypatch):
# fetch_commits: contributionCalendar/weeks
resp_lib.add(resp_lib.POST, "https://api.github.com/graphql",
json={"data": {"user": {"contributionsCollection": {"contributionCalendar": {"weeks": []}}}}}, status=200)
# fetch_commit_count (alltime) — first call: get createdAt
resp_lib.add(resp_lib.POST, "https://api.github.com/graphql",
json={"data": {"user": {"createdAt": "2020-01-01T00:00:00Z"}}}, status=200)
# fetch_commit_count (alltime) — per-year contribution totals (2020–2026, 7 years)
for _ in range(7):
resp_lib.add(resp_lib.POST, "https://api.github.com/graphql",
json={"data": {"user": {"contributionsCollection": {"contributionCalendar": {"totalContributions": 10}}}}},
status=200)
# fetch_commit_count (recent, last 6 months)
resp_lib.add(resp_lib.POST, "https://api.github.com/graphql",
json={"data": {"user": {"contributionsCollection": {"contributionCalendar": {"totalContributions": 5}}}}},
status=200)
# fetch_user_commit_repos (for collaborators)
resp_lib.add(resp_lib.POST, "https://api.github.com/graphql",
json={"data": {"user": {"contributionsCollection": {"commitContributionsByRepository": []}}}},
status=200)
# PR count search
# The fetcher fires the independent GitHub calls in parallel, so we
# can't rely on a fixed FIFO order of `resp_lib.add` calls. Match each
# GraphQL call by inspecting the request body and returning the right
# response for that query — this mirrors how the real GitHub API
# routes by query content rather than insertion order.
import json as _json
def _graphql_callback(request):
body = _json.loads(request.body)
q = body.get("query", "")
variables = body.get("variables", {})
if "weeks" in q:
data = {"user": {"contributionsCollection": {"contributionCalendar": {"weeks": []}}}}
elif "createdAt" in q:
data = {"user": {"createdAt": "2020-01-01T00:00:00Z"}}
elif "commitContributionsByRepository" in q:
data = {"user": {"contributionsCollection": {"commitContributionsByRepository": []}}}
elif "contributionCalendar" in q and "totalContributions" in q:
# Both alltime per-year and 6-month range queries land here. Tests
# don't care about the exact totals — just that a number comes back.
data = {"user": {"contributionsCollection": {"contributionCalendar": {"totalContributions": 10}}}}
else:
data = {"user": None}
return (200, {}, _json.dumps({"data": data}))
resp_lib.add_callback(resp_lib.POST, "https://api.github.com/graphql",
callback=_graphql_callback)
resp_lib.add(resp_lib.GET, "https://api.github.com/search/issues",
json={"total_count": 0, "items": []}, status=200)
resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice",
json={"login": "alice", "public_repos": 1, "followers": 0, "avatar_url": "https://avatars.example/a"}, status=200)
# fetch_repos: two calls (owner + all)
json={"login": "alice", "public_repos": 1, "followers": 0,
"avatar_url": "https://avatars.example/a"},
status=200)
# fetch_repos fires the owner+all calls in parallel; both hit the same URL.
resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice/repos", json=[], status=200)
resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice/repos", json=[], status=200)
resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice/events", json=[], status=200)