-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_api_pydantic.py
100 lines (77 loc) · 3.39 KB
/
test_api_pydantic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import pytest
from pydantic import BaseModel, Field
from pytest_httpx import HTTPXMock
import quickapi
class Fact(BaseModel):
fact: str
length: int
class RequestParams(BaseModel):
max_length: int = 100
limit: int = 10
class RequestBody(BaseModel):
some_data: str | None = None
class ResponseBody(BaseModel):
current_page: int = Field(lt=100)
data: list[Fact] = Field(default_factory=list)
class ResponseError401(BaseModel):
status: str
message: str
# TODO: Build real mock API to easily test various scenarios?
class PostPydanticApi(quickapi.BaseApi[ResponseBody]):
url = "https://example.com/facts"
method = quickapi.BaseHttpMethod.POST
request_params = RequestParams
request_body = RequestBody
response_body = ResponseBody
response_errors = {401: ResponseError401} # noqa: RUF012
class TestGetPydanticApi:
def test_api_call_with_default_request_params(self, httpx_mock: HTTPXMock):
mock_json = {"current_page": 1, "data": [{"fact": "Some fact", "length": 9}]}
httpx_mock.add_response(
url=f"{PostPydanticApi.url}?max_length={RequestParams().max_length}&limit={RequestParams().limit}",
json=mock_json,
)
client = PostPydanticApi()
response = client.execute()
assert response.body.current_page == 1
assert response.body.data[0] == Fact(fact="Some fact", length=9)
def test_api_call_with_custom_request_params(self, httpx_mock: HTTPXMock):
mock_json = {"current_page": 1, "data": [{"fact": "fact", "length": 4}]}
request_params = RequestParams(max_length=5, limit=10)
httpx_mock.add_response(
url=f"{PostPydanticApi.url}?max_length={request_params.max_length}&limit={request_params.limit}",
json=mock_json,
)
client = PostPydanticApi()
response = client.execute(request_params=request_params)
assert response.body.current_page == 1
assert response.body.data[0] == Fact(fact="fact", length=4)
def test_api_call_with_custom_request_body(self, httpx_mock: HTTPXMock):
mock_json = {"current_page": 1, "data": [{"fact": "fact", "length": 4}]}
request_params = RequestParams(max_length=5, limit=10)
request_body = RequestBody(some_data="some data")
httpx_mock.add_response(
url=f"{PostPydanticApi.url}?max_length={request_params.max_length}&limit={request_params.limit}",
match_json={"some_data": request_body.some_data},
json=mock_json,
)
client = PostPydanticApi()
response = client.execute(
request_params=request_params, request_body=request_body
)
assert response.body.current_page == 1
assert response.body.data[0] == Fact(fact="fact", length=4)
def test_api_call_with_custom_response_errors(self, httpx_mock: HTTPXMock):
mock_json = {"status": "Failure", "message": "Unauthorized"}
httpx_mock.add_response(
url=f"{PostPydanticApi.url}?max_length={RequestParams().max_length}&limit={RequestParams().limit}",
json=mock_json,
status_code=401,
)
client = PostPydanticApi()
with pytest.raises(quickapi.HandledHTTPError) as e:
client.execute()
assert e.value.status_code == 401
assert e.value.body == ResponseError401(
status="Failure", message="Unauthorized"
)