forked from tiangolo/fastapi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add tests for required nonable parameters and body fields (tiangolo…
- Loading branch information
1 parent
21894ec
commit d9ea495
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |