test_retrieval_confidence.py
Ricky/RAG_project/tests/test_retrieval_confidence.py
"""
Unit tests for retrieval/confidence.py — pure functions, no network required.
"""
import pytest
from langchain_core.documents import Document
from retrieval.confidence import (
build_vector_score_map,
token_overlap_ratio,
compute_text_confidence,
)
from retrieval.fusion import doc_identity_key
def _doc(content: str, source: str = "test.pdf", page=None) -> Document:
meta = {"source": source}
if page is not None:
meta["page"] = page
return Document(page_content=content, metadata=meta)
class TestBuildVectorScoreMap:
def test_normalizes_score(self):
doc = _doc("content")
score_map = build_vector_score_map([(doc, 0.85)])
key = doc_identity_key(doc)
assert score_map[key] == pytest.approx(0.85)
def test_clamps_above_one(self):
doc = _doc("content")
score_map = build_vector_score_map([(doc, 1.5)])
key = doc_identity_key(doc)
assert score_map[key] == 1.0
def test_clamps_below_zero(self):
doc = _doc("content")
score_map = build_vector_score_map([(doc, -0.1)])
key = doc_identity_key(doc)
assert score_map[key] == 0.0
def test_keeps_best_score_for_duplicates(self):
doc = _doc("content")
score_map = build_vector_score_map([(doc, 0.5), (doc, 0.9)])
key = doc_identity_key(doc)
assert score_map[key] == pytest.approx(0.9)
class TestTokenOverlapRatio:
def test_full_overlap(self):
doc = _doc("coolfire vibration watch alarm", source="coolfire watch.pdf")
ratio = token_overlap_ratio("coolfire watch alarm", doc)
assert ratio > 0.8
def test_no_overlap(self):
doc = _doc("unrelated content here", source="other.pdf")
ratio = token_overlap_ratio("coolfire watch alarm", doc)
assert ratio == 0.0
def test_empty_question(self):
doc = _doc("some content")
ratio = token_overlap_ratio("", doc)
assert ratio == 0.0
def test_result_bounded(self):
doc = _doc("word word word word word")
ratio = token_overlap_ratio("word word word", doc)
assert 0.0 <= ratio <= 1.0
class TestComputeTextConfidence:
def test_empty_docs_returns_zero(self):
assert compute_text_confidence("question", [], {}) == 0.0
def test_high_score_raises_confidence(self):
doc = _doc("coolfire alarm instructions", source="coolfire watch.pdf")
key = doc_identity_key(doc)
score_map = {key: 0.95}
conf = compute_text_confidence("coolfire alarm", [doc], score_map)
assert conf > 0.5
def test_zero_score_lowers_confidence(self):
doc = _doc("some content", source="unrelated.pdf")
key = doc_identity_key(doc)
score_map = {key: 0.0}
conf = compute_text_confidence("coolfire alarm", [doc], score_map)
assert conf < 0.7
def test_result_bounded(self):
doc = _doc("coolfire watch", source="coolfire.pdf")
key = doc_identity_key(doc)
score_map = {key: 1.0}
conf = compute_text_confidence("coolfire watch", [doc], score_map)
assert 0.0 <= conf <= 1.0
Bài viết liên quan
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
Đọc bài viết →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
Đọc bài viết →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
Đọc bài viết →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
Đọc bài viết →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
Đọc bài viết →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
Đọc bài viết →