Coverage for tests/test_forms_from_non_typing_sequences.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import FastAPI, Form 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4app = FastAPI() 1abcde

5 

6 

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

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

9 return items 1abcde

10 

11 

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

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

14 return items 1abcde

15 

16 

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

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

19 return items 1abcde

20 

21 

22client = TestClient(app) 1abcde

23 

24 

25def test_python_list_param_as_form(): 1abcde

26 response = client.post( 1abcde

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

28 ) 

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

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

31 

32 

33def test_python_set_param_as_form(): 1abcde

34 response = client.post( 1abcde

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

36 ) 

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

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

39 

40 

41def test_python_tuple_param_as_form(): 1abcde

42 response = client.post( 1abcde

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

44 ) 

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

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