"""Test fixtures. The engine is built at import time from the environment, so the database URL has to be set before anything from `app` is imported — hence the env juggling here rather than in a fixture. """ from __future__ import annotations import os import tempfile import pytest # Before anything imports the settings: no `.env`, ever. A developer's real # file would otherwise point the suite at live Jira, Telegram and laundry. os.environ["KADENCE_NO_DOTENV"] = "1" os.environ.setdefault("DATABASE_URL", "sqlite:///" + tempfile.mktemp(suffix=".db")) os.environ["SCHEDULER_ENABLED"] = "false" os.environ["TELEGRAM_BOT_ENABLED"] = "false" # Keep the seeded settings empty so tests never reach a real Jira/Todoist. for name in ( "JIRA_BASE_URL", "JIRA_EMAIL", "JIRA_API_TOKEN", "TODOIST_API_TOKEN", "GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET", "GOOGLE_REFRESH_TOKEN", "LAUNDRY_SESSION", "TELEGRAM_BOT_TOKEN", "TELEGRAM_CHAT_ID", "SLACK_WEBHOOK_URL", "AUTH_PASSWORD", "TELEGRAM_OWNER_ID", "TELEGRAM_BOT_USERNAME", "API_TOKEN", ): os.environ.pop(name, None) from app.db import Base, SessionLocal, engine, init_db # noqa: E402 from app.services import settings_service # noqa: E402 from app.settings_schema import AppSettings # noqa: E402 @pytest.fixture() def session(): Base.metadata.drop_all(bind=engine) init_db() db = SessionLocal() try: yield db finally: db.close() @pytest.fixture() def settings(session) -> AppSettings: return settings_service.load_settings(session) @pytest.fixture() def client(): from fastapi.testclient import TestClient from app.main import app Base.metadata.drop_all(bind=engine) init_db() with TestClient(app) as test_client: yield test_client