Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support app=None for tortoise.contrib.fastapi.RegisterTortoise #1733

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/fastapi/_tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# mypy: no-disallow-untyped-decorators
# pylint: disable=E0611,E0401
import multiprocessing
import os
from concurrent.futures import ProcessPoolExecutor
from contextlib import asynccontextmanager
from datetime import datetime
from pathlib import Path
from typing import AsyncGenerator, Tuple

import anyio
import pytest
import pytz
from asgi_lifespan import LifespanManager
Expand All @@ -16,6 +19,7 @@

os.environ["DB_URL"] = MEMORY_SQLITE
try:
from config import register_orm
from main import app
from main_custom_timezone import app as app_east
from models import Users
Expand Down Expand Up @@ -117,3 +121,18 @@ async def test_user_list(self, client_east: AsyncClient) -> None: # nosec
created_at = user_obj.created_at
assert (created_at.hour - time.hour) in [self.delta_hours, self.delta_hours - 24]
assert item.model_dump()["created_at"].hour == created_at.hour


def query_without_app(pk: int) -> int:
async def runner() -> bool:
async with register_orm():
return await Users.filter(id__gt=pk).count()

return anyio.run(runner)


def test_query_without_app():
multiprocessing.set_start_method("spawn")
with ProcessPoolExecutor(max_workers=1) as executor:
future = executor.submit(query_without_app, 0)
assert future.result() >= 0
4 changes: 2 additions & 2 deletions tortoise/contrib/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class RegisterTortoise(AbstractAsyncContextManager):

def __init__(
self,
app: FastAPI,
app: Optional[FastAPI] = None,
config: Optional[dict] = None,
config_file: Optional[str] = None,
db_url: Optional[str] = None,
Expand All @@ -120,7 +120,7 @@ def __init__(
self.timezone = timezone
self._create_db = _create_db

if add_exception_handlers:
if add_exception_handlers and app is not None:

@app.exception_handler(DoesNotExist)
async def doesnotexist_exception_handler(request: "Request", exc: DoesNotExist):
Expand Down