11import pytest
2- import asyncio
3- from unittest .mock import patch , MagicMock , AsyncMock
4-
2+ from unittest .mock import patch , MagicMock
53from agents .search_agent_factory import SearchAgentFactory
64
75
@@ -13,48 +11,49 @@ def reset_search_agent_factory():
1311
1412
1513@pytest .mark .asyncio
16- @patch ("agents.search_agent_factory.Config" , autospec = True )
1714@patch ("agents.search_agent_factory.DefaultAzureCredential" , autospec = True )
1815@patch ("agents.search_agent_factory.AIProjectClient" , autospec = True )
1916@patch ("agents.search_agent_factory.AzureAISearchTool" , autospec = True )
20- async def test_get_agent_creates_new_instance (
21- mock_search_tool ,
22- mock_project_client_class ,
23- mock_credential ,
24- mock_config_class
17+ async def test_create_agent_creates_new_instance (
18+ mock_search_tool_cls ,
19+ mock_project_client_cls ,
20+ mock_credential_cls
2521):
2622 # Mock config
2723 mock_config = MagicMock ()
2824 mock_config .ai_project_endpoint = "https://fake-endpoint"
2925 mock_config .azure_ai_search_connection_name = "fake-connection"
3026 mock_config .azure_ai_search_index = "fake-index"
3127 mock_config .azure_openai_deployment_model = "fake-model"
32- mock_config_class .return_value = mock_config
28+ mock_config .solution_name = "test-solution"
29+ mock_config .ai_project_api_version = "2025-05-01"
3330
3431 # Mock project client
3532 mock_project_client = MagicMock ()
36- mock_project_client_class .return_value = mock_project_client
33+ mock_project_client_cls .return_value = mock_project_client
3734
35+ # Mock index response
3836 mock_index = MagicMock ()
3937 mock_index .name = "index-name"
4038 mock_index .version = "1"
4139 mock_project_client .indexes .create_or_update .return_value = mock_index
4240
4341 # Mock search tool
44- mock_tool = MagicMock ()
45- mock_tool .definitions = ["tool-def" ]
46- mock_tool .resources = ["tool-res" ]
47- mock_search_tool .return_value = mock_tool
42+ mock_search_tool_instance = MagicMock ()
43+ mock_search_tool_instance .definitions = ["tool-def" ]
44+ mock_search_tool_instance .resources = ["tool-res" ]
45+ mock_search_tool_cls .return_value = mock_search_tool_instance
4846
4947 # Mock agent
5048 mock_agent = MagicMock ()
5149 mock_project_client .agents .create_agent .return_value = mock_agent
5250
53- # Run the factory
54- result = await SearchAgentFactory .get_agent ( )
51+ # Run the factory directly
52+ result = await SearchAgentFactory .create_agent ( mock_config )
5553
5654 assert result ["agent" ] == mock_agent
5755 assert result ["client" ] == mock_project_client
56+
5857 mock_project_client .indexes .create_or_update .assert_called_once_with (
5958 name = "project-index-fake-connection-fake-index" ,
6059 version = "1" ,
@@ -74,23 +73,20 @@ async def test_get_agent_creates_new_instance(
7473
7574@pytest .mark .asyncio
7675async def test_get_agent_returns_existing_instance ():
76+ # Setup: Already initialized
7777 SearchAgentFactory ._agent = {"agent" : MagicMock (), "client" : MagicMock ()}
7878 result = await SearchAgentFactory .get_agent ()
7979 assert result == SearchAgentFactory ._agent
8080
8181
8282@pytest .mark .asyncio
8383async def test_delete_agent_removes_agent ():
84+ # Setup
8485 mock_agent = MagicMock ()
8586 mock_agent .id = "mock-agent-id"
86-
8787 mock_client = MagicMock ()
88- mock_client .agents .delete_agent = MagicMock ()
8988
90- SearchAgentFactory ._agent = {
91- "agent" : mock_agent ,
92- "client" : mock_client
93- }
89+ SearchAgentFactory ._agent = {"agent" : mock_agent , "client" : mock_client }
9490
9591 await SearchAgentFactory .delete_agent ()
9692
@@ -99,6 +95,8 @@ async def test_delete_agent_removes_agent():
9995
10096
10197@pytest .mark .asyncio
102- def test_delete_agent_does_nothing_if_none ():
98+ async def test_delete_agent_does_nothing_if_none ():
10399 SearchAgentFactory ._agent = None
104- SearchAgentFactory .delete_agent ()
100+ await SearchAgentFactory .delete_agent ()
101+ # No error should be raised, and nothing is called
102+
0 commit comments