Skip to content

Commit 930cd74

Browse files
committed
unit tests
Signed-off-by: Anxhela Coba <[email protected]>
1 parent 0871751 commit 930cd74

File tree

2 files changed

+206
-1
lines changed

2 files changed

+206
-1
lines changed

tests/unit/models/responses/test_query_response.py

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Unit tests for QueryResponse model."""
22

3-
from models.responses import QueryResponse
3+
from models.responses import QueryResponse, RAGChunk, ToolCall, ReferencedDocument
44

55

66
class TestQueryResponse:
@@ -20,3 +20,110 @@ def test_optional_conversation_id(self) -> None:
2020
qr = QueryResponse(response="LLM answer")
2121
assert qr.conversation_id is None
2222
assert qr.response == "LLM answer"
23+
24+
def test_rag_chunks_empty_by_default(self) -> None:
25+
"""Test that rag_chunks is empty by default."""
26+
qr = QueryResponse(response="LLM answer")
27+
assert not qr.rag_chunks
28+
29+
def test_rag_chunks_with_data(self) -> None:
30+
"""Test QueryResponse with RAG chunks."""
31+
rag_chunks = [
32+
RAGChunk(
33+
content="Kubernetes is an open-source container orchestration system",
34+
source="kubernetes-docs/overview.md",
35+
score=0.95,
36+
),
37+
RAGChunk(
38+
content="Container orchestration automates deployment and management",
39+
source="kubernetes-docs/concepts.md",
40+
score=0.87,
41+
),
42+
]
43+
44+
qr = QueryResponse(
45+
conversation_id="123e4567-e89b-12d3-a456-426614174000",
46+
response="LLM answer with RAG context",
47+
rag_chunks=rag_chunks,
48+
)
49+
50+
assert len(qr.rag_chunks) == 2
51+
assert (
52+
qr.rag_chunks[0].content
53+
== "Kubernetes is an open-source container orchestration system"
54+
)
55+
assert qr.rag_chunks[0].source == "kubernetes-docs/overview.md"
56+
assert qr.rag_chunks[0].score == 0.95
57+
assert (
58+
qr.rag_chunks[1].content
59+
== "Container orchestration automates deployment and management"
60+
)
61+
assert qr.rag_chunks[1].source == "kubernetes-docs/concepts.md"
62+
assert qr.rag_chunks[1].score == 0.87
63+
64+
def test_rag_chunks_with_optional_fields(self) -> None:
65+
"""Test RAG chunks with optional source and score fields."""
66+
rag_chunks = [
67+
RAGChunk(content="Some content without source or score"),
68+
RAGChunk(content="Content with source only", source="docs/guide.md"),
69+
RAGChunk(content="Content with score only", score=0.75),
70+
]
71+
72+
qr = QueryResponse(response="LLM answer", rag_chunks=rag_chunks)
73+
74+
assert len(qr.rag_chunks) == 3
75+
assert qr.rag_chunks[0].source is None
76+
assert qr.rag_chunks[0].score is None
77+
assert qr.rag_chunks[1].source == "docs/guide.md"
78+
assert qr.rag_chunks[1].score is None
79+
assert qr.rag_chunks[2].source is None
80+
assert qr.rag_chunks[2].score == 0.75
81+
82+
def test_complete_query_response_with_all_fields(self) -> None:
83+
"""Test QueryResponse with all fields including RAG chunks, tool calls, and docs."""
84+
rag_chunks = [
85+
RAGChunk(
86+
content="OLM is a component of the Operator Framework toolkit",
87+
source="kubernetes-docs/operators.md",
88+
score=0.95,
89+
)
90+
]
91+
92+
tool_calls = [
93+
ToolCall(
94+
tool_name="knowledge_search",
95+
arguments={"query": "operator lifecycle manager"},
96+
result={"chunks_found": 5},
97+
)
98+
]
99+
100+
referenced_documents = [
101+
ReferencedDocument(
102+
doc_url=(
103+
"https://docs.openshift.com/container-platform/4.15/operators/olm/index.html"
104+
),
105+
doc_title="Operator Lifecycle Manager (OLM)",
106+
)
107+
]
108+
109+
qr = QueryResponse(
110+
conversation_id="123e4567-e89b-12d3-a456-426614174000",
111+
response="Operator Lifecycle Manager (OLM) helps users install...",
112+
rag_chunks=rag_chunks,
113+
tool_calls=tool_calls,
114+
referenced_documents=referenced_documents,
115+
)
116+
117+
assert qr.conversation_id == "123e4567-e89b-12d3-a456-426614174000"
118+
assert qr.response == "Operator Lifecycle Manager (OLM) helps users install..."
119+
assert len(qr.rag_chunks) == 1
120+
assert (
121+
qr.rag_chunks[0].content
122+
== "OLM is a component of the Operator Framework toolkit"
123+
)
124+
assert len(qr.tool_calls) == 1
125+
assert qr.tool_calls[0].tool_name == "knowledge_search"
126+
assert len(qr.referenced_documents) == 1
127+
assert (
128+
qr.referenced_documents[0].doc_title == "Operator Lifecycle Manager (OLM)"
129+
)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Unit tests for RAGChunk model."""
2+
3+
from models.responses import RAGChunk
4+
5+
6+
class TestRAGChunk:
7+
"""Test cases for the RAGChunk model."""
8+
9+
def test_constructor_with_content_only(self) -> None:
10+
"""Test RAGChunk constructor with content only."""
11+
chunk = RAGChunk(content="Sample content")
12+
assert chunk.content == "Sample content"
13+
assert chunk.source is None
14+
assert chunk.score is None
15+
16+
def test_constructor_with_all_fields(self) -> None:
17+
"""Test RAGChunk constructor with all fields."""
18+
chunk = RAGChunk(
19+
content="Kubernetes is an open-source container orchestration system",
20+
source="kubernetes-docs/overview.md",
21+
score=0.95,
22+
)
23+
assert (
24+
chunk.content
25+
== "Kubernetes is an open-source container orchestration system"
26+
)
27+
assert chunk.source == "kubernetes-docs/overview.md"
28+
assert chunk.score == 0.95
29+
30+
def test_constructor_with_content_and_source(self) -> None:
31+
"""Test RAGChunk constructor with content and source."""
32+
chunk = RAGChunk(
33+
content="Container orchestration automates deployment",
34+
source="docs/concepts.md",
35+
)
36+
assert chunk.content == "Container orchestration automates deployment"
37+
assert chunk.source == "docs/concepts.md"
38+
assert chunk.score is None
39+
40+
def test_constructor_with_content_and_score(self) -> None:
41+
"""Test RAGChunk constructor with content and score."""
42+
chunk = RAGChunk(content="Pod is the smallest deployable unit", score=0.82)
43+
assert chunk.content == "Pod is the smallest deployable unit"
44+
assert chunk.source is None
45+
assert chunk.score == 0.82
46+
47+
def test_score_range_validation(self) -> None:
48+
"""Test that RAGChunk accepts valid score ranges."""
49+
# Test minimum score
50+
chunk_min = RAGChunk(content="Test content", score=0.0)
51+
assert chunk_min.score == 0.0
52+
53+
# Test maximum score
54+
chunk_max = RAGChunk(content="Test content", score=1.0)
55+
assert chunk_max.score == 1.0
56+
57+
# Test decimal score
58+
chunk_decimal = RAGChunk(content="Test content", score=0.751)
59+
assert chunk_decimal.score == 0.751
60+
61+
def test_empty_content(self) -> None:
62+
"""Test RAGChunk with empty content."""
63+
chunk = RAGChunk(content="")
64+
assert chunk.content == ""
65+
assert chunk.source is None
66+
assert chunk.score is None
67+
68+
def test_multiline_content(self) -> None:
69+
"""Test RAGChunk with multiline content."""
70+
multiline_content = """This is a multiline content
71+
that spans multiple lines
72+
and contains various information."""
73+
74+
chunk = RAGChunk(
75+
content=multiline_content, source="docs/multiline.md", score=0.88
76+
)
77+
assert chunk.content == multiline_content
78+
assert chunk.source == "docs/multiline.md"
79+
assert chunk.score == 0.88
80+
81+
def test_long_source_path(self) -> None:
82+
"""Test RAGChunk with long source path."""
83+
long_source = (
84+
"very/deep/nested/directory/structure/with/many/levels/document.md"
85+
)
86+
chunk = RAGChunk(
87+
content="Content from deeply nested document", source=long_source
88+
)
89+
assert chunk.source == long_source
90+
91+
def test_url_as_source(self) -> None:
92+
"""Test RAGChunk with URL as source."""
93+
url_source = "https://docs.example.com/api/v1/documentation"
94+
chunk = RAGChunk(
95+
content="API documentation content", source=url_source, score=0.92
96+
)
97+
assert chunk.source == url_source
98+
assert chunk.score == 0.92

0 commit comments

Comments
 (0)