mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-20 20:35:00 +00:00
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.
This commit is contained in:
parent
3bdddf4b36
commit
2d1fc9f6da
1 changed files with 18 additions and 0 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue