diff options
author | t.me/xtekky <98614666+xtekky@users.noreply.github.com> | 2023-05-05 10:23:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-05 10:23:14 +0200 |
commit | 4825a8457293ebb4ab8de944f6bcb165728767fd (patch) | |
tree | 44519bfb8bf4bbf67dac64472d95f8dae5d1e8a3 /gpt4free/quora/tests/test_api.py | |
parent | Merge pull request #452 from ego/feature/requirements-and-docs (diff) | |
parent | fix(Dockerfile): Fixed issue with unable to fetch the response. (diff) | |
download | gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar.gz gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar.bz2 gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar.lz gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar.xz gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.tar.zst gpt4free-4825a8457293ebb4ab8de944f6bcb165728767fd.zip |
Diffstat (limited to 'gpt4free/quora/tests/test_api.py')
-rw-r--r-- | gpt4free/quora/tests/test_api.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/gpt4free/quora/tests/test_api.py b/gpt4free/quora/tests/test_api.py new file mode 100644 index 00000000..2a4bb41b --- /dev/null +++ b/gpt4free/quora/tests/test_api.py @@ -0,0 +1,38 @@ +import unittest +import requests +from unittest.mock import MagicMock +from gpt4free.quora.api import retry_request + + +class TestRetryRequest(unittest.TestCase): + def test_successful_request(self): + # Mock a successful request with a 200 status code + mock_response = MagicMock() + mock_response.status_code = 200 + requests.get = MagicMock(return_value=mock_response) + + # Call the function and assert that it returns the response + response = retry_request(requests.get, "http://example.com", max_attempts=3) + self.assertEqual(response.status_code, 200) + + def test_exponential_backoff(self): + # Mock a failed request that succeeds after two retries + mock_response = MagicMock() + mock_response.status_code = 200 + requests.get = MagicMock(side_effect=[requests.exceptions.RequestException] * 2 + [mock_response]) + + # Call the function and assert that it retries with exponential backoff + with self.assertLogs() as logs: + response = retry_request(requests.get, "http://example.com", max_attempts=3, delay=1) + self.assertEqual(response.status_code, 200) + self.assertGreaterEqual(len(logs.output), 2) + self.assertIn("Retrying in 1 seconds...", logs.output[0]) + self.assertIn("Retrying in 2 seconds...", logs.output[1]) + + def test_too_many_attempts(self): + # Mock a failed request that never succeeds + requests.get = MagicMock(side_effect=requests.exceptions.RequestException) + + # Call the function and assert that it raises an exception after the maximum number of attempts + with self.assertRaises(RuntimeError): + retry_request(requests.get, "http://example.com", max_attempts=3) |