"""Shared pytest fixtures. Provides a SQLite-backed test database. We point DATABASE_URL at a temp file before any `src.db` import so the engine binds to the right URL. """ from __future__ import annotations import os import pytest @pytest.fixture(scope="session", autouse=True) def _set_database_url(tmp_path_factory): """Set DATABASE_URL → SQLite temp file for the whole test session.""" db_path = tmp_path_factory.mktemp("db") / "test.sqlite" os.environ["DATABASE_URL"] = f"sqlite+pysqlite:///{db_path}" yield @pytest.fixture(autouse=True) def _reset_db(): """Create a fresh schema for each test.""" # Import here so DATABASE_URL has been set first. from src import db as db_module # Force a fresh engine in case a previous test mutated module state. db_module._engine = None db_module._SessionLocal = None engine = db_module.get_engine() db_module.Base.metadata.drop_all(engine) db_module.Base.metadata.create_all(engine) yield