Skip to content

Instantly share code, notes, and snippets.

@jedahan
Created January 12, 2023 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedahan/79098389291f8f9e1b9c266b3beba78a to your computer and use it in GitHub Desktop.
Save jedahan/79098389291f8f9e1b9c266b3beba78a to your computer and use it in GitHub Desktop.
# 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