FederalPolicy-RAG / tests /test_context.py
AaTekle
Initial Federal Policy RAG Assistant
4298e57
Raw
History Blame Contribute Delete
1.74 kB
"""
Tests RAG context formatting and source citation consistency
Use case:
Verifies that retrieved document chunks are assigned stable citation
markers, such as [S1] and [S2], along with relevant page references
Stable citations:
Stable citations are predictable source labels that consistently map each
retrieved chunk to a reference in the generated context
Why:
They make generated answers traceable to their source material and help
users verify where supporting information came from
"""
from document_loader import Chunk
from rag_engine import SearchHit, build_context
def test_context_has_stable_source_markers():
"""verifying source markers and page references are preserved in RAG context"""
# creating retrieval results with source metadata and relevance scores
'''
Unique chunk ID = 1 (chunk id)
Alpha = Sample text stored in the first chunk
Policy A,B = Name of the source document
page = Page associated with the chunk
0.9,0.8 = Retrieval relevance score for this chunk
'''
hits = [
SearchHit(
Chunk("1", "Alpha", "Policy A", "TXT", page=2), #Alpha / Beta → dummy document content used only for testing
0.9,
),
SearchHit(
Chunk("2", "Beta", "Policy B", "PDF", page=4),
0.8,
),
]
# building the formatted context passed to the RAG model
context = build_context(hits)
# making sure the first retrieved source receives the [S1] marker
assert "[S1]" in context
# making sure the second retrieved source receives the [S2] marker
assert "[S2]" in context
# making sure page metadata is included for source traceability
assert "p. 2" in context