Coverage for tests / test_required_noneable.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-21 17:29 +0000

1from fastapi import Body, FastAPI, Query 1abcd

2from fastapi.testclient import TestClient 1abcd

3 

4app = FastAPI() 1abcd

5 

6 

7@app.get("/query") 1abcd

8def read_query(q: str | None): 1abcd

9 return q 1efg

10 

11 

12@app.get("/explicit-query") 1abcd

13def read_explicit_query(q: str | None = Query()): 1abcd

14 return q 1hij

15 

16 

17@app.post("/body-embed") 1abcd

18def send_body_embed(b: str | None = Body(embed=True)): 1abcd

19 return b 1klm

20 

21 

22client = TestClient(app) 1abcd

23 

24 

25def test_required_nonable_query_invalid(): 1abcd

26 response = client.get("/query") 1nop

27 assert response.status_code == 422 1nop

28 

29 

30def test_required_noneable_query_value(): 1abcd

31 response = client.get("/query", params={"q": "foo"}) 1efg

32 assert response.status_code == 200 1efg

33 assert response.json() == "foo" 1efg

34 

35 

36def test_required_nonable_explicit_query_invalid(): 1abcd

37 response = client.get("/explicit-query") 1qrs

38 assert response.status_code == 422 1qrs

39 

40 

41def test_required_nonable_explicit_query_value(): 1abcd

42 response = client.get("/explicit-query", params={"q": "foo"}) 1hij

43 assert response.status_code == 200 1hij

44 assert response.json() == "foo" 1hij

45 

46 

47def test_required_nonable_body_embed_no_content(): 1abcd

48 response = client.post("/body-embed") 1tuv

49 assert response.status_code == 422 1tuv

50 

51 

52def test_required_nonable_body_embed_invalid(): 1abcd

53 response = client.post("/body-embed", json={"invalid": "invalid"}) 1wxy

54 assert response.status_code == 422 1wxy

55 

56 

57def test_required_noneable_body_embed_value(): 1abcd

58 response = client.post("/body-embed", json={"b": "foo"}) 1klm

59 assert response.status_code == 200 1klm

60 assert response.json() == "foo" 1klm