cascade_risk / tests /conftest.py
Lucasoppem's picture
Sync from GitHub main (part 2)
36f9d47 verified
Raw
History Blame Contribute Delete
1.89 kB
"""Shared pytest fixtures for evaluation-framework tests."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from src.llm.client import LLMClient
class FakeLLMClient(LLMClient):
"""LLM client that returns a canned response per call.
Tests pass `responses` as a list; each call pops the first item.
"""
def __init__(self, responses: list[str]):
self.responses = list(responses)
self.calls: list[dict] = []
def call(self, prompt_template: str, variables: dict, expert_knowledge: str = "") -> str:
self.calls.append({
"template": prompt_template,
"variables": variables,
"knowledge": expert_knowledge,
})
if not self.responses:
raise RuntimeError("FakeLLMClient: no more responses queued")
return self.responses.pop(0)
@pytest.fixture
def fake_llm():
"""Factory fixture: call `fake_llm([response1, response2, ...])` to create one."""
return lambda responses: FakeLLMClient(responses)
@pytest.fixture
def articles_dir(tmp_path):
"""Empty tmp articles directory. Tests populate it with `write_article`."""
d = tmp_path / "articles"
d.mkdir()
return d
@pytest.fixture
def write_article(articles_dir):
"""Write an article JSON file matching the scraper's output format.
Usage in test:
write_article(event_id="2025-0042-ITA", url="https://news/x",
title="Flood hits Milan", date="2025-06-01", text="...")
"""
def _write(event_id, url, title, date, text, hash_suffix="aaaa1111"):
filename = f"{event_id}_{hash_suffix}.json"
(articles_dir / filename).write_text(
json.dumps({"url": url, "title": title, "date": date, "text": text}),
encoding="utf-8",
)
return articles_dir / filename
return _write