Skip to content

Commit f03ec14

Browse files
Merge pull request #483 from microsoft/psl-pk-km-fdp
fix: fix unittestcases
2 parents d8b0442 + 1ebd678 commit f03ec14

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

src/tests/api/plugins/test_chat_with_data_plugin.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ def chat_plugin(mock_config):
2525

2626

2727
class TestChatWithDataPlugin:
28-
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
28+
@patch("helpers.azure_openai_helper.Config")
29+
@patch("helpers.azure_openai_helper.get_bearer_token_provider")
2930
@patch("helpers.azure_openai_helper.openai.AzureOpenAI")
3031
@pytest.mark.asyncio
31-
async def test_greeting(self, mock_azure_openai, mock_token_provider, chat_plugin):
32+
async def test_greeting(self, mock_azure_openai, mock_token_provider, mock_config, chat_plugin):
3233
# Setup mock token provider
3334
mock_token_provider.return_value = lambda: "fake_token"
3435

3536
# Setup mock client and completion response
37+
mock_config_instance = MagicMock()
38+
mock_config_instance.azure_openai_endpoint = "https://test-openai.azure.com/"
39+
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
40+
mock_config.return_value = mock_config_instance
3641
mock_client = MagicMock()
3742
mock_completion = MagicMock()
3843
mock_completion.choices = [MagicMock()]
@@ -60,30 +65,39 @@ async def test_greeting(self, mock_azure_openai, mock_token_provider, chat_plugi
6065
assert args["messages"][1]["content"] == "Hello"
6166

6267
@pytest.mark.asyncio
68+
@patch("plugins.chat_with_data_plugin.Config")
6369
@patch("plugins.chat_with_data_plugin.AIProjectClient")
6470
@patch("plugins.chat_with_data_plugin.DefaultAzureCredential")
65-
async def test_greeting_with_ai_project_client(self, mock_azure_credential, mock_ai_project_client, chat_plugin):
71+
async def test_greeting_with_ai_project_client(self, mock_azure_credential, mock_ai_project_client, mock_config, chat_plugin):
6672
# Setup AIProjectClient
6773
chat_plugin.use_ai_project_client = True
6874

6975
# Setup mock
70-
mock_project = MagicMock()
71-
mock_ai_project_client.from_connection_string.return_value = mock_project
72-
mock_client = MagicMock()
73-
mock_project.inference.get_chat_completions_client.return_value = mock_client
76+
mock_config_instance = MagicMock()
77+
mock_config_instance.ai_project_endpoint = "https://test-openai.azure.com/"
78+
mock_config.return_value = mock_config_instance
79+
mock_credential_instance = MagicMock()
80+
mock_azure_credential.return_value = mock_credential_instance
81+
mock_project_instance = MagicMock()
82+
mock_ai_project_client.return_value = mock_project_instance
83+
mock_chat_client = MagicMock()
84+
mock_project_instance.inference.get_chat_completions_client.return_value = mock_chat_client
7485
mock_completion = MagicMock()
7586
mock_completion.choices = [MagicMock()]
7687
mock_completion.choices[0].message.content = "Hello from AI Project Client"
77-
mock_client.complete.return_value = mock_completion
88+
mock_chat_client.complete.return_value = mock_completion
7889

7990
# Call the method
8091
result = await chat_plugin.greeting("Hello")
8192

8293
# Assertions
8394
assert result == "Hello from AI Project Client"
84-
mock_ai_project_client.from_connection_string.assert_called_once()
85-
mock_client.complete.assert_called_once()
86-
args = mock_client.complete.call_args[1]
95+
mock_ai_project_client.assert_called_once_with(
96+
endpoint=chat_plugin.ai_project_endpoint,
97+
credential=mock_credential_instance
98+
)
99+
mock_chat_client.complete.assert_called_once()
100+
args = mock_chat_client.complete.call_args[1]
87101
assert args["model"] == "gpt-4"
88102
assert args["temperature"] == 0
89103
assert len(args["messages"]) == 2
@@ -106,12 +120,17 @@ async def test_greeting_exception(self, mock_azure_openai, chat_plugin):
106120
assert result == "Details could not be retrieved. Please try again later."
107121

108122
@pytest.mark.asyncio
109-
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
123+
@patch("helpers.azure_openai_helper.Config")
124+
@patch("helpers.azure_openai_helper.get_bearer_token_provider")
110125
@patch("plugins.chat_with_data_plugin.execute_sql_query")
111126
@patch("helpers.azure_openai_helper.openai.AzureOpenAI")
112-
async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, mock_token_provider, chat_plugin):
127+
async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, mock_token_provider, mock_config, chat_plugin):
113128

