Browse Source

Add variants for `path-operation-advanced-configuration/tutorial004`

pull/14413/head
Yurii Motov 8 months ago
parent
commit
f13f460d9b
  1. 2
      docs/en/docs/advanced/path-operation-advanced-configuration.md
  2. 28
      docs_src/path_operation_advanced_configuration/tutorial004_py310.py
  3. 30
      docs_src/path_operation_advanced_configuration/tutorial004_py39.py
  4. 29
      tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial004.py

2
docs/en/docs/advanced/path-operation-advanced-configuration.md

@ -50,7 +50,7 @@ Adding an `\f` (an escaped "form feed" character) causes **FastAPI** to truncate
It won't show up in the documentation, but other tools (such as Sphinx) will be able to use the rest. It won't show up in the documentation, but other tools (such as Sphinx) will be able to use the rest.
{* ../../docs_src/path_operation_advanced_configuration/tutorial004.py hl[19:29] *} {* ../../docs_src/path_operation_advanced_configuration/tutorial004_py310.py hl[17:27] *}
## Additional Responses { #additional-responses } ## Additional Responses { #additional-responses }

28
docs_src/path_operation_advanced_configuration/tutorial004_py310.py

@ -0,0 +1,28 @@
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
\f
:param item: User input.
"""
return item

30
docs_src/path_operation_advanced_configuration/tutorial004_py39.py

@ -0,0 +1,30 @@
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
\f
:param item: User input.
"""
return item

29
tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial004.py

@ -1,13 +1,30 @@
import importlib
import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial004 import app from ...utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2
from ...utils import needs_pydanticv1, needs_pydanticv2 @pytest.fixture(
name="client",
params=[
pytest.param("tutorial004"),
pytest.param("tutorial004_py39", marks=needs_py39),
pytest.param("tutorial004_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.path_operation_advanced_configuration.{request.param}"
)
client = TestClient(app) client = TestClient(mod.app)
client.headers.clear()
return client
def test_query_params_str_validations(): def test_query_params_str_validations(client: TestClient):
response = client.post("/items/", json={"name": "Foo", "price": 42}) response = client.post("/items/", json={"name": "Foo", "price": 42})
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == { assert response.json() == {
@ -20,7 +37,7 @@ def test_query_params_str_validations():
@needs_pydanticv2 @needs_pydanticv2
def test_openapi_schema(): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == { assert response.json() == {
@ -123,7 +140,7 @@ def test_openapi_schema():
# TODO: remove when deprecating Pydantic v1 # TODO: remove when deprecating Pydantic v1
@needs_pydanticv1 @needs_pydanticv1
def test_openapi_schema_pv1(): def test_openapi_schema_pv1(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() == { assert response.json() == {

Loading…
Cancel
Save