XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

add(quora/tests): Added a module with quora tests. It is covering 3 scenarios: 1. test_successful_request 2. test_exponential backoff 3. test_too_many_requests

Run tests: python -m unittest gpt4free/quora/tests/test_api.py

cc9179ed
valerii@valeriis-air.(none) <valery.chirkov99@gmail.com>
提交于

代码差异

2 个文件 +38 -0
Added gpt4free/quora/tests/__init__.py +0 -0
此文件没有可显示的逐行差异。
Added gpt4free/quora/tests/test_api.py +38 -0
@@ -0,0 +1,38 @@
1 import unittest
2 import requests
3 from unittest.mock import MagicMock
4 from gpt4free.quora.api import retry_request
5
6
7 class TestRetryRequest(unittest.TestCase):
8 def test_successful_request(self):
9 # Mock a successful request with a 200 status code
10 mock_response = MagicMock()
11 mock_response.status_code = 200
12 requests.get = MagicMock(return_value=mock_response)
13
14 # Call the function and assert that it returns the response
15 response = retry_request(requests.get, "http://example.com", max_attempts=3)
16 self.assertEqual(response.status_code, 200)
17
18 def test_exponential_backoff(self):
19 # Mock a failed request that succeeds after two retries
20 mock_response = MagicMock()
21 mock_response.status_code = 200
22 requests.get = MagicMock(side_effect=[requests.exceptions.RequestException] * 2 + [mock_response])
23
24 # Call the function and assert that it retries with exponential backoff
25 with self.assertLogs() as logs:
26 response = retry_request(requests.get, "http://example.com", max_attempts=3, delay=1)
27 self.assertEqual(response.status_code, 200)
28 self.assertGreaterEqual(len(logs.output), 2)
29 self.assertIn("Retrying in 1 seconds...", logs.output[0])
30 self.assertIn("Retrying in 2 seconds...", logs.output[1])
31
32 def test_too_many_attempts(self):
33 # Mock a failed request that never succeeds
34 requests.get = MagicMock(side_effect=requests.exceptions.RequestException)
35
36 # Call the function and assert that it raises an exception after the maximum number of attempts
37 with self.assertRaises(RuntimeError):
38 retry_request(requests.get, "http://example.com", max_attempts=3)