# #region TestSessionConfig [C:2] [TYPE Module] [SEMANTICS test, conftest, db] # @BRIEF Shared pytest fixtures and session configuration for backend tests. # @RELATION BINDS_TO -> [init_db] # @PRE All test modules use sys.path patching to import from src/. # @POST Database tables exist before test session executes. # @RATIONALE AUTH_SECRET_KEY/AUTH_DATABASE_URL/DATABASE_URL/DEV_MODE must be set before # any project import because src.core.auth.config creates an AuthConfig() # singleton at module level that crashes without these env vars (crash-early fix). import os import sys from pathlib import Path # Set env defaults for module-level AuthConfig() singleton — crash-early fix os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing") os.environ.setdefault("AUTH_DATABASE_URL", "sqlite:///:memory:test_auth") os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:test_main") os.environ.setdefault("DEV_MODE", "true") sys.path.insert(0, str(Path(__file__).parent.parent / "src")) import pytest @pytest.fixture(scope="session", autouse=True) def ensure_db_tables(): """Ensure all tables exist before test session. This fixture runs once per test session, creating all registered SQLAlchemy tables. Individual test modules may also create their own in-memory engines — this guarantees the real engine schema is initialized for tests that depend on it. NOTE: src.core.database may fail to import due to a pydantic-settings v2 compatibility issue with the deprecated Field(env=...) parameter in AuthConfig. Tests that need the database import it directly and manage the env themselves. """ try: from src.core.database import init_db init_db() except Exception: pass # Graceful degradation — tests requiring DB manage env themselves # #endregion TestSessionConfig