This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_function.py
More file actions
307 lines (255 loc) · 9.82 KB
/
test_function.py
File metadata and controls
307 lines (255 loc) · 9.82 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
import pytest
from flask_utils import BadRequestError
from flask_utils import validate_params
class TestDefaultTypes:
@pytest.fixture()
def test_function(self):
@validate_params()
def default_types(name: str, age: int, is_active: bool, weight: float, hobbies: list, address: dict):
return True
return default_types
def test_valid_request(self, test_function):
test_function(
"John",
25,
True,
70.5,
["reading", 1, 6.66, True, False, [1, 2, 3], {"name": "John"}],
{
"city": "New York City",
"street": "Main St.",
"number": 123,
"is_active": True,
"hobbies": ["reading", 1, 6.66, True, False, [1, 2, 3], {"name": "John"}],
},
)
@pytest.mark.parametrize(
"key, data",
[
(
"name",
{
"name": 25,
"age": 25,
"is_active": True,
"weight": 70.5,
"hobbies": ["reading"],
"address": {"city": "New York City"},
},
),
(
"age",
{
"name": "John",
"age": "25",
"is_active": True,
"weight": 70.5,
"hobbies": ["reading"],
"address": {"city": "New York City"},
},
),
(
"is_active",
{
"name": "John",
"age": 25,
"is_active": "True",
"weight": 70.5,
"hobbies": ["reading"],
"address": {"city": "New York City"},
},
),
(
"weight",
{
"name": "John",
"age": 25,
"is_active": True,
"weight": "70.5",
"hobbies": ["reading"],
"address": {"city": "New York City"},
},
),
(
"hobbies",
{
"name": "John",
"age": 25,
"is_active": True,
"weight": 70.5,
"hobbies": "reading",
"address": {"city": "New York City"},
},
),
(
"address",
{
"name": "John",
"age": 25,
"is_active": True,
"weight": 70.5,
"hobbies": ["reading"],
"address": "New York City",
},
),
],
)
def test_wrong_type(self, test_function, key, data):
with pytest.raises(BadRequestError) as e:
test_function(**data)
assert str(e.value.msg) == f"Wrong type for key {key}."
@pytest.mark.skip(reason="Skipping this test until I hear from Seluj78")
class TestTupleUnion:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/tuple-union")
@validate_params()
def union(name: (str, int)):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/tuple-union", json={"name": "John"})
assert response.status_code == 200
response = client.post("/tuple-union", json={"name": 25})
assert response.status_code == 200
def test_wrong_type(self, client):
response = client.post("/tuple-union", json={"name": 25.5})
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Wrong type for key name."
class TestUnion:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/union")
@validate_params()
def union(name: Union[str, int]):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/union", json={"name": "John"})
assert response.status_code == 200
response = client.post("/union", json={"name": 25})
assert response.status_code == 200
def test_wrong_type(self, client):
response = client.post("/union", json={"name": 25.5})
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Wrong type for key name."
class TestOptional:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/optional")
@validate_params()
def optional(name: str, age: Optional[int]):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/optional", json={"name": "John", "age": 25})
assert response.status_code == 200
response = client.post("/optional", json={"name": "John"})
assert response.status_code == 200
def test_wrong_type(self, client):
response = client.post("/optional", json={"name": "John", "age": "25"})
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Wrong type for key age."
class TestList:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/list")
@validate_params()
def list(name: List[str]):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/list", json={"name": ["John", "Doe"]})
assert response.status_code == 200
def test_wrong_type(self, client):
response = client.post("/list", json={"name": ["John", 25]})
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Wrong type for key name."
class TestDict:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/dict")
@validate_params()
def dict_route(name: Dict[str, int]):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/dict", json={"name": {"John": 25}})
assert response.status_code == 200
def test_wrong_type(self, client):
response = client.post("/dict", json={"name": {"John": "25"}})
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Wrong type for key name."
class TestAny:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/any")
@validate_params()
def any_route(name: Any):
return "OK", 200
def test_valid_request(self, client):
response = client.post("/any", json={"name": "John"})
assert response.status_code == 200
response = client.post("/any", json={"name": 25})
assert response.status_code == 200
response = client.post("/any", json={"name": True})
assert response.status_code == 200
response = client.post("/any", json={"name": 25.5})
assert response.status_code == 200
response = client.post("/any", json={"name": ["John", 25]})
assert response.status_code == 200
response = client.post("/any", json={"name": {"John": 25}})
assert response.status_code == 200
class TestMixAndMatch:
@pytest.fixture(autouse=True)
def setup_routes(self, flask_client):
@flask_client.post("/mix-and-match")
@validate_params()
def mix_and_match(
name: Union[str, int], age: Optional[int], hobbies: List[str], address: Dict[str, int], is_active: Any
):
return "OK", 200
def test_valid_request(self, client):
response = client.post(
"/mix-and-match",
json={"name": "John", "age": 25, "hobbies": ["reading"], "address": {"city": 123}, "is_active": True},
)
assert response.status_code == 200
@pytest.mark.parametrize(
"key, data, is_missing",
[
("name", {"age": 25, "hobbies": ["reading"], "address": {"city": 123}, "is_active": True}, True),
("age", {"name": "John", "hobbies": ["reading"], "address": {"city": 123}, "is_active": True}, False),
("hobbies", {"name": "John", "age": 25, "address": {"city": 123}, "is_active": True}, True),
("address", {"name": "John", "age": 25, "hobbies": ["reading"], "is_active": True}, True),
("is_active", {"name": "John", "age": 25, "hobbies": ["reading"], "address": {"city": 123}}, True),
],
)
def test_missing_key(self, client, key, data, is_missing):
response = client.post("/mix-and-match", json=data)
if not is_missing:
assert response.status_code == 200
else:
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == f"Missing key: {key}"
def test_unexpected_key(self, client):
response = client.post(
"/mix-and-match",
json={
"name": "John",
"age": 25,
"hobbies": ["reading"],
"address": {"city": 123},
"is_active": True,
"unexpected_key": "value",
},
)
assert response.status_code == 400
error_dict = response.get_json()["error"]
assert error_dict["message"] == "Unexpected key: unexpected_key."