Coverage for tests/test_router_events.py: 100%
86 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from contextlib import asynccontextmanager 1abcde
2from typing import AsyncGenerator, Dict 1abcde
4import pytest 1abcde
5from fastapi import APIRouter, FastAPI 1abcde
6from fastapi.testclient import TestClient 1abcde
7from pydantic import BaseModel 1abcde
10class State(BaseModel): 1abcde
11 app_startup: bool = False 1abcde
12 app_shutdown: bool = False 1abcde
13 router_startup: bool = False 1abcde
14 router_shutdown: bool = False 1abcde
15 sub_router_startup: bool = False 1abcde
16 sub_router_shutdown: bool = False 1abcde
19@pytest.fixture 1abcde
20def state() -> State: 1abcde
21 return State() 1abcde
24@pytest.mark.filterwarnings( 1abcde
25 r"ignore:\s*on_event is deprecated, use lifespan event handlers instead.*:DeprecationWarning"
26)
27def test_router_events(state: State) -> None: 1abcde
28 app = FastAPI() 1abcde
30 @app.get("/") 1abcde
31 def main() -> Dict[str, str]: 1abcde
32 return {"message": "Hello World"} 1abcde
34 @app.on_event("startup") 1abcde
35 def app_startup() -> None: 1abcde
36 state.app_startup = True 1abcde
38 @app.on_event("shutdown") 1abcde
39 def app_shutdown() -> None: 1abcde
40 state.app_shutdown = True 1abcde
42 router = APIRouter() 1abcde
44 @router.on_event("startup") 1abcde
45 def router_startup() -> None: 1abcde
46 state.router_startup = True 1abcde
48 @router.on_event("shutdown") 1abcde
49 def router_shutdown() -> None: 1abcde
50 state.router_shutdown = True 1abcde
52 sub_router = APIRouter() 1abcde
54 @sub_router.on_event("startup") 1abcde
55 def sub_router_startup() -> None: 1abcde
56 state.sub_router_startup = True 1abcde
58 @sub_router.on_event("shutdown") 1abcde
59 def sub_router_shutdown() -> None: 1abcde
60 state.sub_router_shutdown = True 1abcde
62 router.include_router(sub_router) 1abcde
63 app.include_router(router) 1abcde
65 assert state.app_startup is False 1abcde
66 assert state.router_startup is False 1abcde
67 assert state.sub_router_startup is False 1abcde
68 assert state.app_shutdown is False 1abcde
69 assert state.router_shutdown is False 1abcde
70 assert state.sub_router_shutdown is False 1abcde
71 with TestClient(app) as client: 1abcde
72 assert state.app_startup is True 1abcde
73 assert state.router_startup is True 1abcde
74 assert state.sub_router_startup is True 1abcde
75 assert state.app_shutdown is False 1abcde
76 assert state.router_shutdown is False 1abcde
77 assert state.sub_router_shutdown is False 1abcde
78 response = client.get("/") 1abcde
79 assert response.status_code == 200, response.text 1abcde
80 assert response.json() == {"message": "Hello World"} 1abcde
81 assert state.app_startup is True 1abcde
82 assert state.router_startup is True 1abcde
83 assert state.sub_router_startup is True 1abcde
84 assert state.app_shutdown is True 1abcde
85 assert state.router_shutdown is True 1abcde
86 assert state.sub_router_shutdown is True 1abcde
89def test_app_lifespan_state(state: State) -> None: 1abcde
90 @asynccontextmanager 1abcde
91 async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: 1abcde
92 state.app_startup = True 1abcde
93 yield 1abcde
94 state.app_shutdown = True 1abcde
96 app = FastAPI(lifespan=lifespan) 1abcde
98 @app.get("/") 1abcde
99 def main() -> Dict[str, str]: 1abcde
100 return {"message": "Hello World"} 1abcde
102 assert state.app_startup is False 1abcde
103 assert state.app_shutdown is False 1abcde
104 with TestClient(app) as client: 1abcde
105 assert state.app_startup is True 1abcde
106 assert state.app_shutdown is False 1abcde
107 response = client.get("/") 1abcde
108 assert response.status_code == 200, response.text 1abcde
109 assert response.json() == {"message": "Hello World"} 1abcde
110 assert state.app_startup is True 1abcde
111 assert state.app_shutdown is True 1abcde