From 2d1fc9f6da180f76472017eab6a0c93b93fc2953 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 3 Jun 2023 06:22:33 -0700 Subject: [PATCH] aider: Added a new test case `test_send_with_retries_connection_error` to ensure that `send_with_retries` handles the `ConnectionError` correctly and calls `time.sleep` once before retrying. # Context: USER: make another test like that, but raise requests.exceptions.ConnectionError ASSISTANT: To add another test case for `coder.send_with_retries(model, messages)` that raises `requests.exceptions.ConnectionError`, we will modify the `tests/test_coder.py` file. We will create a new test method called `test_send_with_retries_connection_error` and use `patch` to mock the `openai.ChatCompletion.create` and `time.sleep` methods. Here's the code change for the new test case: ```python tests/test_coder.py <<<<<<< ORIGINAL if __name__ == "__main__": unittest.main() ======= @patch("aider.coder.openai.ChatCompletion.create") @patch("aider.coder.time.sleep") def test_send_with_retries_connection_error(self, mock_sleep, mock_chat_completion_create): # Mock the IO object mock_io = MagicMock() # Initialize the Coder object with the mocked IO and mocked repo coder = Coder(io=mock_io, openai_api_key="fake_key") # 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"), None] # Call the send_with_retries method coder.send_with_retries("model", ["message"]) # Assert that time.sleep was called once mock_sleep.assert_called_once() if __name__ == "__main__": unittest.main() >>>>>>> UPDATED ``` This test case will ensure that `send_with_retries` handles the `ConnectionError` correctly and calls `time.sleep` once before retrying. --- tests/test_coder.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_coder.py b/tests/test_coder.py index 61b60cc79..e50ac756b 100644 --- a/tests/test_coder.py +++ b/tests/test_coder.py @@ -142,5 +142,23 @@ class TestCoder(unittest.TestCase): mock_sleep.assert_called_once() + @patch("aider.coder.openai.ChatCompletion.create") + @patch("aider.coder.time.sleep") + def test_send_with_retries_connection_error(self, mock_sleep, mock_chat_completion_create): + # Mock the IO object + mock_io = MagicMock() + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder(io=mock_io, openai_api_key="fake_key") + + # 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"), None] + + # Call the send_with_retries method + coder.send_with_retries("model", ["message"]) + + # Assert that time.sleep was called once + mock_sleep.assert_called_once() + if __name__ == "__main__": unittest.main()