fixed test_sendchat

This commit is contained in:
Paul Gauthier 2023-12-05 11:08:14 -08:00
parent 2ed0c8fb66
commit 5b21d5704a
2 changed files with 25 additions and 20 deletions

View file

@ -2,8 +2,8 @@ import hashlib
import json
import backoff
import httpx
import openai
import requests
# from diskcache import Cache
from openai import APIConnectionError, InternalServerError, RateLimitError
@ -21,7 +21,7 @@ CACHE = None
InternalServerError,
RateLimitError,
APIConnectionError,
requests.exceptions.ConnectionError,
httpx.ConnectError,
),
max_tries=10,
on_backoff=lambda details: print(

View file

@ -1,41 +1,46 @@
import unittest
from unittest.mock import patch
from unittest.mock import MagicMock, patch
import httpx
import openai
import requests
from aider.sendchat import send_with_retries
class PrintCalled(Exception):
pass
class TestSendChat(unittest.TestCase):
@patch("aider.sendchat.openai.ChatCompletion.create")
@patch("builtins.print")
def test_send_with_retries_rate_limit_error(self, mock_print, mock_chat_completion_create):
# Set up the mock to raise RateLimitError on
# the first call and return None on the second call
mock_chat_completion_create.side_effect = [
openai.RateLimitError("Rate limit exceeded"),
def test_send_with_retries_rate_limit_error(self, mock_print):
mock_client = MagicMock()
# Set up the mock to raise
mock_client.chat.completions.create.side_effect = [
openai.RateLimitError(
"rate limit exceeded",
response=MagicMock(),
body=None,
),
None,
]
# Call the send_with_retries method
send_with_retries("model", ["message"], None, False)
# Assert that print was called once
send_with_retries(mock_client, "model", ["message"], None, False)
mock_print.assert_called_once()
@patch("aider.sendchat.openai.ChatCompletion.create")
@patch("builtins.print")
def test_send_with_retries_connection_error(self, mock_print, mock_chat_completion_create):
# Set up the mock to raise ConnectionError on the first call
# and return None on the second call
mock_chat_completion_create.side_effect = [
requests.exceptions.ConnectionError("Connection error"),
mock_client = MagicMock()
# Set up the mock to raise
mock_client.chat.completions.create.side_effect = [
httpx.ConnectError("Connection error"),
None,
]
# Call the send_with_retries method
send_with_retries("model", ["message"], None, False)
# Assert that print was called once
send_with_retries(mock_client, "model", ["message"], None, False)
mock_print.assert_called_once()