pagepiper/tests/conftest.py

48 lines
1.4 KiB
Python

# tests/conftest.py
"""Shared fixtures for pagepiper test suite."""
from __future__ import annotations
import sqlite3
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
@pytest.fixture
def test_db(tmp_path) -> str:
db_path = str(tmp_path / "test.db")
schema = Path("migrations/001_initial_schema.sql").read_text()
conn = sqlite3.connect(db_path)
conn.executescript(schema)
conn.commit()
conn.close()
return db_path
@pytest.fixture
def client(test_db, tmp_path, monkeypatch):
monkeypatch.setenv("PAGEPIPER_DATA_DIR", str(tmp_path))
monkeypatch.setenv("PAGEPIPER_WATCH_DIR", str(tmp_path / "books"))
(tmp_path / "books").mkdir(exist_ok=True)
import app.main as _main_module
from app.main import app, _bm25
from app.deps import get_db
# Suppress migrations during tests — test_db fixture already applies the schema
monkeypatch.setattr(_main_module, "_apply_migrations", lambda: None)
def override_db():
conn = sqlite3.connect(test_db)
conn.execute("PRAGMA foreign_keys = ON")
conn.row_factory = sqlite3.Row
try:
yield conn
finally:
conn.close()
app.dependency_overrides[get_db] = override_db
_bm25.mark_dirty() # clear any state from previous tests
yield TestClient(app)
app.dependency_overrides.clear()