mirror of https://github.com/tiangolo/fastapi.git
Merge cbbed039b1 into 0127069d47
This commit is contained in:
commit
6a18ddf1ab
|
|
@ -0,0 +1,7 @@
|
|||
from decimal import Decimal
|
||||
|
||||
from fastapi import Query
|
||||
|
||||
|
||||
def create_item(price: Decimal = Query(..., max_digits=5, decimal_places=2)):
|
||||
return {"price": price}
|
||||
|
|
@ -94,6 +94,7 @@ As the testing function is now asynchronous, you can now also call (and `await`)
|
|||
|
||||
/// tip
|
||||
|
||||
If you encounter a `RuntimeError: Task attached to a different loop` when integrating asynchronous function calls in your tests (e.g. when using <a href="https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop" class="external-link" target="_blank">MongoDB's MotorClient</a>), remember to instantiate objects that need an event loop only within async functions, for example, using a lifespan async context manager.
|
||||
If you encounter a `RuntimeError: Task attached to a different loop` when integrating asynchronous function calls in your tests (e.g. when using [MongoDB's MotorClient](https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop)), remember to instantiate objects that need an event loop only within async functions, e.g. an `@app.on_event("startup")` callback.
|
||||
|
||||
///
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
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"}
|
||||
|
|
@ -1,15 +1,19 @@
|
|||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
items = {}
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
items["foo"] = {"name": "Fighters"}
|
||||
items["bar"] = {"name": "Tenders"}
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
import pytest
|
||||
from docs_src.app_testing import tutorial003, tutorial003_py310
|
||||
|
||||
|
||||
def test_main():
|
||||
with pytest.warns(DeprecationWarning):
|
||||
from docs_src.app_testing.tutorial003_py310 import test_read_items
|
||||
test_read_items()
|
||||
def test_tutorial003():
|
||||
# This covers the base version (tutorial003.py)
|
||||
tutorial003.test_read_items()
|
||||
|
||||
|
||||
def test_tutorial003_py310():
|
||||
# This covers the modern version (tutorial003_py310.py)
|
||||
tutorial003_py310.test_read_items()
|
||||
|
|
|
|||
Loading…
Reference in New Issue