2019-04-12 20:15:05 +04:00
|
|
|
from fastapi import FastAPI
|
2020-03-01 21:49:20 +01:00
|
|
|
from fastapi.testclient import TestClient
|
2019-04-12 20:15:05 +04:00
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
items = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup_event():
|
|
|
|
|
items["foo"] = {"name": "Fighters"}
|
|
|
|
|
items["bar"] = {"name": "Tenders"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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"}
|