Skip to content

Commit

Permalink
Add example of testing lifespan to fastapi example
Browse files Browse the repository at this point in the history
  • Loading branch information
abondar committed Jul 18, 2024
1 parent 4c14800 commit 0c3e3ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
0.21
====

0.21.5 <../0.21.5>`_ - 2024-07-18
------
Added
^^^^^
- Propagate `_create_db` parameter to RegisterTortoise. (#1676)

0.21.4 <../0.21.4>`_ - 2024-07-03
------
Added
Expand Down
1 change: 1 addition & 0 deletions examples/fastapi/_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def anyio_backend() -> str:

@asynccontextmanager
async def client_manager(app, base_url="http://test", **kw) -> ClientManagerType:
app.state.testing = True
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url=base_url, **kw) as c:
Expand Down
38 changes: 34 additions & 4 deletions examples/fastapi/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
# pylint: disable=E0611,E0401
import os
from contextlib import asynccontextmanager
from typing import AsyncGenerator

from config import register_orm
from fastapi import FastAPI

from examples.fastapi.config import register_orm
from routers import router as users_router
from tortoise import Tortoise, generate_config
from tortoise.contrib.fastapi import RegisterTortoise


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
# app startup
async with register_orm(app):
async def lifespan_test(app: FastAPI) -> AsyncGenerator[None, None]:
config = generate_config(
os.getenv("TORTOISE_TEST_DB", ""),
app_modules={"models": ["models"]},
testing=True,
connection_label="models",
)
async with RegisterTortoise(
app=app,
config=config,
generate_schemas=True,
add_exception_handlers=True,
_create_db=True,
):
# db connected
yield
# app teardown
# db connections closed
await Tortoise._drop_databases()


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
if getattr(app.state, "testing", None):
async with lifespan_test(app) as _:
yield
else:
# app startup
async with register_orm(app):
# db connected
yield
# app teardown
# db connections closed


app = FastAPI(title="Tortoise ORM FastAPI example", lifespan=lifespan)
Expand Down
3 changes: 3 additions & 0 deletions tortoise/contrib/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def __init__(
add_exception_handlers: bool = False,
use_tz: bool = False,
timezone: str = "UTC",
_create_db: bool = False,
) -> None:
self.app = app
self.config = config
Expand All @@ -117,6 +118,7 @@ def __init__(
self.generate_schemas = generate_schemas
self.use_tz = use_tz
self.timezone = timezone
self._create_db = _create_db

if add_exception_handlers:

Expand All @@ -139,6 +141,7 @@ async def init_orm(self) -> None: # pylint: disable=W0612
modules=self.modules,
use_tz=self.use_tz,
timezone=self.timezone,
_create_db=self._create_db,
)
logger.info("Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps)
if self.generate_schemas:
Expand Down

0 comments on commit 0c3e3ad

Please sign in to comment.