Add automated unit and mock integration test suite

- Configure conftest.py with pytest tmp_path DB isolation fixtures
- Add test_db.py for schema connection validations
- Add test_fetcher.py with mocked yt-dlp subprocess parser checks
- Add test_summarizer.py with mocked transcripts and Claude API requests
- Add test_main.py for FastAPI router endpoints and state updates

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Gan, Jimmy
2026-06-03 11:15:27 +08:00
parent dbd3dc0237
commit 1e67eeb25b
7 changed files with 419 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
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