Skip to content

Commit 42769af

Browse files
committed
Add tests with async and non-async decorators to check for consistent behavior upstream.
1 parent 1fd539b commit 42769af

17 files changed

+1133
-7
lines changed

flask_parameter_validation/test/test_file_params.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ def test_required_file(client):
1717
assert "error" in r.json
1818

1919

20+
def test_required_file_decorator(client):
21+
url = "/file/decorator/required"
22+
# Test that we receive a success response if a file is provided
23+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
24+
assert "success" in r.json
25+
assert r.json["success"]
26+
# Test that we receive an error if a file is not provided
27+
r = client.post(url)
28+
assert "error" in r.json
29+
30+
31+
def test_required_file_async_decorator(client):
32+
url = "/file/async_decorator/required"
33+
# Test that we receive a success response if a file is provided
34+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
35+
assert "success" in r.json
36+
assert r.json["success"]
37+
# Test that we receive an error if a file is not provided
38+
r = client.post(url)
39+
assert "error" in r.json
40+
41+
2042
def test_optional_file(client):
2143
url = "/file/optional"
2244
# Test that we receive a success response if a file is provided

flask_parameter_validation/test/test_form_params.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ def test_required_str(client):
2525
assert "error" in r.json
2626

2727

28+
def test_required_str_decorator(client):
29+
url = "/form/str/decorator/required"
30+
# Test that present input yields input value
31+
r = client.post(url, data={"v": "v"})
32+
assert "v" in r.json
33+
assert r.json["v"] == "v"
34+
# Test that missing input yields error
35+
r = client.post(url)
36+
assert "error" in r.json
37+
38+
39+
def test_required_str_async_decorator(client):
40+
url = "/form/str/async_decorator/required"
41+
# Test that present input yields input value
42+
r = client.post(url, data={"v": "v"})
43+
assert "v" in r.json
44+
assert r.json["v"] == "v"
45+
# Test that missing input yields error
46+
r = client.post(url)
47+
assert "error" in r.json
48+
49+
2850
def test_optional_str(client):
2951
url = "/form/str/optional"
3052
# Test that missing input yields None

flask_parameter_validation/test/test_json_params.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,54 @@ def test_dict_default(client):
952952
assert opt == r.json["opt"]
953953

954954

955+
def test_dict_default_decorator(client):
956+
url = "/json/dict/decorator/default"
957+
# Test that present dict yields input values
958+
n_opt = {"e": "f"}
959+
opt = {"g": "h"}
960+
r = client.post(url, json={"n_opt": n_opt, "opt": opt})
961+
assert "n_opt" in r.json
962+
assert "opt" in r.json
963+
assert type(r.json["n_opt"]) is dict
964+
assert type(r.json["opt"]) is dict
965+
assert n_opt == r.json["n_opt"]
966+
assert opt == r.json["opt"]
967+
# Test that missing dict yields default values
968+
n_opt = {"a": "b"}
969+
opt = {"c": "d"}
970+
r = client.post(url)
971+
assert "n_opt" in r.json
972+
assert "opt" in r.json
973+
assert type(r.json["n_opt"]) is dict
974+
assert type(r.json["opt"]) is dict
975+
assert n_opt == r.json["n_opt"]
976+
assert opt == r.json["opt"]
977+
978+
979+
def test_dict_default_async_decorator(client):
980+
url = "/json/dict/async_decorator/default"
981+
# Test that present dict yields input values
982+
n_opt = {"e": "f"}
983+
opt = {"g": "h"}
984+
r = client.post(url, json={"n_opt": n_opt, "opt": opt})
985+
assert "n_opt" in r.json
986+
assert "opt" in r.json
987+
assert type(r.json["n_opt"]) is dict
988+
assert type(r.json["opt"]) is dict
989+
assert n_opt == r.json["n_opt"]
990+
assert opt == r.json["opt"]
991+
# Test that missing dict yields default values
992+
n_opt = {"a": "b"}
993+
opt = {"c": "d"}
994+
r = client.post(url)
995+
assert "n_opt" in r.json
996+
assert "opt" in r.json
997+
assert type(r.json["n_opt"]) is dict
998+
assert type(r.json["opt"]) is dict
999+
assert n_opt == r.json["n_opt"]
1000+
assert opt == r.json["opt"]
1001+
1002+
9551003
def test_dict_func(client):
9561004
url = "/json/dict/func"
9571005
# Test that dict passing func yields input value

0 commit comments

Comments
 (0)