44
55from unittest .mock import AsyncMock , MagicMock , Mock , patch , ANY
66
7- from httpx import Response
87from openai import RateLimitError , BadRequestError , InternalServerError
98import pytest
109from flask .testing import FlaskClient
@@ -335,17 +334,19 @@ def test_conversation_custom_returns_error_response_on_rate_limit_error(
335334 response_mock = Mock ()
336335 response_mock .status_code = 429
337336 response_mock .json .return_value = {
338- ' error' : {
339- ' code' : "429" ,
340- ' message' : ' Requests to the Embeddings_Create Operation under Azure OpenAI API version 2024-02-01 '
341- ' have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after '
342- ' 2 seconds. Please go here: https://aka.ms/oai/quotaincrease if you would like to further '
343- ' increase the default rate limit.'
337+ " error" : {
338+ " code" : "429" ,
339+ " message" : " Requests to the Embeddings_Create Operation under Azure OpenAI API version 2024-02-01 "
340+ " have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after "
341+ " 2 seconds. Please go here: https://aka.ms/oai/quotaincrease if you would like to further "
342+ " increase the default rate limit." ,
344343 }
345344 }
346345 body_mock = {"error" : "Rate limit exceeded" }
347346
348- rate_limit_error = RateLimitError ("Rate limit exceeded" , response = response_mock , body = body_mock )
347+ rate_limit_error = RateLimitError (
348+ "Rate limit exceeded" , response = response_mock , body = body_mock
349+ )
349350 get_orchestrator_config_mock .side_effect = rate_limit_error
350351
351352 # when
@@ -359,18 +360,20 @@ def test_conversation_custom_returns_error_response_on_rate_limit_error(
359360 assert response .status_code == 429
360361 assert response .json == {
361362 "error" : "We're currently experiencing a high number of requests for the service you're trying to access. "
362- "Please wait a moment and try again."
363+ "Please wait a moment and try again."
363364 }
364365
365366 @patch ("create_app.get_orchestrator_config" )
366367 def test_conversation_custom_returns_500_when_internalservererror_occurs (
367- self , get_orchestrator_config_mock , env_helper_mock , client
368+ self , get_orchestrator_config_mock , env_helper_mock , client
368369 ):
369370 """Test that an error response is returned when an exception occurs."""
370371 # given
371372 response_mock = MagicMock ()
372373 response_mock .status_code = 500
373- get_orchestrator_config_mock .side_effect = InternalServerError ("Test exception" , response = response_mock , body = "" )
374+ get_orchestrator_config_mock .side_effect = InternalServerError (
375+ "Test exception" , response = response_mock , body = ""
376+ )
374377
375378 # when
376379 response = client .post (
@@ -383,7 +386,7 @@ def test_conversation_custom_returns_500_when_internalservererror_occurs(
383386 assert response .status_code == 500
384387 assert response .json == {
385388 "error" : "An error occurred. Please try again. If the problem persists, please contact the site "
386- "administrator."
389+ "administrator."
387390 }
388391
389392 @patch ("create_app.get_message_orchestrator" )
@@ -756,13 +759,15 @@ def test_conversation_azure_byod_returns_500_when_exception_occurs(
756759
757760 @patch ("create_app.conversation_with_data" )
758761 def test_conversation_azure_byod_returns_500_when_internalservererror_occurs (
759- self , conversation_with_data_mock , env_helper_mock , client
762+ self , conversation_with_data_mock , env_helper_mock , client
760763 ):
761764 """Test that an error response is returned when an exception occurs."""
762765 # given
763766 response_mock = MagicMock ()
764767 response_mock .status_code = 500
765- conversation_with_data_mock .side_effect = InternalServerError ("Test exception" , response = response_mock , body = "" )
768+ conversation_with_data_mock .side_effect = InternalServerError (
769+ "Test exception" , response = response_mock , body = ""
770+ )
766771 env_helper_mock .CONVERSATION_FLOW = ConversationFlow .BYOD .value
767772
768773 # when
@@ -776,29 +781,30 @@ def test_conversation_azure_byod_returns_500_when_internalservererror_occurs(
776781 assert response .status_code == 500
777782 assert response .json == {
778783 "error" : "An error occurred. Please try again. If the problem persists, please contact the site "
779- "administrator."
784+ "administrator."
780785 }
781786
782787 @patch ("create_app.conversation_with_data" )
783788 def test_conversation_azure_byod_returns_429_on_rate_limit_error (
784- self , conversation_with_data_mock , env_helper_mock , client
789+ self , conversation_with_data_mock , env_helper_mock , client
785790 ):
786791 """Test that a 429 response is returned on RateLimitError for BYOD conversation."""
787792 # given
788793 response_mock = MagicMock ()
789794 response_mock .status_code = 400
790795 response_mock .json .return_value = {
791- ' error' : {
792- ' requestid' : ' f30740e1-c6e1-48ab-ab1e-35469ed41ba4' ,
793- ' code' : "400" ,
794- ' message' : ' An error occurred when calling Azure OpenAI: Rate limit reached for AOAI embedding '
795- 'resource: Server responded with status 429. Error message: {"error":{"code":"429",'
796- '"message": "Rate limit is exceeded. Try again in 44 seconds."}}'
796+ " error" : {
797+ " requestid" : " f30740e1-c6e1-48ab-ab1e-35469ed41ba4" ,
798+ " code" : "400" ,
799+ " message" : " An error occurred when calling Azure OpenAI: Rate limit reached for AOAI embedding "
800+ 'resource: Server responded with status 429. Error message: {"error":{"code":"429",'
801+ '"message": "Rate limit is exceeded. Try again in 44 seconds."}}' ,
797802 }
798803 }
799804
800- conversation_with_data_mock .side_effect = BadRequestError (message = "Error code: 400" , response = response_mock ,
801- body = "" )
805+ conversation_with_data_mock .side_effect = BadRequestError (
806+ message = "Error code: 400" , response = response_mock , body = ""
807+ )
802808 env_helper_mock .CONVERSATION_FLOW = ConversationFlow .BYOD .value
803809
804810 # when
@@ -812,7 +818,7 @@ def test_conversation_azure_byod_returns_429_on_rate_limit_error(
812818 assert response .status_code == 429
813819 assert response .json == {
814820 "error" : "We're currently experiencing a high number of requests for the service you're trying to access. "
815- "Please wait a moment and try again."
821+ "Please wait a moment and try again."
816822 }
817823
818824 @patch ("create_app.AzureOpenAI" )
0 commit comments