114129
# Setup mocks
130+
mock_config_instance = MagicMock()
131+
mock_config_instance.azure_openai_endpoint = "https://test-openai.azure.com/"
132+
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
133+
mock_config.return_value = mock_config_instance
115134
mock_token_provider.return_value = lambda: "fake_token"
116135
mock_client = MagicMock()
117136
mock_azure_openai.return_value = mock_client
@@ -136,18 +155,24 @@ async def test_get_SQL_Response(self, mock_azure_openai, mock_execute_sql, mock_
136155
mock_execute_sql.assert_called_once_with("SELECT * FROM km_processed_data")
137156

138157
@pytest.mark.asyncio
158+
@patch("plugins.chat_with_data_plugin.Config")
139159
@patch("plugins.chat_with_data_plugin.execute_sql_query")
140160
@patch("plugins.chat_with_data_plugin.AIProjectClient")
141161
@patch("plugins.chat_with_data_plugin.DefaultAzureCredential")
142-
async def test_get_SQL_Response_with_ai_project_client(self, mock_azure_credential, mock_ai_project_client, mock_execute_sql, chat_plugin):
162+
async def test_get_SQL_Response_with_ai_project_client(self, mock_azure_credential, mock_ai_project_client, mock_execute_sql, mock_config, chat_plugin):
143163
# Setup AIProjectClient
144164
chat_plugin.use_ai_project_client = True
145165

146166
# Setup mocks
147-
mock_project = MagicMock()
148-
mock_ai_project_client.from_connection_string.return_value = mock_project
167+
mock_config_instance = MagicMock()
168+
mock_config_instance.ai_project_endpoint = "https://test-openai.azure.com/"
169+
mock_config.return_value = mock_config_instance
170+
mock_credential_instance = MagicMock()
171+
mock_azure_credential.return_value = mock_credential_instance
172+
mock_project_instance = MagicMock()
173+
mock_ai_project_client.return_value = mock_project_instance
149174
mock_client = MagicMock()
150-
mock_project.inference.get_chat_completions_client.return_value = mock_client
175+
mock_project_instance.inference.get_chat_completions_client.return_value = mock_client
151176
mock_completion = MagicMock()
152177
mock_completion.choices = [MagicMock()]
153178
mock_completion.choices[0].message.content = "```sql\nSELECT * FROM km_processed_data\n```"
@@ -160,8 +185,10 @@ async def test_get_SQL_Response_with_ai_project_client(self, mock_azure_credenti
160185

161186
# Assertions
162187
assert result == "Query results data with AI Project Client"
163-
mock_ai_project_client.from_connection_string.assert_called_once()
164-
mock_client.complete.assert_called_once()
188+
mock_ai_project_client.assert_called_once_with(
189+
endpoint=chat_plugin.ai_project_endpoint,
190+
credential=mock_credential_instance
191+
)
165192
mock_execute_sql.assert_called_once_with("\nSELECT * FROM km_processed_data\n")
166193

167194
@pytest.mark.asyncio
@@ -181,13 +208,18 @@ async def test_get_SQL_Response_exception(self, mock_azure_openai, mock_execute_
181208
mock_execute_sql.assert_not_called()
182209

183210
@pytest.mark.asyncio
184-
@patch("plugins.chat_with_data_plugin.get_bearer_token_provider")
211+
@patch("helpers.azure_openai_helper.Config")
212+
@patch("helpers.azure_openai_helper.get_bearer_token_provider")
185213
@patch("helpers.azure_openai_helper.openai.AzureOpenAI")
186-
async def test_get_answers_from_calltranscripts(self, mock_azure_openai, mock_token_provider, chat_plugin):
214+
async def test_get_answers_from_calltranscripts(self, mock_azure_openai, mock_token_provider, mock_config, chat_plugin):
187215
# Setup mock
188216
mock_token_provider.return_value = lambda: "fake_token"
189217
mock_client = MagicMock()
190218
mock_azure_openai.return_value = mock_client
219+
mock_config_instance = MagicMock()
220+
mock_config_instance.azure_openai_endpoint = "https://test-openai.azure.com/"
221+
mock_config_instance.azure_openai_api_version = "2024-02-15-preview"
222+
mock_config.return_value = mock_config_instance
191223
mock_completion = MagicMock()
192224
mock_completion.choices = [MagicMock()]
193225
mock_completion.choices[0].message = MagicMock()

src/tests/api/services/test_chat_service.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
# ---- Patch imports before importing the service under test ----
12-
@patch("common.config.config.Config")
12+
@patch("helpers.azure_openai_helper.Config")
1313
@patch("semantic_kernel.agents.AzureAIAgentThread")
1414
@patch("azure.ai.agents.models.TruncationObject")
1515
@patch("semantic_kernel.exceptions.agent_exceptions.AgentException")
@@ -167,8 +167,6 @@ def test_init(self, mock_config_class, mock_request):
167167

168168
service = ChatService(mock_request)
169169

170-
assert service.azure_openai_endpoint == "https://test.openai.azure.com"
171-
assert service.azure_openai_api_version == "2024-02-15-preview"
172170
assert service.azure_openai_deployment_name == "gpt-4o-mini"
173171
assert service.agent == mock_request.app.state.agent
174172
assert ChatService.thread_cache is not None

src/tests/api/services/test_history_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def history_service(mock_config_instance):
2828
# Create patches for other dependencies used by HistoryService
2929
with patch("services.history_service.CosmosConversationClient"):
3030
with patch("services.history_service.AsyncAzureOpenAI"):
31-
with patch("services.history_service.get_bearer_token_provider"):
31+
with patch("helpers.azure_openai_helper.get_bearer_token_provider"):
3232
with patch("services.history_service.complete_chat_request"):
3333
service = HistoryService()
3434
return service
@@ -105,7 +105,7 @@ def test_init_openai_client_no_deployment_name(self, history_service):
105105

106106
def test_init_openai_client_no_api_key(self, history_service):
107107
"""Test OpenAI client initialization with no API key"""
108-
with patch("services.history_service.get_bearer_token_provider", return_value="token_provider"):
108+
with patch("helpers.azure_openai_helper.get_bearer_token_provider", return_value="token_provider"):
109109
with patch("services.history_service.AsyncAzureOpenAI", return_value="openai_client"):
110110
client = history_service.init_openai_client()
111111
assert client == "openai_client"

0 commit comments

Comments
 (0)