generated from john/python-template
test settings
This commit is contained in:
@@ -41,6 +41,7 @@ class Settings(BaseSettings):
|
|||||||
# --- persistence ---
|
# --- persistence ---
|
||||||
database_url: str = "sqlite:///./transcription.db"
|
database_url: str = "sqlite:///./transcription.db"
|
||||||
bootstrap_schema_on_startup: bool | None = None
|
bootstrap_schema_on_startup: bool | None = None
|
||||||
|
sqlite_check_same_thread: bool = False
|
||||||
|
|
||||||
# --- filesystem paths ---
|
# --- filesystem paths ---
|
||||||
upload_dir: Path = Path("./uploads")
|
upload_dir: Path = Path("./uploads")
|
||||||
@@ -61,10 +62,10 @@ class Settings(BaseSettings):
|
|||||||
_settings: ContextVar[Settings | None] = ContextVar("settings", default=None)
|
_settings: ContextVar[Settings | None] = ContextVar("settings", default=None)
|
||||||
|
|
||||||
|
|
||||||
def get_settings() -> Settings:
|
def get_settings(**kwargs) -> Settings:
|
||||||
settings = _settings.get()
|
settings = _settings.get()
|
||||||
if settings is None:
|
if settings is None:
|
||||||
settings = Settings() # pyright: ignore[reportCallIssue]
|
settings = Settings(**kwargs) # pyright: ignore[reportCallIssue]
|
||||||
_settings.set(settings)
|
_settings.set(settings)
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
|||||||
@@ -79,24 +79,25 @@ def initialize_database_runtime(*, settings: Settings | None = None) -> Database
|
|||||||
return runtime
|
return runtime
|
||||||
|
|
||||||
|
|
||||||
def get_engine() -> AsyncEngine:
|
def get_engine(settings: Settings | None = None) -> AsyncEngine:
|
||||||
"""Return the current async SQLAlchemy engine."""
|
"""Return the current async SQLAlchemy engine."""
|
||||||
runtime = _runtime.get() or initialize_database_runtime()
|
runtime = _runtime.get() or initialize_database_runtime(settings=settings)
|
||||||
return runtime.engine
|
return runtime.engine
|
||||||
|
|
||||||
|
|
||||||
def get_session_factory() -> async_sessionmaker[AsyncSession]:
|
def get_session_factory(settings: Settings | None = None) -> async_sessionmaker[AsyncSession]:
|
||||||
"""Return the shared async session factory."""
|
"""Return the shared async session factory."""
|
||||||
runtime = _runtime.get() or initialize_database_runtime()
|
runtime = _runtime.get() or initialize_database_runtime(settings=settings)
|
||||||
return runtime.session_factory
|
return runtime.session_factory
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def get_session(
|
async def get_session(
|
||||||
*,
|
*,
|
||||||
|
settings: Settings | None = None,
|
||||||
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||||
) -> AsyncGenerator[AsyncSession]:
|
) -> AsyncGenerator[AsyncSession]:
|
||||||
"""Yield a database session and ensure cleanup."""
|
"""Yield a database session and ensure cleanup."""
|
||||||
active_session_factory = session_factory or get_session_factory()
|
active_session_factory = session_factory or get_session_factory(settings)
|
||||||
async with active_session_factory() as session:
|
async with active_session_factory() as session:
|
||||||
yield session
|
yield session
|
||||||
|
|||||||
+14
-2
@@ -11,7 +11,11 @@ from sqlmodel import SQLModel
|
|||||||
from sqlmodel import create_engine
|
from sqlmodel import create_engine
|
||||||
from sqlmodel.pool import StaticPool
|
from sqlmodel.pool import StaticPool
|
||||||
|
|
||||||
|
from transcription.config import Settings
|
||||||
|
from transcription.config import get_settings
|
||||||
|
from transcription.db.operations import create_all
|
||||||
from transcription.db.runtime import dispose_database_runtime
|
from transcription.db.runtime import dispose_database_runtime
|
||||||
|
from transcription.db.runtime import get_engine
|
||||||
from transcription.db.runtime import get_session
|
from transcription.db.runtime import get_session
|
||||||
|
|
||||||
|
|
||||||
@@ -29,9 +33,17 @@ def session():
|
|||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def async_session():
|
async def default_settings():
|
||||||
|
"""Provide default settings for tests."""
|
||||||
|
settings = get_settings(database_url="sqlite:///:memory:")
|
||||||
|
await create_all(engine=get_engine(settings=settings))
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
@pytest_asyncio.fixture
|
||||||
|
async def async_session(default_settings: Settings):
|
||||||
"""Provide a clean asynchronous database session for async tests."""
|
"""Provide a clean asynchronous database session for async tests."""
|
||||||
async with get_session() as async_session:
|
async with get_session(settings=default_settings) as async_session:
|
||||||
yield async_session
|
yield async_session
|
||||||
|
|
||||||
await dispose_database_runtime()
|
await dispose_database_runtime()
|
||||||
|
|||||||
@@ -2,15 +2,18 @@ from uuid import uuid4
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from transcription.config import Settings
|
||||||
|
from transcription.db.runtime import get_session_factory
|
||||||
from transcription.models import Job
|
from transcription.models import Job
|
||||||
from transcription.services.jobs import JobService
|
from transcription.services.jobs import JobService
|
||||||
from transcription.services.jobs import JobStatus
|
from transcription.services.jobs import JobStatus
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def job_service():
|
def job_service(default_settings: Settings) -> JobService:
|
||||||
"""Provide a JobService instance for testing."""
|
"""Provide a JobService instance for testing."""
|
||||||
return JobService()
|
session_factory = get_session_factory(settings=default_settings)
|
||||||
|
return JobService(session_factory=session_factory)
|
||||||
|
|
||||||
|
|
||||||
class TestJobService:
|
class TestJobService:
|
||||||
|
|||||||
Reference in New Issue
Block a user