Debian Codegraph @

tests/test_forms_from_non_typing_sequences.py

raw
1 from fastapi import FastAPI, Form
2 from fastapi.testclient import TestClient
3
4 app = FastAPI()
5
6
7 @app.post("/form/python-list")
8 def post_form_param_list(items: list = Form()):
9 return items
10
11
12 @app.post("/form/python-set")
13 def post_form_param_set(items: set = Form()):
14 return items
15
16
17 @app.post("/form/python-tuple")
18 def post_form_param_tuple(items: tuple = Form()):
19 return items
20
21
22 client = TestClient(app)
23
24
25 def test_python_list_param_as_form():
26 response = client.post(
27 "/form/python-list", data={"items": ["first", "second", "third"]}
28 )
29 assert response.status_code == 200, response.text
30 assert response.json() == ["first", "second", "third"]
31
32
33 def test_python_set_param_as_form():
34 response = client.post(
35 "/form/python-set", data={"items": ["first", "second", "third"]}
36 )
37 assert response.status_code == 200, response.text
38 assert set(response.json()) == {"first", "second", "third"}
39
40
41 def test_python_tuple_param_as_form():
42 response = client.post(
43 "/form/python-tuple", data={"items": ["first", "second", "third"]}
44 )
45 assert response.status_code == 200, response.text
46 assert response.json() == ["first", "second", "third"]