Browse Source

Merge branch 'master' of github.com:Purushot14/fastapi

pull/13483/head
Purushot14 3 weeks ago
parent
commit
262a799f7c
  1. 22
      docs_src/custom_api_router/tutorial001.py
  2. 11
      tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py

22
docs_src/custom_api_router/tutorial001.py

@ -110,6 +110,7 @@ app = FastAPI(route_class=TimedRoute, router_class=AppRouter)
model = AppRouter(prefix="/model", name="Model", route_class=TimedRoute)
item = AppRouter(prefix="/{model_id}/item", name="Item", route_class=TimedRoute)
async def create_model(request: Request):
"""
Create a model
@ -117,12 +118,21 @@ async def create_model(request: Request):
print("Model created")
route: TimedRoute = request.scope["route"]
router: AppRouter = request.scope["router"]
return JSONResponse({"route_class": route.__class__.__name__, "route_name": route.name, "router_class": router.__class__.__name__}, status_code=200)
return JSONResponse(
{
"route_class": route.__class__.__name__,
"route_name": route.name,
"router_class": router.__class__.__name__,
},
status_code=200,
)
model.add_api_route(
path="/create", endpoint=create_model, methods=["POST"], name="create-model"
)
async def create_item(request: Request):
"""
Create an item
@ -131,8 +141,14 @@ async def create_item(request: Request):
route: TimedRoute = request.scope["route"]
router: AppRouter = request.scope["router"]
return JSONResponse(
{"route_class": route.__class__.__name__, "route_name": route.name, "router_class": router.__class__.__name__},
status_code=200)
{
"route_class": route.__class__.__name__,
"route_name": route.name,
"router_class": router.__class__.__name__,
},
status_code=200,
)
item.add_api_route(
path="/create", endpoint=create_item, methods=["POST"], name="create-item"

11
tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py

@ -13,22 +13,29 @@ from docs_src.custom_api_router.tutorial001 import app
client = TestClient(app)
def test_get_timed():
response = client.get("/healthz")
assert response.text == "OK"
assert "X-Response-Time" in response.headers
assert float(response.headers["X-Response-Time"]) >= 0
def test_route_class():
response = client.post("/model/create", json={"name": "test", "description": "test"})
response = client.post(
"/model/create", json={"name": "test", "description": "test"}
)
assert response.status_code == 200
response_json = response.json()
assert response_json["route_name"] == "Global.Model.create-model"
assert response_json["route_class"] == "TimedRoute"
assert response_json["router_class"] == "AppRouter"
def test_route_name():
response = client.post("/model/Model001/item/create", json={"name": "test", "description": "test"})
response = client.post(
"/model/Model001/item/create", json={"name": "test", "description": "test"}
)
assert response.status_code == 200
response_json = response.json()
assert response_json["route_name"] == "Global.Model.Item.create-item"

Loading…
Cancel
Save