7a34807fa0
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
"""
|
|
pytest configuration: isolate each test with a temporary data directory.
|
|
"""
|
|
import os
|
|
import json
|
|
import pytest
|
|
import tempfile
|
|
import shutil
|
|
from pathlib import Path
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def isolated_data_dir(monkeypatch, tmp_path):
|
|
"""Each test gets its own clean data directory."""
|
|
data_dir = tmp_path / "data"
|
|
(data_dir / "uploads").mkdir(parents=True)
|
|
(data_dir / "metadata").mkdir(parents=True)
|
|
(data_dir / "topics.json").write_text(json.dumps({"topics": []}))
|
|
|
|
from config import DEFAULT_SETTINGS
|
|
(data_dir / "settings.json").write_text(json.dumps(DEFAULT_SETTINGS))
|
|
|
|
monkeypatch.setenv("DATA_DIR", str(data_dir))
|
|
|
|
# Patch the module-level path constants so the running app sees the temp dir
|
|
import config
|
|
monkeypatch.setattr(config, "DATA_DIR", data_dir)
|
|
monkeypatch.setattr(config, "UPLOADS_DIR", data_dir / "uploads")
|
|
monkeypatch.setattr(config, "METADATA_DIR", data_dir / "metadata")
|
|
monkeypatch.setattr(config, "TOPICS_FILE", data_dir / "topics.json")
|
|
monkeypatch.setattr(config, "SETTINGS_FILE", data_dir / "settings.json")
|
|
|
|
import services.storage as st
|
|
from filelock import FileLock
|
|
monkeypatch.setattr(st, "UPLOADS_DIR", data_dir / "uploads")
|
|
monkeypatch.setattr(st, "METADATA_DIR", data_dir / "metadata")
|
|
monkeypatch.setattr(st, "TOPICS_FILE", data_dir / "topics.json")
|
|
monkeypatch.setattr(st, "SETTINGS_FILE", data_dir / "settings.json")
|
|
monkeypatch.setattr(st, "_topics_lock", FileLock(str(data_dir / "topics.json") + ".lock"))
|
|
monkeypatch.setattr(st, "_settings_lock", FileLock(str(data_dir / "settings.json") + ".lock"))
|
|
|
|
yield data_dir
|
|
|
|
|
|
@pytest.fixture
|
|
def client(isolated_data_dir):
|
|
from main import app
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_txt(tmp_path):
|
|
p = tmp_path / "sample.txt"
|
|
p.write_text("This is a test document about invoices and finance.")
|
|
return p
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_pdf(tmp_path):
|
|
"""Create a minimal valid PDF for testing."""
|
|
import fitz
|
|
doc = fitz.open()
|
|
page = doc.new_page()
|
|
page.insert_text((50, 50), "Test PDF document about contracts and legal matters.")
|
|
pdf_path = tmp_path / "sample.pdf"
|
|
doc.save(str(pdf_path))
|
|
doc.close()
|
|
return pdf_path
|