ghub/api_tests.py

38 lines
1.2 KiB
Python
Raw Normal View History

2021-11-04 05:01:35 +00:00
import json
import pytest
from api import API
from pytest_httpx import HTTPXMock
2021-11-05 02:59:03 +00:00
LINK1 = '<https://api.github.com/user/6292/repos?page=2>; rel="next", <https://api.github.com/user/6292/repos?page=2>; rel="last"'
LINK2 = '<https://api.github.com/user/6292/repos?page=1>; rel="prev", <https://api.github.com/user/6292/repos?page=2>; rel="last"'
2021-11-04 05:01:35 +00:00
2021-11-05 02:59:03 +00:00
def _load_fixture(name: str):
with open(f"./tests/fixtures/{name}.json", "r") as f:
2021-11-04 05:01:35 +00:00
return json.loads(f.read())
2021-11-05 02:59:03 +00:00
def test_get(httpx_mock: HTTPXMock):
httpx_mock.add_response(method="GET", json=_load_fixture("user"))
api = API()
assert api.get("users")["login"] == "tyrelsouza"
2021-11-04 05:01:35 +00:00
2021-11-05 02:59:03 +00:00
def test_get_with_pagination(httpx_mock: HTTPXMock):
api = API()
# link, so multiple pages
httpx_mock.add_response(
method="GET", json=_load_fixture("repos_1"), headers={"link": LINK1}
)
httpx_mock.add_response(
method="GET", json=_load_fixture("repos_2"), headers={"link": LINK2}
)
2021-11-04 05:01:35 +00:00
2021-11-05 02:59:03 +00:00
repos = api.get_with_pagination("repos")
assert len(repos) == 2
2021-11-04 05:01:35 +00:00
2021-11-05 02:59:03 +00:00
# No link, so no 2nd page, only one item
httpx_mock.add_response(method="GET", json=_load_fixture("repos_1"))
repos = api.get_with_pagination("repos")
assert len(repos) == 1