Fix startup runtime settings ordering and use effective database path for request DB connections
This commit is contained in:
@@ -97,22 +97,31 @@ async def get_app_context(request: Request) -> ApplicationContext:
|
||||
return _build_app_context(request)
|
||||
|
||||
|
||||
async def get_db(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> AsyncGenerator[aiosqlite.Connection, None]:
|
||||
async def get_settings(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> Settings:
|
||||
"""Provide the effective application settings for the current request."""
|
||||
return app_context.runtime_settings if app_context.runtime_settings is not None else app_context.settings
|
||||
|
||||
|
||||
async def get_db(
|
||||
settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> AsyncGenerator[aiosqlite.Connection, None]:
|
||||
"""Provide a request-scoped :class:`aiosqlite.Connection` for the current request.
|
||||
|
||||
Opens a fresh connection for every request and closes it when the request
|
||||
is finished. This avoids contention and locking issues from a single shared
|
||||
SQLite connection across concurrent requests.
|
||||
|
||||
The database path is taken from the effective application settings so
|
||||
runtime overrides stored during setup are respected.
|
||||
|
||||
Args:
|
||||
app_context: The injected shared application context.
|
||||
settings: The effective application settings for the current request.
|
||||
|
||||
Yields:
|
||||
An open :class:`aiosqlite.Connection` for the request.
|
||||
"""
|
||||
from app.db import open_db # noqa: PLC0415
|
||||
|
||||
settings = app_context.settings
|
||||
try:
|
||||
db = await open_db(settings.database_path)
|
||||
except Exception as exc:
|
||||
@@ -128,11 +137,6 @@ async def get_db(app_context: Annotated[ApplicationContext, Depends(get_app_cont
|
||||
await db.close()
|
||||
|
||||
|
||||
async def get_settings(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> Settings:
|
||||
"""Provide the effective application settings for the current request."""
|
||||
return app_context.runtime_settings if app_context.runtime_settings is not None else app_context.settings
|
||||
|
||||
|
||||
async def get_http_session(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> aiohttp.ClientSession:
|
||||
"""Provide the shared HTTP client session from application context.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
@@ -12,6 +12,7 @@ from app.config import Settings
|
||||
from app.dependencies import (
|
||||
ApplicationContext,
|
||||
get_app_context,
|
||||
get_db,
|
||||
get_http_session,
|
||||
get_scheduler,
|
||||
get_settings,
|
||||
@@ -64,6 +65,25 @@ async def test_app_context_dependency_exposes_shared_resources(test_settings: Se
|
||||
await session.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_db_uses_effective_runtime_database_path(test_settings: Settings) -> None:
|
||||
"""Database connections should use effective runtime settings when overridden."""
|
||||
runtime_settings = test_settings.model_copy(update={"database_path": "/tmp/runtime.db"})
|
||||
|
||||
mock_connection = MagicMock()
|
||||
mock_connection.close = AsyncMock()
|
||||
|
||||
with patch("app.db.open_db", new=AsyncMock(return_value=mock_connection)) as mock_open_db:
|
||||
gen = get_db(settings=runtime_settings)
|
||||
try:
|
||||
connection = await gen.__anext__()
|
||||
assert connection is mock_connection
|
||||
finally:
|
||||
await gen.aclose()
|
||||
|
||||
mock_open_db.assert_awaited_once_with("/tmp/runtime.db")
|
||||
|
||||
|
||||
def test_request_app_state_access_is_only_allowed_in_dependencies() -> None:
|
||||
app_root = Path(__file__).resolve().parents[1] / "app"
|
||||
bad_modules: list[str] = []
|
||||
|
||||
Reference in New Issue
Block a user