| 1 |
from collections.abc import AsyncGenerator |
| 2 |
from contextlib import asynccontextmanager |
| 3 |
|
| 4 |
import pytest |
| 5 |
from fastapi import APIRouter, FastAPI, Request |
| 6 |
from fastapi.testclient import TestClient |
| 7 |
from pydantic import BaseModel |
| 8 |
|
| 9 |
|
| 10 |
class State(BaseModel): |
| 11 |
app_startup: bool = False |
| 12 |
app_shutdown: bool = False |
| 13 |
router_startup: bool = False |
| 14 |
router_shutdown: bool = False |
| 15 |
sub_router_startup: bool = False |
| 16 |
sub_router_shutdown: bool = False |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture |
| 20 |
def state() -> State: |
| 21 |
return State() |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.filterwarnings( |
| 25 |
r"ignore:\s*on_event is deprecated, use lifespan event handlers instead.*:DeprecationWarning" |
| 26 |
) |
| 27 |
def test_router_events(state: State) -> None: |
| 28 |
app = FastAPI() |
| 29 |
|
| 30 |
@app.get("/") |
| 31 |
def main() -> dict[str, str]: |
| 32 |
return {"message": "Hello World"} |
| 33 |
|
| 34 |
@app.on_event("startup") # ty: ignore[deprecated] |
| 35 |
def app_startup() -> None: |
| 36 |
state.app_startup = True |
| 37 |
|
| 38 |
@app.on_event("shutdown") # ty: ignore[deprecated] |
| 39 |
def app_shutdown() -> None: |
| 40 |
state.app_shutdown = True |
| 41 |
|
| 42 |
router = APIRouter() |
| 43 |
|
| 44 |
@router.on_event("startup") # ty: ignore[deprecated] |
| 45 |
def router_startup() -> None: |
| 46 |
state.router_startup = True |
| 47 |
|
| 48 |
@router.on_event("shutdown") # ty: ignore[deprecated] |
| 49 |
def router_shutdown() -> None: |
| 50 |
state.router_shutdown = True |
| 51 |
|
| 52 |
sub_router = APIRouter() |
| 53 |
|
| 54 |
@sub_router.on_event("startup") # ty: ignore[deprecated] |
| 55 |
def sub_router_startup() -> None: |
| 56 |
state.sub_router_startup = True |
| 57 |
|
| 58 |
@sub_router.on_event("shutdown") # ty: ignore[deprecated] |
| 59 |
def sub_router_shutdown() -> None: |
| 60 |
state.sub_router_shutdown = True |
| 61 |
|
| 62 |
router.include_router(sub_router) |
| 63 |
app.include_router(router) |
| 64 |
|
| 65 |
assert state.app_startup is False |
| 66 |
assert state.router_startup is False |
| 67 |
assert state.sub_router_startup is False |
| 68 |
assert state.app_shutdown is False |
| 69 |
assert state.router_shutdown is False |
| 70 |
assert state.sub_router_shutdown is False |
| 71 |
with TestClient(app) as client: |
| 72 |
assert state.app_startup is True |
| 73 |
assert state.router_startup is True |
| 74 |
assert state.sub_router_startup is True |
| 75 |
assert state.app_shutdown is False |
| 76 |
assert state.router_shutdown is False |
| 77 |
assert state.sub_router_shutdown is False |
| 78 |
response = client.get("/") |
| 79 |
assert response.status_code == 200, response.text |
| 80 |
assert response.json() == {"message": "Hello World"} |
| 81 |
assert state.app_startup is True |
| 82 |
assert state.router_startup is True |
| 83 |
assert state.sub_router_startup is True |
| 84 |
assert state.app_shutdown is True |
| 85 |
assert state.router_shutdown is True |
| 86 |
assert state.sub_router_shutdown is True |
| 87 |
|
| 88 |
|
| 89 |
def test_app_lifespan_state(state: State) -> None: |
| 90 |
@asynccontextmanager |
| 91 |
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 92 |
state.app_startup = True |
| 93 |
yield |
| 94 |
state.app_shutdown = True |
| 95 |
|
| 96 |
app = FastAPI(lifespan=lifespan) |
| 97 |
|
| 98 |
@app.get("/") |
| 99 |
def main() -> dict[str, str]: |
| 100 |
return {"message": "Hello World"} |
| 101 |
|
| 102 |
assert state.app_startup is False |
| 103 |
assert state.app_shutdown is False |
| 104 |
with TestClient(app) as client: |
| 105 |
assert state.app_startup is True |
| 106 |
assert state.app_shutdown is False |
| 107 |
response = client.get("/") |
| 108 |
assert response.status_code == 200, response.text |
| 109 |
assert response.json() == {"message": "Hello World"} |
| 110 |
assert state.app_startup is True |
| 111 |
assert state.app_shutdown is True |
| 112 |
|
| 113 |
|
| 114 |
def test_router_nested_lifespan_state(state: State) -> None: |
| 115 |
@asynccontextmanager |
| 116 |
async def lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]: |
| 117 |
state.app_startup = True |
| 118 |
yield {"app": True} |
| 119 |
state.app_shutdown = True |
| 120 |
|
| 121 |
@asynccontextmanager |
| 122 |
async def router_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]: |
| 123 |
state.router_startup = True |
| 124 |
yield {"router": True} |
| 125 |
state.router_shutdown = True |
| 126 |
|
| 127 |
@asynccontextmanager |
| 128 |
async def subrouter_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]: |
| 129 |
state.sub_router_startup = True |
| 130 |
yield {"sub_router": True} |
| 131 |
state.sub_router_shutdown = True |
| 132 |
|
| 133 |
sub_router = APIRouter(lifespan=subrouter_lifespan) |
| 134 |
|
| 135 |
router = APIRouter(lifespan=router_lifespan) |
| 136 |
router.include_router(sub_router) |
| 137 |
|
| 138 |
app = FastAPI(lifespan=lifespan) |
| 139 |
app.include_router(router) |
| 140 |
|
| 141 |
@app.get("/") |
| 142 |
def main(request: Request) -> dict[str, str]: |
| 143 |
assert request.state.app |
| 144 |
assert request.state.router |
| 145 |
assert request.state.sub_router |
| 146 |
return {"message": "Hello World"} |
| 147 |
|
| 148 |
assert state.app_startup is False |
| 149 |
assert state.router_startup is False |
| 150 |
assert state.sub_router_startup is False |
| 151 |
assert state.app_shutdown is False |
| 152 |
assert state.router_shutdown is False |
| 153 |
assert state.sub_router_shutdown is False |
| 154 |
|
| 155 |
with TestClient(app) as client: |
| 156 |
assert state.app_startup is True |
| 157 |
assert state.router_startup is True |
| 158 |
assert state.sub_router_startup is True |
| 159 |
assert state.app_shutdown is False |
| 160 |
assert state.router_shutdown is False |
| 161 |
assert state.sub_router_shutdown is False |
| 162 |
response = client.get("/") |
| 163 |
assert response.status_code == 200, response.text |
| 164 |
assert response.json() == {"message": "Hello World"} |
| 165 |
|
| 166 |
assert state.app_startup is True |
| 167 |
assert state.router_startup is True |
| 168 |
assert state.sub_router_startup is True |
| 169 |
assert state.app_shutdown is True |
| 170 |
assert state.router_shutdown is True |
| 171 |
assert state.sub_router_shutdown is True |
| 172 |
|
| 173 |
|
| 174 |
def test_router_nested_lifespan_state_overriding_by_parent() -> None: |
| 175 |
@asynccontextmanager |
| 176 |
async def lifespan( |
| 177 |
app: FastAPI, |
| 178 |
) -> AsyncGenerator[dict[str, str | bool], None]: |
| 179 |
yield { |
| 180 |
"app_specific": True, |
| 181 |
"overridden": "app", |
| 182 |
} |
| 183 |
|
| 184 |
@asynccontextmanager |
| 185 |
async def router_lifespan( |
| 186 |
app: FastAPI, |
| 187 |
) -> AsyncGenerator[dict[str, str | bool], None]: |
| 188 |
yield { |
| 189 |
"router_specific": True, |
| 190 |
"overridden": "router", # should override parent |
| 191 |
} |
| 192 |
|
| 193 |
router = APIRouter(lifespan=router_lifespan) |
| 194 |
app = FastAPI(lifespan=lifespan) |
| 195 |
app.include_router(router) |
| 196 |
|
| 197 |
with TestClient(app) as client: |
| 198 |
assert client.app_state == { |
| 199 |
"app_specific": True, |
| 200 |
"router_specific": True, |
| 201 |
"overridden": "app", |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
def test_merged_no_return_lifespans_return_none() -> None: |
| 206 |
@asynccontextmanager |
| 207 |
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 208 |
yield |
| 209 |
|
| 210 |
@asynccontextmanager |
| 211 |
async def router_lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 212 |
yield |
| 213 |
|
| 214 |
router = APIRouter(lifespan=router_lifespan) |
| 215 |
app = FastAPI(lifespan=lifespan) |
| 216 |
app.include_router(router) |
| 217 |
|
| 218 |
with TestClient(app) as client: |
| 219 |
assert not client.app_state |
| 220 |
|
| 221 |
|
| 222 |
def test_merged_mixed_state_lifespans() -> None: |
| 223 |
@asynccontextmanager |
| 224 |
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 225 |
yield |
| 226 |
|
| 227 |
@asynccontextmanager |
| 228 |
async def router_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]: |
| 229 |
yield {"router": True} |
| 230 |
|
| 231 |
@asynccontextmanager |
| 232 |
async def sub_router_lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 233 |
yield |
| 234 |
|
| 235 |
sub_router = APIRouter(lifespan=sub_router_lifespan) |
| 236 |
router = APIRouter(lifespan=router_lifespan) |
| 237 |
app = FastAPI(lifespan=lifespan) |
| 238 |
router.include_router(sub_router) |
| 239 |
app.include_router(router) |
| 240 |
|
| 241 |
with TestClient(app) as client: |
| 242 |
assert client.app_state == {"router": True} |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.filterwarnings( |
| 246 |
r"ignore:\s*on_event is deprecated, use lifespan event handlers instead.*:DeprecationWarning" |
| 247 |
) |
| 248 |
def test_router_async_shutdown_handler(state: State) -> None: |
| 249 |
"""Test that async on_shutdown event handlers are called correctly, for coverage.""" |
| 250 |
app = FastAPI() |
| 251 |
|
| 252 |
@app.get("/") |
| 253 |
def main() -> dict[str, str]: |
| 254 |
return {"message": "Hello World"} |
| 255 |
|
| 256 |
@app.on_event("shutdown") # ty: ignore[deprecated] |
| 257 |
async def app_shutdown() -> None: |
| 258 |
state.app_shutdown = True |
| 259 |
|
| 260 |
assert state.app_shutdown is False |
| 261 |
with TestClient(app) as client: |
| 262 |
assert state.app_shutdown is False |
| 263 |
response = client.get("/") |
| 264 |
assert response.status_code == 200, response.text |
| 265 |
assert state.app_shutdown is True |
| 266 |
|
| 267 |
|
| 268 |
def test_router_sync_generator_lifespan(state: State) -> None: |
| 269 |
"""Test that a sync generator lifespan works via _wrap_gen_lifespan_context.""" |
| 270 |
from collections.abc import Generator |
| 271 |
|
| 272 |
def lifespan(app: FastAPI) -> Generator[None, None, None]: |
| 273 |
state.app_startup = True |
| 274 |
yield |
| 275 |
state.app_shutdown = True |
| 276 |
|
| 277 |
app = FastAPI(lifespan=lifespan) # type: ignore[invalid-argument-type] # ty: ignore[invalid-argument-type] |
| 278 |
|
| 279 |
@app.get("/") |
| 280 |
def main() -> dict[str, str]: |
| 281 |
return {"message": "Hello World"} |
| 282 |
|
| 283 |
assert state.app_startup is False |
| 284 |
assert state.app_shutdown is False |
| 285 |
with TestClient(app) as client: |
| 286 |
assert state.app_startup is True |
| 287 |
assert state.app_shutdown is False |
| 288 |
response = client.get("/") |
| 289 |
assert response.status_code == 200, response.text |
| 290 |
assert response.json() == {"message": "Hello World"} |
| 291 |
assert state.app_startup is True |
| 292 |
assert state.app_shutdown is True |
| 293 |
|
| 294 |
|
| 295 |
def test_router_async_generator_lifespan(state: State) -> None: |
| 296 |
"""Test that an async generator lifespan (not wrapped) works.""" |
| 297 |
|
| 298 |
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
| 299 |
state.app_startup = True |
| 300 |
yield |
| 301 |
state.app_shutdown = True |
| 302 |
|
| 303 |
app = FastAPI(lifespan=lifespan) # type: ignore[invalid-argument-type] # ty: ignore[invalid-argument-type] |
| 304 |
|
| 305 |
@app.get("/") |
| 306 |
def main() -> dict[str, str]: |
| 307 |
return {"message": "Hello World"} |
| 308 |
|
| 309 |
assert state.app_startup is False |
| 310 |
assert state.app_shutdown is False |
| 311 |
with TestClient(app) as client: |
| 312 |
assert state.app_startup is True |
| 313 |
assert state.app_shutdown is False |
| 314 |
response = client.get("/") |
| 315 |
assert response.status_code == 200, response.text |
| 316 |
assert response.json() == {"message": "Hello World"} |
| 317 |
assert state.app_startup is True |
| 318 |
assert state.app_shutdown is True |
| 319 |
|
| 320 |
|
| 321 |
def test_startup_shutdown_handlers_as_parameters(state: State) -> None: |
| 322 |
"""Test that startup/shutdown handlers passed as parameters to FastAPI are called correctly.""" |
| 323 |
|
| 324 |
def app_startup() -> None: |
| 325 |
state.app_startup = True |
| 326 |
|
| 327 |
def app_shutdown() -> None: |
| 328 |
state.app_shutdown = True |
| 329 |
|
| 330 |
app = FastAPI(on_startup=[app_startup], on_shutdown=[app_shutdown]) |
| 331 |
|
| 332 |
@app.get("/") |
| 333 |
def main() -> dict[str, str]: |
| 334 |
return {"message": "Hello World"} |
| 335 |
|
| 336 |
def router_startup() -> None: |
| 337 |
state.router_startup = True |
| 338 |
|
| 339 |
def router_shutdown() -> None: |
| 340 |
state.router_shutdown = True |
| 341 |
|
| 342 |
router = APIRouter(on_startup=[router_startup], on_shutdown=[router_shutdown]) |
| 343 |
|
| 344 |
def sub_router_startup() -> None: |
| 345 |
state.sub_router_startup = True |
| 346 |
|
| 347 |
def sub_router_shutdown() -> None: |
| 348 |
state.sub_router_shutdown = True |
| 349 |
|
| 350 |
sub_router = APIRouter( |
| 351 |
on_startup=[sub_router_startup], on_shutdown=[sub_router_shutdown] |
| 352 |
) |
| 353 |
|
| 354 |
router.include_router(sub_router) |
| 355 |
app.include_router(router) |
| 356 |
|
| 357 |
assert state.app_startup is False |
| 358 |
assert state.router_startup is False |
| 359 |
assert state.sub_router_startup is False |
| 360 |
assert state.app_shutdown is False |
| 361 |
assert state.router_shutdown is False |
| 362 |
assert state.sub_router_shutdown is False |
| 363 |
with TestClient(app) as client: |
| 364 |
assert state.app_startup is True |
| 365 |
assert state.router_startup is True |
| 366 |
assert state.sub_router_startup is True |
| 367 |
assert state.app_shutdown is False |
| 368 |
assert state.router_shutdown is False |
| 369 |
assert state.sub_router_shutdown is False |
| 370 |
response = client.get("/") |
| 371 |
assert response.status_code == 200, response.text |
| 372 |
assert response.json() == {"message": "Hello World"} |
| 373 |
assert state.app_startup is True |
| 374 |
assert state.router_startup is True |
| 375 |
assert state.sub_router_startup is True |
| 376 |
assert state.app_shutdown is True |
| 377 |
assert state.router_shutdown is True |
| 378 |
assert state.sub_router_shutdown is True |