|
| 1 | +import json |
1 | 2 | import unittest |
2 | 3 | import warnings |
3 | 4 | from datetime import datetime, timedelta |
4 | | -from http.client import HTTPMessage |
5 | | -from unittest.mock import ANY, Mock, call, patch |
| 5 | +from unittest.mock import Mock, patch |
6 | 6 | from uuid import UUID, uuid4 |
7 | 7 |
|
8 | | -from sypht.client import SyphtClient |
| 8 | +import httpretty |
| 9 | +import pytest |
9 | 10 |
|
10 | | -from .util.mock_http_server import MockRequestHandler, MockServerSession |
| 11 | +from sypht.client import SyphtClient |
11 | 12 |
|
12 | 13 |
|
13 | 14 | def validate_uuid4(uuid_string): |
@@ -45,11 +46,7 @@ def test_data_extraction_1(self): |
45 | 46 | fid = self.sypht_client.upload(f, ["invoices:2"]) |
46 | 47 | self.assertTrue(validate_uuid4(fid)) |
47 | 48 |
|
48 | | - import json |
49 | | - |
50 | | - print("<< fid", fid) |
51 | 49 | results = self.sypht_client.fetch_results(fid) |
52 | | - print("<< results", json.dumps(results, indent=2)) |
53 | 50 |
|
54 | 51 | self.assertTrue(isinstance(results, dict)) |
55 | 52 | self.assertIn("invoice.dueDate", results) |
@@ -105,65 +102,70 @@ class RetryTest(unittest.TestCase): |
105 | 102 |
|
106 | 103 | @patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100)) |
107 | 104 | @patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100)) |
108 | | - def test_it_should_eventually_fail_for_50x(self, auth_v1: Mock, auth_v2: Mock): |
| 105 | + @httpretty.activate(verbose=True, allow_net_connect=False) |
| 106 | + def test_it_should_retry_n_times(self, auth_v1: Mock, auth_v2: Mock): |
109 | 107 | # arrange |
110 | | - requests = [] |
111 | | - |
112 | | - def create_request_handler(*args, **kwargs): |
113 | | - response_sequences = { |
114 | | - "/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01": [ |
115 | | - (502, {}), |
116 | | - # Retries start from here... |
117 | | - # There should be n for where Retry(status=n). |
118 | | - (503, {}), |
119 | | - (504, {}), |
120 | | - (502, {}), |
121 | | - ], |
122 | | - } |
123 | | - return MockRequestHandler( |
124 | | - *args, **kwargs, requests=requests, responses=response_sequences |
125 | | - ) |
126 | | - |
127 | | - with MockServerSession(create_request_handler) as address: |
128 | | - sypht_client = SyphtClient(base_endpoint=address) |
129 | | - |
130 | | - # act / assert |
131 | | - with self.assertRaisesRegex(Exception, ".") as e: |
132 | | - sypht_client.get_annotations( |
133 | | - from_date=datetime( |
134 | | - year=2021, month=1, day=1, hour=0, minute=0, second=0 |
135 | | - ).strftime("%Y-%m-%d"), |
136 | | - to_date=datetime( |
137 | | - year=2021, month=1, day=1, hour=0, minute=0, second=0 |
138 | | - ).strftime("%Y-%m-%d"), |
139 | | - ) |
| 108 | + self.count = 0 |
| 109 | + |
| 110 | + def get_annotations(request, uri, response_headers): |
| 111 | + self.count += 1 |
| 112 | + # 1 req + 3 retries = 4 |
| 113 | + if self.count == 4: |
| 114 | + return [200, response_headers, json.dumps({"annotations": []})] |
| 115 | + return [502, response_headers, json.dumps({})] |
| 116 | + |
| 117 | + httpretty.register_uri( |
| 118 | + httpretty.GET, |
| 119 | + "https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
| 120 | + body=get_annotations, |
| 121 | + ) |
| 122 | + |
| 123 | + sypht_client = SyphtClient(base_endpoint="https://api.sypht.com") |
| 124 | + |
| 125 | + # act / assert |
| 126 | + response = sypht_client.get_annotations( |
| 127 | + from_date=datetime( |
| 128 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 129 | + ).strftime("%Y-%m-%d"), |
| 130 | + to_date=datetime( |
| 131 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 132 | + ).strftime("%Y-%m-%d"), |
| 133 | + ) |
| 134 | + |
| 135 | + assert response == {"annotations": []} |
140 | 136 |
|
141 | | - self.assertEqual( |
142 | | - [ |
143 | | - ( |
144 | | - "GET", |
145 | | - "/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
146 | | - {}, |
147 | | - ), |
148 | | - ( |
149 | | - "GET", |
150 | | - "/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
151 | | - {}, |
152 | | - ), |
153 | | - ( |
154 | | - "GET", |
155 | | - "/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
156 | | - {}, |
157 | | - ), |
158 | | - ( |
159 | | - "GET", |
160 | | - "/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
161 | | - {}, |
162 | | - ), |
163 | | - ], |
164 | | - requests, |
| 137 | + @patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100)) |
| 138 | + @patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100)) |
| 139 | + @httpretty.activate(verbose=True, allow_net_connect=False) |
| 140 | + def test_retry_should_eventually_fail_for_50x(self, auth_v1: Mock, auth_v2: Mock): |
| 141 | + # arrange |
| 142 | + self.count = 0 |
| 143 | + |
| 144 | + def get_annotations(request, uri, response_headers): |
| 145 | + self.count += 1 |
| 146 | + return [502, response_headers, json.dumps({})] |
| 147 | + |
| 148 | + httpretty.register_uri( |
| 149 | + httpretty.GET, |
| 150 | + "https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
| 151 | + body=get_annotations, |
| 152 | + ) |
| 153 | + |
| 154 | + sypht_client = SyphtClient(base_endpoint="https://api.sypht.com") |
| 155 | + |
| 156 | + # act / assert |
| 157 | + with self.assertRaisesRegex(Exception, ".") as e: |
| 158 | + sypht_client.get_annotations( |
| 159 | + from_date=datetime( |
| 160 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 161 | + ).strftime("%Y-%m-%d"), |
| 162 | + to_date=datetime( |
| 163 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 164 | + ).strftime("%Y-%m-%d"), |
165 | 165 | ) |
166 | 166 |
|
| 167 | + assert self.count == 4, "should be 1 req + 3 retries" |
| 168 | + |
167 | 169 |
|
168 | 170 | if __name__ == "__main__": |
169 | 171 | unittest.main() |
0 commit comments