mirror of https://github.com/tiangolo/fastapi.git
31 lines
705 B
Python
31 lines
705 B
Python
from contextlib import asynccontextmanager
|
|
from typing import Any
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
# Use typing.Dict for compatibility with older Python versions
|
|
items: dict[str, Any] = {}
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
items["foo"] = {"name": "Fighters"}
|
|
items["bar"] = {"name": "Tenders"}
|
|
yield
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
@app.get("/items/{item_id}")
|
|
async def read_items(item_id: str):
|
|
return items[item_id]
|
|
|
|
|
|
def test_read_items():
|
|
with TestClient(app) as client:
|
|
response = client.get("/items/foo")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"name": "Fighters"}
|