@@ -25,14 +25,19 @@ def chat_plugin(mock_config):
2525
2626
2727class 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\n SELECT * 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 ("\n SELECT * 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 ()
0 commit comments