nixprstatus/tests/helpers/mocks.py

36 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-10-09 20:21:28 +00:00
import requests
import json
2024-12-03 02:15:59 +00:00
from typing import Any
2024-10-09 20:21:28 +00:00
2024-12-03 02:15:59 +00:00
class MockedResponse:
def __init__(self, json_data: bytes, status_code: int) -> None:
self.json_data = json_data
self.status_code = status_code
2024-10-09 20:21:28 +00:00
2024-12-03 02:15:59 +00:00
def json(self) -> Any:
return json.loads(self.json_data)
2024-10-09 20:21:28 +00:00
2024-12-03 02:15:59 +00:00
def raise_for_status(self) -> None:
if self.status_code not in [200, 201]:
raise requests.exceptions.HTTPError()
def mocked_requests_get(*args, **kwargs) -> MockedResponse:
2024-10-09 20:21:28 +00:00
if "pulls" in args[0]:
pr = args[0].split("/")[-1]
with open(f"tests/fixtures/pulls_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "compare" in args[0]:
branch, commit_sha = args[0].split("/")[-1].split("...")
with open(f"tests/fixtures/compare_{branch}_{commit_sha}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "comments" in args[0]:
pr = args[0].split("/")[-2]
with open(f"tests/fixtures/comments_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)