mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-30 09:14:59 +00:00
fixed test_sendchat
This commit is contained in:
parent
2ed0c8fb66
commit
5b21d5704a
2 changed files with 25 additions and 20 deletions
|
@ -2,8 +2,8 @@ import hashlib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import backoff
|
import backoff
|
||||||
|
import httpx
|
||||||
import openai
|
import openai
|
||||||
import requests
|
|
||||||
|
|
||||||
# from diskcache import Cache
|
# from diskcache import Cache
|
||||||
from openai import APIConnectionError, InternalServerError, RateLimitError
|
from openai import APIConnectionError, InternalServerError, RateLimitError
|
||||||
|
@ -21,7 +21,7 @@ CACHE = None
|
||||||
InternalServerError,
|
InternalServerError,
|
||||||
RateLimitError,
|
RateLimitError,
|
||||||
APIConnectionError,
|
APIConnectionError,
|
||||||
requests.exceptions.ConnectionError,
|
httpx.ConnectError,
|
||||||
),
|
),
|
||||||
max_tries=10,
|
max_tries=10,
|
||||||
on_backoff=lambda details: print(
|
on_backoff=lambda details: print(
|
||||||
|
|
|
@ -1,41 +1,46 @@
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import httpx
|
||||||
import openai
|
import openai
|
||||||
import requests
|
|
||||||
|
|
||||||
from aider.sendchat import send_with_retries
|
from aider.sendchat import send_with_retries
|
||||||
|
|
||||||
|
|
||||||
|
class PrintCalled(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TestSendChat(unittest.TestCase):
|
class TestSendChat(unittest.TestCase):
|
||||||
@patch("aider.sendchat.openai.ChatCompletion.create")
|
|
||||||
@patch("builtins.print")
|
@patch("builtins.print")
|
||||||
def test_send_with_retries_rate_limit_error(self, mock_print, mock_chat_completion_create):
|
def test_send_with_retries_rate_limit_error(self, mock_print):
|
||||||
# Set up the mock to raise RateLimitError on
|
mock_client = MagicMock()
|
||||||
# the first call and return None on the second call
|
|
||||||
mock_chat_completion_create.side_effect = [
|
# Set up the mock to raise
|
||||||
openai.RateLimitError("Rate limit exceeded"),
|
mock_client.chat.completions.create.side_effect = [
|
||||||
|
openai.RateLimitError(
|
||||||
|
"rate limit exceeded",
|
||||||
|
response=MagicMock(),
|
||||||
|
body=None,
|
||||||
|
),
|
||||||
None,
|
None,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Call the send_with_retries method
|
# Call the send_with_retries method
|
||||||
send_with_retries("model", ["message"], None, False)
|
send_with_retries(mock_client, "model", ["message"], None, False)
|
||||||
|
|
||||||
# Assert that print was called once
|
|
||||||
mock_print.assert_called_once()
|
mock_print.assert_called_once()
|
||||||
|
|
||||||
@patch("aider.sendchat.openai.ChatCompletion.create")
|
@patch("aider.sendchat.openai.ChatCompletion.create")
|
||||||
@patch("builtins.print")
|
@patch("builtins.print")
|
||||||
def test_send_with_retries_connection_error(self, mock_print, mock_chat_completion_create):
|
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
|
mock_client = MagicMock()
|
||||||
# and return None on the second call
|
|
||||||
mock_chat_completion_create.side_effect = [
|
# Set up the mock to raise
|
||||||
requests.exceptions.ConnectionError("Connection error"),
|
mock_client.chat.completions.create.side_effect = [
|
||||||
|
httpx.ConnectError("Connection error"),
|
||||||
None,
|
None,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Call the send_with_retries method
|
# Call the send_with_retries method
|
||||||
send_with_retries("model", ["message"], None, False)
|
send_with_retries(mock_client, "model", ["message"], None, False)
|
||||||
|
|
||||||
# Assert that print was called once
|
|
||||||
mock_print.assert_called_once()
|
mock_print.assert_called_once()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue