Files
povtornyy-ekzamen-faq-bot-c…/tests/test_retrieval.py
T

51 lines
1.3 KiB
Python

import os
import tempfile
import shutil
import json
from pathlib import Path
import chromadb
from chromadb.config import Settings
from src.ingest import ingest_faq
from src.retriever import get_answer
def test_retrieval(tmp_path, monkeypatch):
# Mock OpenAI API key
monkeypatch.setenv("OPENAI_API_KEY", "test_key")
# Create a temporary FAQ file
faq_content = """Q: What is Python?
A: Python is a programming language.
Q: What is ChromaDB?
A: ChromaDB is a vector database."""
faq_file = tmp_path / "faq.txt"
faq_file.write_text(faq_content, encoding="utf-8")
# Initialize a temporary ChromaDB client
db_dir = tmp_path / "chromadb"
client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory=str(db_dir)
))
collection_name = "test_collection"
# Ingest
ingest_faq(faq_file, client, collection_name)
# Mock OpenAIChat to avoid real API calls
class DummyLLM:
def __call__(self, *args, **kwargs):
return "Dummy answer"
# Patch the LLM in retriever
monkeypatch.setattr("src.retriever.OpenAIChat", DummyLLM)
# Retrieve answer
answer = get_answer("What is Python?", client, collection_name)
assert answer == "Dummy answer"
# Clean up
shutil.rmtree(db_dir)