Skip to content

Commit a1e31b8

Browse files
author
Daniel Bush
committed
test: use httpretty for retry test
1 parent dcd49d1 commit a1e31b8

File tree

5 files changed

+66
-157
lines changed

5 files changed

+66
-157
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install dependencies
2727
run: |
2828
python -m pip install --upgrade pip
29-
pip install pytest coverage codecov
29+
pip install pytest coverage codecov httpretty
3030
pip install .
3131
type python
3232
type pip

pytest.ini

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/tests_client.py

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import json
12
import unittest
23
import warnings
34
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
66
from uuid import UUID, uuid4
77

8-
from sypht.client import SyphtClient
8+
import httpretty
9+
import pytest
910

10-
from .util.mock_http_server import MockRequestHandler, MockServerSession
11+
from sypht.client import SyphtClient
1112

1213

1314
def validate_uuid4(uuid_string):
@@ -45,11 +46,7 @@ def test_data_extraction_1(self):
4546
fid = self.sypht_client.upload(f, ["invoices:2"])
4647
self.assertTrue(validate_uuid4(fid))
4748

48-
import json
49-
50-
print("<< fid", fid)
5149
results = self.sypht_client.fetch_results(fid)
52-
print("<< results", json.dumps(results, indent=2))
5350

5451
self.assertTrue(isinstance(results, dict))
5552
self.assertIn("invoice.dueDate", results)
@@ -105,65 +102,70 @@ class RetryTest(unittest.TestCase):
105102

106103
@patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100))
107104
@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):
109107
# 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": []}
140136

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"),
165165
)
166166

167+
assert self.count == 4, "should be 1 req + 3 retries"
168+
167169

168170
if __name__ == "__main__":
169171
unittest.main()

tests/util/__init__.py

Whitespace-only changes.

tests/util/mock_http_server.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)