mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-04 15:13:09 -05:00
✨ Add support for combining form models (#16577)
This commit is contained in:
1 parent
386a28af2f
commit
48e43e5cbf
10 files changed
+726
-24
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture(name="client", params=["tutorial003_py310", "tutorial003_an_py310"])
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
|
||||
return TestClient(mod.app)
|
||||
|
||||
|
||||
def test_form_models(client: TestClient):
|
||||
response = client.post(
|
||||
"/profiles/",
|
||||
data={"name": "Rick", "tags": ["python", "fastapi"], "token": "secret"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
"profile": {"name": "Rick"},
|
||||
"preferences": {"tags": ["python", "fastapi"]},
|
||||
"token": "secret",
|
||||
}
|
||||
response = client.post("/profiles/", data={"name": "Rick", "token": "secret"})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json()["preferences"] == {"tags": []}
|
||||
|
||||
|
||||
def test_openapi(client: TestClient):
|
||||
schema = client.get("/openapi.json").json()
|
||||
body = schema["components"]["schemas"]["Body_create_profile_profiles__post"]
|
||||
assert set(body["properties"]) == {"name", "tags", "token"}
|
||||
assert set(body["required"]) == {"name", "token"}
|
||||
@@ -0,0 +1,40 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture(name="client", params=["tutorial004_py310", "tutorial004_an_py310"])
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
|
||||
return TestClient(mod.app)
|
||||
|
||||
|
||||
def test_form_model_and_file(client: TestClient):
|
||||
response = client.post(
|
||||
"/profiles/", data={"name": "Rick"}, files={"avatar": ("avatar.png", b"image")}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"profile": {"name": "Rick"}, "filename": "avatar.png"}
|
||||
|
||||
|
||||
def test_missing_file(client: TestClient):
|
||||
response = client.post("/profiles/", data={"name": "Rick"})
|
||||
assert response.status_code == 422
|
||||
assert response.json()["detail"][0]["loc"] == ["body", "avatar"]
|
||||
|
||||
|
||||
def test_openapi(client: TestClient):
|
||||
schema = client.get("/openapi.json").json()
|
||||
body = schema["paths"]["/profiles/"]["post"]["requestBody"]
|
||||
assert set(body["content"]) == {"multipart/form-data"}
|
||||
model = schema["components"]["schemas"]["Body_create_profile_profiles__post"]
|
||||
assert model["properties"] == {
|
||||
"name": {"type": "string", "title": "Name"},
|
||||
"avatar": {
|
||||
"type": "string",
|
||||
"contentMediaType": "application/octet-stream",
|
||||
"title": "Avatar",
|
||||
},
|
||||
}
|
||||
assert set(model["required"]) == {"name", "avatar"}
|
||||
Reference in new issue
Block a user