mirror of https://github.com/tiangolo/fastapi.git
✅ Add tests for required nonable parameters and body fields (#4907)
This commit is contained in:
parent
0a8d6871fb
commit
acab64b3c3
|
|
@ -0,0 +1,62 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from fastapi import Body, FastAPI, Query
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/query")
|
||||||
|
def read_query(q: Union[str, None]):
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/explicit-query")
|
||||||
|
def read_explicit_query(q: Union[str, None] = Query()):
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/body-embed")
|
||||||
|
def send_body_embed(b: Union[str, None] = Body(embed=True)):
|
||||||
|
return b
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_nonable_query_invalid():
|
||||||
|
response = client.get("/query")
|
||||||
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_noneable_query_value():
|
||||||
|
response = client.get("/query", params={"q": "foo"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == "foo"
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_nonable_explicit_query_invalid():
|
||||||
|
response = client.get("/explicit-query")
|
||||||
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_nonable_explicit_query_value():
|
||||||
|
response = client.get("/explicit-query", params={"q": "foo"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == "foo"
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_nonable_body_embed_no_content():
|
||||||
|
response = client.post("/body-embed")
|
||||||
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_nonable_body_embed_invalid():
|
||||||
|
response = client.post("/body-embed", json={"invalid": "invalid"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_required_noneable_body_embed_value():
|
||||||
|
response = client.post("/body-embed", json={"b": "foo"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == "foo"
|
||||||
Loading…
Reference in New Issue