-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
49 lines (41 loc) · 1.58 KB
/
test_utils.py
File metadata and controls
49 lines (41 loc) · 1.58 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
# Standard library imports
from typing import Callable, Any
# Third party imports
from flask.testing import FlaskClient
# Local application imports
JsonData = dict[str, Any]
def test_route_wrong_params(
client: FlaskClient,
route: str,
get_full_data: Callable[[], JsonData],
path: list[str | int] | None = None,
) -> None:
if path is None:
path = []
def get_json() -> tuple[JsonData, Any]:
json = get_full_data()
target: Any = json
for sub_path in path:
target = target[sub_path]
return json, target
data = get_json()[1]
if isinstance(data, dict):
for key, value in data.items():
json, target = get_json()
target.pop(key)
response = client.post(route, json=json)
if response.status_code == 400:
error_description: str = response.get_json()["description"]
assert "must contain" in error_description
assert f"'{key}'" in error_description
if isinstance(value, (dict, list)):
test_route_wrong_params(client, route, get_full_data, path + [key])
json, target = get_json()
target["dumb_key"] = "dumb_value"
response = client.post(route, json=json)
assert response.status_code == 400
error_description = response.get_json()["description"]
assert "must not contain" in error_description
assert "'dumb_key'" in error_description
elif isinstance(data, list) and data:
test_route_wrong_params(client, route, get_full_data, path + [0])