2021-11-04 05:01:35 +00:00
|
|
|
import json
|
|
|
|
import pytest
|
2021-11-05 03:30:12 +00:00
|
|
|
import httpx
|
|
|
|
|
2021-11-04 05:01:35 +00:00
|
|
|
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 04:34:19 +00:00
|
|
|
|
2021-11-05 03:30:12 +00:00
|
|
|
def test_except(httpx_mock: HTTPXMock):
|
|
|
|
httpx_mock.add_response(method="GET", status_code=403)
|
|
|
|
with pytest.raises(httpx.HTTPStatusError):
|
|
|
|
API().get("boooger")
|
|
|
|
|
2021-11-04 05:01:35 +00:00
|
|
|
|
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
|
2021-11-05 03:30:12 +00:00
|
|
|
|
|
|
|
# test exit on dict
|
|
|
|
httpx_mock.add_response(method="GET", json=_load_fixture("user"))
|
|
|
|
user = api.get_with_pagination("user")
|
2021-11-05 04:34:19 +00:00
|
|
|
assert user["login"] == "tyrelsouza"
|