|
1 | 1 | import unittest
|
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from parameterized import parameterized |
2 | 5 |
|
3 | 6 | from src.amplitude_experiment import RemoteEvaluationClient, Variant, User, RemoteEvaluationConfig
|
| 7 | +from src.amplitude_experiment.exception import FetchException |
4 | 8 |
|
5 | 9 | API_KEY = 'client-DvWljIjiiuqLbyjqdvBaLFfEBrAvGuA3'
|
6 | 10 | SERVER_URL = 'https://api.lab.amplitude.com/sdk/vardata'
|
@@ -36,11 +40,33 @@ def test_fetch_async(self):
|
36 | 40 | self.client.fetch_async(user, self.callback_for_async)
|
37 | 41 |
|
38 | 42 | def test_fetch_failed_with_retry(self):
|
39 |
| - with RemoteEvaluationClient(API_KEY, RemoteEvaluationConfig(debug=False, fetch_retries=1, fetch_timeout_millis=1)) as client: |
| 43 | + with RemoteEvaluationClient(API_KEY, RemoteEvaluationConfig(debug=False, fetch_retries=1, |
| 44 | + fetch_timeout_millis=1)) as client: |
40 | 45 | user = User(user_id='test_user')
|
41 | 46 | variants = client.fetch(user)
|
42 | 47 | self.assertEqual({}, variants)
|
43 | 48 |
|
| 49 | + @parameterized.expand([ |
| 50 | + (300, "Fetch Exception 300", True), |
| 51 | + (400, "Fetch Exception 400", False), |
| 52 | + (429, "Fetch Exception 429", True), |
| 53 | + (500, "Fetch Exception 500", True), |
| 54 | + (000, "Other Exception", True), |
| 55 | + ]) |
| 56 | + @mock.patch("src.amplitude_experiment.remote.client.RemoteEvaluationClient._RemoteEvaluationClient__retry_fetch") |
| 57 | + @mock.patch("src.amplitude_experiment.remote.client.RemoteEvaluationClient._RemoteEvaluationClient__do_fetch") |
| 58 | + def test_fetch_retry_with_response(self, response_code, error_message, should_call_retry, mock_do_fetch, |
| 59 | + mock_retry_fetch): |
| 60 | + if response_code == 000: |
| 61 | + mock_do_fetch.side_effect = Exception(error_message) |
| 62 | + else: |
| 63 | + mock_do_fetch.side_effect = FetchException(response_code, error_message) |
| 64 | + instance = RemoteEvaluationClient(API_KEY, RemoteEvaluationConfig(fetch_retries=1)) |
| 65 | + user = User(user_id='test_user') |
| 66 | + instance.fetch(user) |
| 67 | + mock_do_fetch.assert_called_once_with(user) |
| 68 | + self.assertEqual(should_call_retry, mock_retry_fetch.called) |
| 69 | + |
44 | 70 |
|
45 | 71 | if __name__ == '__main__':
|
46 | 72 | unittest.main()
|
0 commit comments