import os import pytest import asyncio import aiosqlite import sys # Ensure backend folder is in path for imports backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if backend_dir not in sys.path: sys.path.insert(0, backend_dir) import db @pytest.fixture(scope="session") def event_loop(): try: loop = asyncio.get_running_loop() except RuntimeError: loop = asyncio.new_event_loop() yield loop loop.close() @pytest.fixture(autouse=True) def mock_db_path(tmp_path, monkeypatch): # Ensure all modules (db, fetcher, summarizer, main) point to the temp test db test_db_file = os.path.join(tmp_path, "test_t_youtube.db") monkeypatch.setattr(db, "DB_PATH", test_db_file) # Also monkeypatch in other modules that import DB_PATH directly import fetcher import summarizer monkeypatch.setattr(fetcher, "DB_PATH", test_db_file) monkeypatch.setattr(summarizer, "DB_PATH", test_db_file) return test_db_file @pytest.fixture async def test_db(mock_db_path): # Initialize the tables await db.init_db() async with aiosqlite.connect(mock_db_path) as conn: conn.row_factory = aiosqlite.Row yield conn