5 changed files with 81 additions and 0 deletions
@ -0,0 +1,11 @@ |
|||||
|
from fastapi import FastAPI |
||||
|
from pydantic import BaseModel |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
class Item(BaseModel): |
||||
|
description: str | None |
||||
|
|
||||
|
@app.post("/items/") |
||||
|
async def create_item(item: Item): |
||||
|
return item |
||||
@ -0,0 +1,12 @@ |
|||||
|
from typing import Optional |
||||
|
from fastapi import FastAPI |
||||
|
from pydantic import BaseModel |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
class Item(BaseModel): |
||||
|
description: Optional[str] |
||||
|
|
||||
|
@app.post("/items/") |
||||
|
async def create_item(item: Item): |
||||
|
return item |
||||
@ -0,0 +1,28 @@ |
|||||
|
import importlib |
||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py310 |
||||
|
|
||||
|
@pytest.fixture( |
||||
|
name="client", |
||||
|
params=[ |
||||
|
pytest.param("tutorial005_py39"), |
||||
|
pytest.param("tutorial005_py310", marks=needs_py310), |
||||
|
], |
||||
|
) |
||||
|
def get_client(request: pytest.FixtureRequest): |
||||
|
mod = importlib.import_module(f"docs_src.body.{request.param}") |
||||
|
client = TestClient(mod.app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
def test_required_nullable_field(client: TestClient): |
||||
|
response = client.post("/items/", json={"description": None}) |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == {"description": None} |
||||
|
|
||||
|
|
||||
|
def test_required_field_missing(client: TestClient): |
||||
|
response = client.post("/items/", json={}) |
||||
|
assert response.status_code == 422 |
||||
Loading…
Reference in new issue