Browse Source

increase test coverage

pull/9630/head
Lancetnik 2 years ago
parent
commit
52ebb5dcd6
  1. 13
      tests/test_router_events.py

13
tests/test_router_events.py

@ -2,7 +2,7 @@ from contextlib import asynccontextmanager
from typing import AsyncGenerator, Dict from typing import AsyncGenerator, Dict
import pytest import pytest
from fastapi import APIRouter, FastAPI from fastapi import APIRouter, FastAPI, Request
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from pydantic import BaseModel from pydantic import BaseModel
@ -112,19 +112,19 @@ def test_router_nested_lifespan_state(state: State) -> None:
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
state.app_startup = True state.app_startup = True
yield yield {"app": True}
state.app_shutdown = True state.app_shutdown = True
@asynccontextmanager @asynccontextmanager
async def router_lifespan(app: FastAPI) -> AsyncGenerator[None, None]: async def router_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
state.router_startup = True state.router_startup = True
yield yield {"router": True}
state.router_shutdown = True state.router_shutdown = True
@asynccontextmanager @asynccontextmanager
async def subrouter_lifespan(app: FastAPI) -> AsyncGenerator[None, None]: async def subrouter_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
state.sub_router_startup = True state.sub_router_startup = True
yield yield {"sub_router": True}
state.sub_router_shutdown = True state.sub_router_shutdown = True
sub_router = APIRouter(lifespan=subrouter_lifespan) sub_router = APIRouter(lifespan=subrouter_lifespan)
@ -136,7 +136,10 @@ def test_router_nested_lifespan_state(state: State) -> None:
app.include_router(router) app.include_router(router)
@app.get("/") @app.get("/")
def main() -> Dict[str, str]: def main(request: Request) -> Dict[str, str]:
assert request.state.app
assert request.state.router
assert request.state.sub_router
return {"message": "Hello World"} return {"message": "Hello World"}
assert state.app_startup is False assert state.app_startup is False

Loading…
Cancel
Save