Created
January 12, 2023 20:08
-
-
Save jedahan/79098389291f8f9e1b9c266b3beba78a to your computer and use it in GitHub Desktop.
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
# python3 -m pip install fastapi pytest | |
# python3 -m pytest tests/post.py | |
from typing import Union | |
from fastapi import Form, FastAPI, Query | |
from fastapi.testclient import TestClient | |
app = FastAPI() | |
@app.post("/list") | |
def read_query(q: Union[list[str], list[None]] = Form()): | |
return q | |
@app.post("/none") | |
def read_none_query(q: Union[list[str], None] = Form()): | |
return q | |
client = TestClient(app) | |
def test_empty_list_valid(): | |
response = client.post("/list", data={"q": []}) | |
assert response.status_code == 200 | |
assert response.json() == [] | |
def test_full_list_valid(): | |
response = client.post("/list", data={"q": ["hi", "there"]}) | |
assert response.status_code == 200 | |
assert response.json() == ["hi", "there"] | |
def test_none_empty_list_valid(): | |
response = client.post("/none", data={"q": []}) | |
assert response.status_code == 200 | |
assert response.json() == [] | |
def test_none_full_list_valid(): | |
response = client.post("/none", data={"q": ["hi", "there"]}) | |
assert response.status_code == 200 | |
assert response.json() == ["hi", "there"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment