-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_generated_client.py
235 lines (208 loc) · 8.95 KB
/
test_generated_client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from decimal import Decimal
import pytest
from httpx import Response
from respx import MockRouter
from .test_client import client, config, http, schemas
BASE_URL = config.api_base_url()
@pytest.mark.respx(base_url=BASE_URL)
def test_simple_request_simple_request_get(respx_mock: MockRouter):
# Given
mocked_response = {"status": "hello world"}
mock_path = "/simple-request"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.simple_request_simple_request_get()
# Then
assert isinstance(response, schemas.SimpleResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_simple_request_simple_request_get_raises_exception(respx_mock: MockRouter):
# Given
mocked_response = {"bad": "response"}
mock_path = "/simple-request"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=404))
# Then
with pytest.raises(http.APIException) as raised_exception:
client.simple_request_simple_request_get()
assert isinstance(raised_exception.value, http.APIException)
# Make sure we have the response on the exception
assert raised_exception.value.response.status_code == 404
assert raised_exception.value.reason == "An unexpected status code was received"
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_optional_parameters_request_optional_parameters_get(respx_mock: MockRouter):
# Given
mocked_response = {"optional_parameter": None, "required_parameter": "Hello"}
mock_path = "/optional-parameters"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.optional_parameters_request_optional_parameters_get()
# Then
assert isinstance(response, schemas.OptionalParametersResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_parameter_request_simple_request(respx_mock: MockRouter):
# Given
your_input = "hello world"
mocked_response = {"your_input": your_input}
mock_path = f"/simple-request/{your_input}"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.parameter_request_simple_request(your_input=your_input)
# Then
assert isinstance(response, schemas.ParameterResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_query_request_simple_query_get(respx_mock: MockRouter):
# Given
your_input = "hello world"
mocked_response = {"your_query": your_input}
mock_path = "/simple-query?yourInput=hello+world"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.query_request_simple_query_get(yourInput=your_input)
# Then
assert isinstance(response, schemas.SimpleQueryParametersResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_query_request_optional_query_get(respx_mock: MockRouter):
# Given
your_input = None
mocked_response = {"your_query": "test"}
# NOTE: omits None query parameter
mock_path = "/optional-query"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.query_request_optional_query_get(yourInput=your_input)
# Then
assert isinstance(response, schemas.OptionalQueryParametersResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_complex_model_request_complex_model_request_get(respx_mock: MockRouter):
# Given
mocked_response = {
"a_dict_response": {"dict": "response"},
"a_enum": "ONE",
"a_list_of_enums": ["ONE", "TWO"],
"a_list_of_numbers": [1, 2, 3],
"a_list_of_other_models": [{"key": "first"}],
"a_list_of_strings": ["hello", "world"],
"a_number": 13,
"a_decimal": 0.4,
"a_string": "hello world",
"another_model": {"key": "value"},
}
mock_path = "/complex-model-request"
respx_mock.get(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.complex_model_request_complex_model_request_get()
# Then
assert isinstance(response, schemas.ComplexModelResponse)
expected_dump_data = {
"a_dict_response": {"dict": "response"},
"a_enum": schemas.ExampleEnum.ONE,
"a_list_of_enums": [schemas.ExampleEnum.ONE, schemas.ExampleEnum.TWO],
"a_list_of_numbers": [1, 2, 3],
"a_list_of_other_models": [{"key": "first"}],
"a_list_of_strings": ["hello", "world"],
"a_number": 13,
"a_decimal": Decimal("0.4"),
"a_string": "hello world",
"another_model": {"key": "value"},
}
assert response.model_dump() == expected_dump_data
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_request_data_request_data_post(respx_mock: MockRouter):
# Given
mocked_response = {"my_input": "test"}
mock_path = "/request-data"
respx_mock.post(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
data = schemas.RequestDataRequest(
my_input="test",
my_decimal_input=Decimal(0.1),
)
response = client.request_data_request_data_post(data=data)
# Then
assert isinstance(response, schemas.RequestDataResponse)
assert response.model_dump() == mocked_response
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_request_data_request_data_put(respx_mock: MockRouter):
# Given
mocked_response = {"my_input": "test"}
mock_path = "/request-data"
respx_mock.put(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
data = schemas.RequestDataRequest(my_input="test", my_decimal_input=Decimal(0.1))
response = client.request_data_request_data_put(data=data)
# Then
assert isinstance(response, schemas.RequestDataResponse)
assert response.model_dump() == mocked_response
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_request_data_path_request_data(respx_mock: MockRouter):
# Given
path_parameter = "param"
mocked_response = {"my_input": "test", "path_parameter": path_parameter}
mock_path = f"/request-data/{path_parameter}"
respx_mock.post(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
data = schemas.RequestDataRequest(my_input="test", my_decimal_input=Decimal(0.1))
response = client.request_data_path_request_data(path_parameter, data=data)
# Then
assert isinstance(response, schemas.RequestDataAndParameterResponse)
assert response.model_dump() == mocked_response
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_request_delete_request_delete_delete(respx_mock: MockRouter):
# Given
mocked_response = {}
mock_path = "/request-delete"
respx_mock.delete(mock_path).mock(return_value=Response(json=mocked_response, status_code=200))
# When
response = client.request_delete_request_delete_delete()
# Then
assert isinstance(response, schemas.DeleteResponse)
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path
@pytest.mark.respx(base_url=BASE_URL)
def test_header_request_header_request_get(respx_mock: MockRouter):
# Given
input_header = "foo"
mocked_response = {"x_test": input_header}
mock_path = "/header-request"
respx_mock.get(mock_path, headers={"x-test": input_header}).mock(
return_value=Response(json=mocked_response, status_code=200)
)
# When
headers = schemas.HeaderRequestHeaderRequestGetHeaders(x_test=input_header)
response = client.header_request_header_request_get(headers=headers)
# Then
assert isinstance(response, schemas.HeadersResponse)
assert response.model_dump() == mocked_response
assert len(respx_mock.calls) == 1
call = respx_mock.calls[0]
assert call.request.url == BASE_URL + mock_path