Skip to content

Commit 571f2bb

Browse files
2 parents 707f71c + b37e533 commit 571f2bb

File tree

5 files changed

+39
-44
lines changed

5 files changed

+39
-44
lines changed

src/api/agents/agent_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AgentFactory:
2323
_lock = asyncio.Lock()
2424

2525
@classmethod
26-
async def get_instance(cls, config):
26+
async def get_instance(cls):
2727
"""
2828
Get or create the singleton AzureAIAgent instance.
2929
"""

src/api/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from dotenv import load_dotenv
1515
import uvicorn
1616

17-
from common.config.config import Config
1817
from agents.agent_factory import AgentFactory
1918
from api.api_routes import router as backend_router
2019
from api.history_routes import router as history_router
@@ -30,8 +29,7 @@ async def lifespan(fastapi_app: FastAPI):
3029
On startup, initializes the Azure AI agent using the configuration and attaches it to the app state.
3130
On shutdown, deletes the agent instance and performs any necessary cleanup.
3231
"""
33-
config = Config()
34-
fastapi_app.state.agent = await AgentFactory.get_instance(config=config)
32+
fastapi_app.state.agent = await AgentFactory.get_instance()
3533
yield
3634
await AgentFactory.delete_instance()
3735
fastapi_app.state.agent = None

src/tests/api/agents/test_agent_factory.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@ def reset_agent_factory():
1212
AgentFactory._instance = None
1313

1414

15-
@pytest.fixture
16-
def config_mock():
17-
return MagicMock(
18-
azure_ai_project_conn_string="fake_conn_str",
19-
azure_openai_deployment_model="gpt-4"
20-
)
21-
22-
2315
@pytest.mark.asyncio
24-
@patch("agents.agent_factory.ChatWithDataPlugin", autospec=True)
16+
@patch("agents.agent_factory.AzureAIAgentSettings", autospec=True)
2517
@patch("agents.agent_factory.AzureAIAgent", autospec=True)
2618
@patch("agents.agent_factory.DefaultAzureCredential", autospec=True)
2719
async def test_get_instance_creates_new_agent(
2820
mock_credential,
2921
mock_azure_agent,
30-
mock_plugin,
31-
config_mock
22+
mock_azure_ai_agent_settings
3223
):
24+
mock_settings_instance = MagicMock()
25+
mock_settings_instance.endpoint = "https://fake-endpoint"
26+
mock_settings_instance.model_deployment_name = "gpt-4o-mini"
27+
mock_azure_ai_agent_settings.return_value = mock_settings_instance
28+
3329
mock_client = AsyncMock()
3430
mock_agent_definition = MagicMock()
3531
mock_client.agents.create_agent.return_value = mock_agent_definition
@@ -38,21 +34,21 @@ async def test_get_instance_creates_new_agent(
3834
agent_instance = MagicMock()
3935
mock_azure_agent.return_value = agent_instance
4036

41-
result = await AgentFactory.get_instance(config_mock)
37+
result = await AgentFactory.get_instance()
4238

4339
assert result == agent_instance
4440
mock_azure_agent.create_client.assert_called_once_with(
4541
credential=mock_credential.return_value,
46-
conn_str="fake_conn_str"
42+
endpoint="https://fake-endpoint"
4743
)
4844
mock_client.agents.create_agent.assert_awaited_once()
4945
mock_azure_agent.assert_called_once()
5046

5147

5248
@pytest.mark.asyncio
53-
async def test_get_instance_returns_existing_instance(config_mock):
49+
async def test_get_instance_returns_existing_instance():
5450
AgentFactory._instance = MagicMock()
55-
result = await AgentFactory.get_instance(config_mock)
51+
result = await AgentFactory.get_instance()
5652
assert result == AgentFactory._instance
5753

5854

src/tests/api/plugins/test_chat_with_data_plugin.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def mock_config():
88
config_mock = MagicMock()
99
config_mock.azure_openai_deployment_model = "gpt-4"
1010
config_mock.azure_openai_endpoint = "https://test-openai.azure.com/"
11-
config_mock.azure_openai_api_key = "test-api-key"
1211
config_mock.azure_openai_api_version = "2024-02-15-preview"
1312
config_mock.azure_ai_search_endpoint = "https://search.test.azure.com/"
1413
config_mock.azure_ai_search_api_key = "search-api-key"
@@ -26,25 +25,29 @@ def chat_plugin(mock_config):
2625

2726

2827
class TestChatWithDataPlugin:
29-
@pytest.mark.asyncio
28+
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
3029
@patch("plugins.chat_with_data_plugin.openai.AzureOpenAI")
31-
async def test_greeting(self, mock_azure_openai, chat_plugin):
32-
# Setup mock
30+
@pytest.mark.asyncio
31+
async def test_greeting(self, mock_azure_openai, mock_token_provider, chat_plugin):
32+
# Setup mock token provider
33+
mock_token_provider.return_value = lambda: "fake_token"
34+
35+
# Setup mock client and completion response
3336
mock_client = MagicMock()
34-
mock_azure_openai.return_value = mock_client
3537
mock_completion = MagicMock()
3638
mock_completion.choices = [MagicMock()]
3739
mock_completion.choices[0].message.content = "Hello, how can I help you?"
3840
mock_client.chat.completions.create.return_value = mock_completion
39-
41+
mock_azure_openai.return_value = mock_client
42+
4043
# Call the method
4144
result = await chat_plugin.greeting("Hello")
42-
45+
4346
# Assertions
4447
assert result == "Hello, how can I help you?"
4548
mock_azure_openai.assert_called_once_with(
4649
azure_endpoint="https://test-openai.azure.com/",
47-
api_key="test-api-key",
50+
azure_ad_token_provider=mock_token_provider.return_value,
4851
api_version="2024-02-15-preview"
4952
)
5053
mock_client.chat.completions.create.assert_called_once()
@@ -103,10 +106,13 @@ async def test_greeting_exception(self, mock_azure_openai, chat_plugin):
103106
assert result == "Details could not be retrieved. Please try again later."
104107

105108
@pytest.mark.asyncio
109+
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
106110
@patch("plugins.chat_with_data_plugin.execute_sql_query")
107111
@patch("plugins.chat_with_data_plugin.openai.AzureOpenAI")
108-
async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, chat_plugin):
112+
async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, mock_token_provider, chat_plugin):
113+
109114
# Setup mocks
115+
mock_token_provider.return_value = lambda: "fake_token"
110116
mock_client = MagicMock()
111117
mock_azure_openai.return_value = mock_client
112118
mock_completion = MagicMock()
@@ -123,7 +129,7 @@ async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, chat_
123129
assert result == "Query results data"
124130
mock_azure_openai.assert_called_once_with(
125131
azure_endpoint="https://test-openai.azure.com/",
126-
api_key="test-api-key",
132+
azure_ad_token_provider=mock_token_provider.return_value,
127133
api_version="2024-02-15-preview"
128134
)
129135
mock_client.chat.completions.create.assert_called_once()
@@ -175,9 +181,11 @@ async def test_get_SQL_Response_exception(self, mock_azure_openai, mock_execute_
175181
mock_execute_sql.assert_not_called()
176182

177183
@pytest.mark.asyncio
184+
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
178185
@patch("plugins.chat_with_data_plugin.openai.AzureOpenAI")
179-
async def test_get_answers_from_calltranscripts(self, mock_azure_openai, chat_plugin):
186+
async def test_get_answers_from_calltranscripts(self, mock_azure_openai, mock_token_provider, chat_plugin):
180187
# Setup mock
188+
mock_token_provider.return_value = lambda: "fake_token"
181189
mock_client = MagicMock()
182190
mock_azure_openai.return_value = mock_client
183191
mock_completion = MagicMock()
@@ -193,7 +201,7 @@ async def test_get_answers_from_calltranscripts(self, mock_azure_openai, chat_pl
193201
assert result.message.content == "Answer about transcripts"
194202
mock_azure_openai.assert_called_once_with(
195203
azure_endpoint="https://test-openai.azure.com/",
196-
api_key="test-api-key",
204+
azure_ad_token_provider=mock_token_provider.return_value,
197205
api_version="2024-02-15-preview"
198206
)
199207
mock_client.chat.completions.create.assert_called_once()

src/tests/api/services/test_chat_service.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import pytest
2-
from unittest.mock import AsyncMock, MagicMock, patch, call
31
import json
4-
import uuid
52
import time
6-
import asyncio
7-
import types
8-
from types import SimpleNamespace
3+
from unittest.mock import AsyncMock, MagicMock, patch
4+
5+
import pytest
96
from fastapi import HTTPException, status
107
from semantic_kernel.exceptions.agent_exceptions import AgentException as RealAgentException
118

129

10+
1311
# ---- Patch imports before importing the service under test ----
1412
@patch("common.config.config.Config")
1513
@patch("semantic_kernel.agents.AzureAIAgentThread")
16-
@patch("azure.ai.projects.models.TruncationObject")
14+
@patch("azure.ai.agents.models.TruncationObject")
1715
@patch("semantic_kernel.exceptions.agent_exceptions.AgentException")
1816
@patch("openai.AzureOpenAI")
1917
@patch("helpers.utils.format_stream_response")
@@ -23,7 +21,6 @@ def patched_imports(mock_format_stream, mock_openai, mock_agent_exception, mock_
2321
# Configure mock Config
2422
mock_config_instance = MagicMock()
2523
mock_config_instance.azure_openai_endpoint = "https://test.openai.azure.com"
26-
mock_config_instance.azure_openai_api_key = "test_key"
2724
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
2825
mock_config_instance.azure_openai_deployment_model = "gpt-4o-mini"
2926
mock_config_instance.azure_ai_project_conn_string = "test_conn_string"
@@ -50,15 +47,14 @@ def patched_imports(mock_format_stream, mock_openai, mock_agent_exception, mock_
5047
# ---- Import service under test with patches ----
5148
with patch("common.config.config.Config") as mock_config, \
5249
patch("semantic_kernel.agents.AzureAIAgentThread") as mock_thread, \
53-
patch("azure.ai.projects.models.TruncationObject") as mock_truncation, \
50+
patch("azure.ai.agents.models.TruncationObject") as mock_truncation, \
5451
patch("semantic_kernel.exceptions.agent_exceptions.AgentException", new=RealAgentException) as mock_agent_exception, \
5552
patch("openai.AzureOpenAI") as mock_openai, \
5653
patch("helpers.utils.format_stream_response") as mock_format_stream:
5754

5855
# Configure mock Config
5956
mock_config_instance = MagicMock()
6057
mock_config_instance.azure_openai_endpoint = "https://test.openai.azure.com"
61-
mock_config_instance.azure_openai_api_key = "test_key"
6258
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
6359
mock_config_instance.azure_openai_deployment_model = "gpt-4o-mini"
6460
mock_config_instance.azure_ai_project_conn_string = "test_conn_string"
@@ -161,7 +157,6 @@ def test_init(self, mock_config_class, mock_request):
161157
# Configure mock Config
162158
mock_config_instance = MagicMock()
163159
mock_config_instance.azure_openai_endpoint = "https://test.openai.azure.com"
164-
mock_config_instance.azure_openai_api_key = "test_key"
165160
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
166161
mock_config_instance.azure_openai_deployment_model = "gpt-4o-mini"
167162
mock_config_instance.azure_ai_project_conn_string = "test_conn_string"
@@ -173,10 +168,8 @@ def test_init(self, mock_config_class, mock_request):
173168
service = ChatService(mock_request)
174169

175170
assert service.azure_openai_endpoint == "https://test.openai.azure.com"
176-
assert service.azure_openai_api_key == "test_key"
177171
assert service.azure_openai_api_version == "2024-02-15-preview"
178172
assert service.azure_openai_deployment_name == "gpt-4o-mini"
179-
assert service.azure_ai_project_conn_string == "test_conn_string"
180173
assert service.agent == mock_request.app.state.agent
181174
assert ChatService.thread_cache is not None
182175

0 commit comments

Comments
 (0)