@@ -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
2827class 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 ()
0 commit comments