From 5b21d5704a6274ee710f43aa83d146bd416f9cdf Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 5 Dec 2023 11:08:14 -0800 Subject: [PATCH] fixed test_sendchat --- aider/sendchat.py | 4 ++-- tests/test_sendchat.py | 41 +++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/aider/sendchat.py b/aider/sendchat.py index 65b0a46cb..c770ef087 100644 --- a/aider/sendchat.py +++ b/aider/sendchat.py @@ -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( diff --git a/tests/test_sendchat.py b/tests/test_sendchat.py index a63ce3af1..7bb8fcfab 100644 --- a/tests/test_sendchat.py +++ b/tests/test_sendchat.py @@ -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()