Skip to content

Commit

Permalink
Added caplog checks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fatimarahman committed Nov 20, 2024
1 parent e2c88b3 commit 5a882c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/nypl_py_utils/classes/cloudlibrary_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def request(self, path, method_type="GET",
timeout=60)
response.raise_for_status()
except Exception as e:
error_message = f"Failed to retrieve response from {url}: {e}"
error_message = (f"Failed to retrieve response from {url}: "
f"{repr(e)}")
self.logger.error(error_message)
raise CloudLibraryClientError(error_message)

Expand Down
20 changes: 13 additions & 7 deletions tests/test_cloudlibrary_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,29 @@ def test_get_library_events_success_with_no_end_date(
assert response == _TEST_LIBRARY_EVENTS_RESPONSE

def test_get_library_events_exception_when_start_date_greater_than_end(
self, test_instance):
self, test_instance, caplog):
start = "2024-11-11T09:00:00"
end = "2024-11-01T10:00:00"

with pytest.raises(CloudLibraryClientError):
test_instance.get_library_events(start, end)
assert (f"Start date {start} greater than end date {end}, "
f"cannot retrieve library events") in caplog.text

def test_get_library_events_exception_when_connection_timeout(
self, test_instance, requests_mock):
self, test_instance, requests_mock, caplog):
start = "2024-11-10T10:00:00"
end = "2024-11-11T10:00:00"
url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa

# We're making sure that a separate error during a sub-method will
# still result in CloudLibraryClientError
requests_mock.get(
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
exc=ConnectTimeout)
url, exc=ConnectTimeout)

with pytest.raises(CloudLibraryClientError):
test_instance.get_library_events()
assert (f"Failed to retrieve response from {url}") in caplog.text

def test_get_request_success(self, test_instance, requests_mock):
start = "2024-11-10T10:00:00"
Expand Down Expand Up @@ -171,17 +174,20 @@ def test_post_request_success(self, test_instance, requests_mock):
assert expected_headers.items() <= dict(
requests_mock.request_history[0].headers).items()

def test_request_failure(self, test_instance, requests_mock):
def test_request_failure(self, test_instance,
requests_mock, caplog):
start = "2024-11-10T10:00:00"
end = "2024-11-11T10:00:00"
url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa
requests_mock.get(
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
exc=ConnectTimeout)
url, exc=ConnectTimeout)

with pytest.raises(CloudLibraryClientError):
test_instance.request(
path=f"data/cloudevents?startdate={start}&enddate={end}",
method_type="GET")
assert (f"Failed to retrieve response from "
f"{url}: ConnectTimeout()") in caplog.text

def test_create_request_body_success(self, test_instance):
request_type = "CheckoutRequest"
Expand Down

0 comments on commit 5a882c6

Please sign in to comment.