Coverage for tests / test_required_noneable.py: 100%
37 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from fastapi import Body, FastAPI, Query 1abcd
2from fastapi.testclient import TestClient 1abcd
4app = FastAPI() 1abcd
7@app.get("/query") 1abcd
8def read_query(q: str | None): 1abcd
9 return q 1efgh
12@app.get("/explicit-query") 1abcd
13def read_explicit_query(q: str | None = Query()): 1abcd
14 return q 1ijkl
17@app.post("/body-embed") 1abcd
18def send_body_embed(b: str | None = Body(embed=True)): 1abcd
19 return b 1mnop
22client = TestClient(app) 1abcd
25def test_required_nonable_query_invalid(): 1abcd
26 response = client.get("/query") 1qrst
27 assert response.status_code == 422 1qrst
30def test_required_noneable_query_value(): 1abcd
31 response = client.get("/query", params={"q": "foo"}) 1efgh
32 assert response.status_code == 200 1efgh
33 assert response.json() == "foo" 1efgh
36def test_required_nonable_explicit_query_invalid(): 1abcd
37 response = client.get("/explicit-query") 1uvwx
38 assert response.status_code == 422 1uvwx
41def test_required_nonable_explicit_query_value(): 1abcd
42 response = client.get("/explicit-query", params={"q": "foo"}) 1ijkl
43 assert response.status_code == 200 1ijkl
44 assert response.json() == "foo" 1ijkl
47def test_required_nonable_body_embed_no_content(): 1abcd
48 response = client.post("/body-embed") 1yzAB
49 assert response.status_code == 422 1yzAB
52def test_required_nonable_body_embed_invalid(): 1abcd
53 response = client.post("/body-embed", json={"invalid": "invalid"}) 1CDEF
54 assert response.status_code == 422 1CDEF
57def test_required_noneable_body_embed_value(): 1abcd
58 response = client.post("/body-embed", json={"b": "foo"}) 1mnop
59 assert response.status_code == 200 1mnop
60 assert response.json() == "foo" 1mnop