Skip to content

Commit

Permalink
test: use httpretty for retry test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Bush committed Sep 27, 2023
1 parent dcd49d1 commit 08955b7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest coverage codecov
pip install pytest coverage codecov httpretty
pip install .
type python
type pip
Expand Down
125 changes: 64 additions & 61 deletions tests/tests_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import json
import unittest
import warnings
from datetime import datetime, timedelta
from http.client import HTTPMessage
from unittest.mock import ANY, Mock, call, patch
from unittest.mock import Mock, patch
from uuid import UUID, uuid4

from sypht.client import SyphtClient
import httpretty

from .util.mock_http_server import MockRequestHandler, MockServerSession
from sypht.client import SyphtClient


def validate_uuid4(uuid_string):
Expand Down Expand Up @@ -45,8 +45,6 @@ def test_data_extraction_1(self):
fid = self.sypht_client.upload(f, ["invoices:2"])
self.assertTrue(validate_uuid4(fid))

import json

print("<< fid", fid)
results = self.sypht_client.fetch_results(fid)
print("<< results", json.dumps(results, indent=2))
Expand Down Expand Up @@ -105,65 +103,70 @@ class RetryTest(unittest.TestCase):

@patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100))
@patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100))
def test_it_should_eventually_fail_for_50x(self, auth_v1: Mock, auth_v2: Mock):
@httpretty.activate(verbose=True, allow_net_connect=False)
def test_it_should_retry_n_times(self, auth_v1: Mock, auth_v2: Mock):
# arrange
requests = []

def create_request_handler(*args, **kwargs):
response_sequences = {
"/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01": [
(502, {}),
# Retries start from here...
# There should be n for where Retry(status=n).
(503, {}),
(504, {}),
(502, {}),
],
}
return MockRequestHandler(
*args, **kwargs, requests=requests, responses=response_sequences
)

with MockServerSession(create_request_handler) as address:
sypht_client = SyphtClient(base_endpoint=address)

# act / assert
with self.assertRaisesRegex(Exception, ".") as e:
sypht_client.get_annotations(
from_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
to_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
)
self.count = 0

def get_annotations(request, uri, response_headers):
self.count += 1
# 1 req + 3 retries = 4
if self.count == 4:
return [200, response_headers, json.dumps({"annotations": []})]
return [502, response_headers, json.dumps({})]

httpretty.register_uri(
httpretty.GET,
"https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
body=get_annotations,
)

sypht_client = SyphtClient(base_endpoint="https://api.sypht.com")

# act / assert
response = sypht_client.get_annotations(
from_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
to_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
)

assert response == {"annotations": []}

self.assertEqual(
[
(
"GET",
"/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
{},
),
(
"GET",
"/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
{},
),
(
"GET",
"/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
{},
),
(
"GET",
"/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
{},
),
],
requests,
@patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100))
@patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100))
@httpretty.activate(verbose=True, allow_net_connect=False)
def test_retry_should_eventually_fail_for_50x(self, auth_v1: Mock, auth_v2: Mock):
# arrange
self.count = 0

def get_annotations(request, uri, response_headers):
self.count += 1
return [502, response_headers, json.dumps({})]

httpretty.register_uri(
httpretty.GET,
"https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01",
body=get_annotations,
)

sypht_client = SyphtClient(base_endpoint="https://api.sypht.com")

# act / assert
with self.assertRaisesRegex(Exception, ".") as e:
sypht_client.get_annotations(
from_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
to_date=datetime(
year=2021, month=1, day=1, hour=0, minute=0, second=0
).strftime("%Y-%m-%d"),
)

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


if __name__ == "__main__":
unittest.main()
Empty file removed tests/util/__init__.py
Empty file.
91 changes: 0 additions & 91 deletions tests/util/mock_http_server.py

This file was deleted.

0 comments on commit 08955b7

Please sign in to comment.