Also test annotated param

This commit is contained in:
Stefans Mezulis 2023-09-17 12:12:42 +01:00
parent 19e819d8a9
commit 9382f22468
1 changed files with 23 additions and 5 deletions

View File

@ -1,28 +1,46 @@
from typing import Annotated
import pytest
from fastapi import FastAPI, Form
from fastapi.testclient import TestClient
app = FastAPI()
@app.post("/")
@app.post("/old-style", operation_id="old_style")
def route_with_form(form_param: str = Form(alias="aliased-field")):
return {}
@app.post("/annotated", operation_id="annotated")
def route_with_form_annotated(form_param: Annotated[str, Form(alias="aliased-field")]):
return {}
client = TestClient(app)
def test_get_route():
response = client.post("/", data={"aliased-field": "Hello, World!"})
@pytest.mark.parametrize(
"path",
["/old-style", "/annotated"]
)
def test_get_route(path: str):
response = client.post(path, data={"aliased-field": "Hello, World!"})
assert response.status_code == 200, response.text
assert response.json() == {}
def test_openapi():
@pytest.mark.parametrize(
"schema_obj",
["Body_annotated", "Body_old_style"]
)
def test_form_alias_is_correct(schema_obj: str):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
print(response.json()["components"]["schemas"])
form_properties = (
response.json()
["components"]["schemas"]["Body_route_with_form__post"]["properties"]
["components"]["schemas"][schema_obj]["properties"]
)
assert "aliased-field" in form_properties