Skip to content

Commit b0a20d8

Browse files
TaylorTaylor
authored andcommitted
Working on tests
1 parent f60f79f commit b0a20d8

File tree

8 files changed

+257
-392
lines changed

8 files changed

+257
-392
lines changed

tests/conftest.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import msal
1111
import pytest
1212
import pytest_asyncio
13+
from azure.core.credentials import AzureKeyCredential
1314
from azure.search.documents.agent.aio import KnowledgeAgentRetrievalClient
1415
from azure.search.documents.aio import SearchClient
1516
from azure.search.documents.indexes.aio import SearchIndexClient
@@ -1102,7 +1103,7 @@ def mock_user_directory_client(monkeypatch):
11021103
@pytest.fixture
11031104
def chat_approach():
11041105
return ChatReadRetrieveReadApproach(
1105-
search_client=None,
1106+
search_client=SearchClient(endpoint="", index_name="", credential=AzureKeyCredential("")),
11061107
search_index_name=None,
11071108
agent_model=None,
11081109
agent_deployment=None,
@@ -1131,3 +1132,38 @@ def chat_approach():
11311132
credential=MockAzureCredential(),
11321133
),
11331134
)
1135+
1136+
1137+
@pytest.fixture
1138+
def chat_approach_with_hydration():
1139+
return ChatReadRetrieveReadApproach(
1140+
search_client=SearchClient(endpoint="", index_name="", credential=AzureKeyCredential("")),
1141+
search_index_name=None,
1142+
agent_model=None,
1143+
agent_deployment=None,
1144+
agent_client=None,
1145+
auth_helper=None,
1146+
openai_client=None,
1147+
chatgpt_model="gpt-4.1-mini",
1148+
chatgpt_deployment="chat",
1149+
embedding_deployment="embeddings",
1150+
embedding_model=MOCK_EMBEDDING_MODEL_NAME,
1151+
embedding_dimensions=MOCK_EMBEDDING_DIMENSIONS,
1152+
embedding_field="embedding3",
1153+
sourcepage_field="",
1154+
content_field="",
1155+
query_language="en-us",
1156+
query_speller="lexicon",
1157+
prompt_manager=PromptyManager(),
1158+
hydrate_references=True,
1159+
user_blob_manager=AdlsBlobManager(
1160+
endpoint="https://test-userstorage-account.dfs.core.windows.net",
1161+
container="test-userstorage-container",
1162+
credential=MockAzureCredential(),
1163+
),
1164+
global_blob_manager=BlobManager( # on normal Azure storage
1165+
endpoint="https://test-globalstorage-account.blob.core.windows.net",
1166+
container="test-globalstorage-container",
1167+
credential=MockAzureCredential(),
1168+
),
1169+
)

tests/mocks.py

