Coverage for tests/test_forms_from_non_typing_sequences.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from fastapi import FastAPI, Form 1abcdefg

2from fastapi.testclient import TestClient 1abcdefg

3 

4app = FastAPI() 1abcdefg

5 

6 

7@app.post("/form/python-list") 1abcdefg

8def post_form_param_list(items: list = Form()): 1abcdefg

9 return items 1hijklmn

10 

11 

12@app.post("/form/python-set") 1abcdefg

13def post_form_param_set(items: set = Form()): 1abcdefg

14 return items 1opqrstu

15 

16 

17@app.post("/form/python-tuple") 1abcdefg

18def post_form_param_tuple(items: tuple = Form()): 1abcdefg

19 return items 1vwxyzAB

20 

21 

22client = TestClient(app) 1abcdefg

23 

24 

25def test_python_list_param_as_form(): 1abcdefg

26 response = client.post( 1hijklmn

27 "/form/python-list", data={"items": ["first", "second", "third"]} 

28 ) 

29 assert response.status_code == 200, response.text 1hijklmn

30 assert response.json() == ["first", "second", "third"] 1hijklmn

31 

32 

33def test_python_set_param_as_form(): 1abcdefg

34 response = client.post( 1opqrstu

35 "/form/python-set", data={"items": ["first", "second", "third"]} 

36 ) 

37 assert response.status_code == 200, response.text 1opqrstu

38 assert set(response.json()) == {"first", "second", "third"} 1opqrstu

39 

40 

41def test_python_tuple_param_as_form(): 1abcdefg

42 response = client.post( 1vwxyzAB

43 "/form/python-tuple", data={"items": ["first", "second", "third"]} 

44 ) 

45 assert response.status_code == 200, response.text 1vwxyzAB

46 assert response.json() == ["first", "second", "third"] 1vwxyzAB