Lines changed: 180 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ def __init__(self, search_text, vector_queries: Optional[list[VectorQuery]]):
269269
]
270270
]
271271
elif search_text == "hydrated":
272-
# Mock search results for hydration testing with complete data
273272
self.data = [
274273
[
275274
{
@@ -287,7 +286,6 @@ def __init__(self, search_text, vector_queries: Optional[list[VectorQuery]]):
287286
]
288287
]
289288
elif search_text == "hydrated_multi":
290-
# Mock search results for multiple document hydration
291289
self.data = [
292290
[
293291
{
@@ -315,7 +313,6 @@ def __init__(self, search_text, vector_queries: Optional[list[VectorQuery]]):
315313
]
316314
]
317315
elif search_text == "hydrated_single":
318-
# Mock search results for single document hydration
319316
self.data = [
320317
[
321318
{
@@ -458,6 +455,186 @@ def mock_retrieval_response():
458455
)
459456

460457

458+
def mock_retrieval_response_with_sorting():
459+
"""Mock response with multiple references for testing sorting"""
460+
return KnowledgeAgentRetrievalResponse(
461+
response=[
462+
KnowledgeAgentMessage(
463+
role="assistant",
464+
content=[KnowledgeAgentMessageTextContent(text="Test response")],
465+
)
466+
],
467+
activity=[
468+
KnowledgeAgentSearchActivityRecord(
469+
id=1,
470+
target_index="index",
471+
query=KnowledgeAgentSearchActivityRecordQuery(search="first query"),
472+
count=10,
473+
elapsed_ms=50,
474+
),
475+
KnowledgeAgentSearchActivityRecord(
476+
id=2,
477+
target_index="index",
478+
query=KnowledgeAgentSearchActivityRecordQuery(search="second query"),
479+
count=10,
480+
elapsed_ms=50,
481+
),
482+
],
483+
references=[
484+
KnowledgeAgentAzureSearchDocReference(
485+
id="2", # Higher ID for testing interleaved sorting
486+
activity_source=2,
487+
doc_key="doc2",
488+
source_data={"content": "Content 2", "sourcepage": "page2.pdf"},
489+
),
490+
KnowledgeAgentAzureSearchDocReference(
491+
id="1", # Lower ID for testing interleaved sorting
492+
activity_source=1,
493+
doc_key="doc1",
494+
source_data={"content": "Content 1", "sourcepage": "page1.pdf"},
495+
),
496+
],
497+
)
498+
499+
500+
def mock_retrieval_response_with_duplicates():
501+
"""Mock response with duplicate doc_keys for testing deduplication"""
502+
return KnowledgeAgentRetrievalResponse(
503+
response=[
504+
KnowledgeAgentMessage(
505+
role="assistant",
506+
content=[KnowledgeAgentMessageTextContent(text="Test response")],
507+
)
508+
],
509+
activity=[
510+
KnowledgeAgentSearchActivityRecord(
511+
id=1,
512+
target_index="index",
513+
query=KnowledgeAgentSearchActivityRecordQuery(search="query for doc1"),
514+
count=10,
515+
elapsed_ms=50,
516+
),
517+
KnowledgeAgentSearchActivityRecord(
518+
id=2,
519+
target_index="index",
520+
query=KnowledgeAgentSearchActivityRecordQuery(search="another query for doc1"),
521+
count=10,
522+
elapsed_ms=50,
523+
),
524+
],
525+
references=[
526+
KnowledgeAgentAzureSearchDocReference(
527+
id="1",
528+
activity_source=1,
529+
doc_key="doc1", # Same doc_key
530+
source_data={"content": "Content 1", "sourcepage": "page1.pdf"},
531+
),
532+
KnowledgeAgentAzureSearchDocReference(
533+
id="2",
534+
activity_source=2,
535+
doc_key="doc1", # Duplicate doc_key
536+
source_data={"content": "Content 1", "sourcepage": "page1.pdf"},
537+
),
538+
KnowledgeAgentAzureSearchDocReference(
539+
id="3",
540+
activity_source=1,
541+
doc_key="doc2", # Different doc_key
542+
source_data={"content": "Content 2", "sourcepage": "page2.pdf"},
543+
),
544+
],
545+
)
546+
547+
548+
async def mock_search_for_hydration(*args, **kwargs):
549+
"""Mock search that returns documents matching the filter"""
550+
filter_param = kwargs.get("filter", "")
551+
552+
# Create documents based on filter - use search_text to distinguish different calls
553+
search_text = ""
554+
if "doc1" in filter_param and "doc2" in filter_param:
555+
search_text = "hydrated_multi"
556+
elif "doc1" in filter_param:
557+
search_text = "hydrated_single"
558+
else:
559+
search_text = "hydrated_empty"
560+
561+
return MockAsyncSearchResultsIterator(search_text, None)
562+
563+
564+
def mock_retrieval_response_with_missing_doc_key():
565+
"""Mock response with missing doc_key to test continue condition"""
566+
return KnowledgeAgentRetrievalResponse(
567+
response=[
568+
KnowledgeAgentMessage(
569+
role="assistant",
570+
content=[KnowledgeAgentMessageTextContent(text="Test response")],
571+
)
572+
],
573+
activity=[
574+
KnowledgeAgentSearchActivityRecord(
575+
id=1,
576+
target_index="index",
577+
query=KnowledgeAgentSearchActivityRecordQuery(search="query"),
578+
count=10,
579+
elapsed_ms=50,
580+
),
581+
],
582+
references=[
583+
KnowledgeAgentAzureSearchDocReference(
584+
id="1",
585+
activity_source=1,
586+
doc_key=None, # Missing doc_key
587+
source_data={"content": "Content 1", "sourcepage": "page1.pdf"},
588+
),
589+
KnowledgeAgentAzureSearchDocReference(
590+
id="2",
591+
activity_source=1,
592+
doc_key="", # Empty doc_key
593+
source_data={"content": "Content 2", "sourcepage": "page2.pdf"},
594+
),
595+
KnowledgeAgentAzureSearchDocReference(
596+
id="3",
597+
activity_source=1,
598+
doc_key="doc3", # Valid doc_key
599+
source_data={"content": "Content 3", "sourcepage": "page3.pdf"},
600+
),
601+
],
602+
)
603+
604+
605+
def mock_retrieval_response_with_top_limit():
606+
"""Mock response with many references to test top limit during document building"""
607+
references = []
608+
for i in range(15): # More than any reasonable top limit
609+
references.append(
610+
KnowledgeAgentAzureSearchDocReference(
611+
id=str(i),
612+
activity_source=1,
613+
doc_key=f"doc{i}",
614+
source_data={"content": f"Content {i}", "sourcepage": f"page{i}.pdf"},
615+
)
616+
)
617+
618+
return KnowledgeAgentRetrievalResponse(
619+
response=[
620+
KnowledgeAgentMessage(
621+
role="assistant",
622+
content=[KnowledgeAgentMessageTextContent(text="Test response")],
623+
)
624+
],
625+
activity=[
626+
KnowledgeAgentSearchActivityRecord(
627+
id=1,
628+
target_index="index",
629+
query=KnowledgeAgentSearchActivityRecordQuery(search="query"),
630+
count=10,
631+
elapsed_ms=50,
632+
),
633+
],
634+
references=references,
635+
)
636+
637+
461638
class MockAudio:
462639
def __init__(self, audio_data):
463640
self.audio_data = audio_data

tests/snapshots/test_app/test_ask_rtr_text_agent/agent_client0/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"category": null,
3939
"content": "There is a whistleblower policy.",
4040
"groups": null,
41-
"id": "Benefit_Options-2.pdf",
41+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
4242
"images": null,
4343
"oids": null,
4444
"reranker_score": 3.4577205181121826,

tests/snapshots/test_app/test_ask_rtr_text_agent_filter/agent_auth_client0/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"category": null,
3939
"content": "There is a whistleblower policy.",
4040
"groups": null,
41-
"id": "Benefit_Options-2.pdf",
41+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
4242
"images": null,
4343
"oids": null,
4444
"reranker_score": 3.4577205181121826,

tests/snapshots/test_app/test_chat_text_agent/agent_client0/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"category": null,
4040
"content": "There is a whistleblower policy.",
4141
"groups": null,
42-
"id": "Benefit_Options-2.pdf",
42+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
4343
"images": null,
4444
"oids": null,
4545
"reranker_score": 3.4577205181121826,

tests/snapshots/test_app/test_chat_text_filter_agent/agent_auth_client0/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"category": null,
4040
"content": "There is a whistleblower policy.",
4141
"groups": null,
42-
"id": "Benefit_Options-2.pdf",
42+
"id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
4343
"images": null,
4444
"oids": null,
4545
"reranker_score": 3.4577205181121826,

0 commit comments

Comments
 (0)