Skip to content

Commit e76724f

Browse files
committed
Updated tests
1 parent 604ec0e commit e76724f

2 files changed

Lines changed: 61 additions & 26 deletions

File tree

src/nypl_py_utils/classes/cloudlibrary_client.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import requests
55

66
from datetime import datetime, timedelta, timezone
7-
from enum import Enum
87
from nypl_py_utils.functions.log_helper import create_log
98
from requests.adapters import HTTPAdapter, Retry
109

@@ -46,7 +45,8 @@ def get_library_events(self, start_date=None,
4645
end_date = datetime.strftime(
4746
today, date_format) if end_date is None else end_date
4847

49-
if start_date > end_date:
48+
if (datetime.strptime(start_date, date_format) >
49+
datetime.strptime(end_date, date_format)):
5050
error_message = (f"Start date {start_date} greater than end date "
5151
f"{end_date}, cannot retrieve library events")
5252
self.logger.error(error_message)
@@ -143,11 +143,3 @@ def _build_authorization(self, method_type,
143143
class CloudLibraryClientError(Exception):
144144
def __init__(self, message=None):
145145
self.message = message
146-
147-
class CloudLibraryEventType(Enum):
148-
CHECKIN = "Patron checked in or returned an item"
149-
CHECKOUT = "Patron checked out or borrowed an item"
150-
HOLD = "Patron placed a hold request on a book"
151-
PURCHASE = "Library purchased an item (this could be another copy of a currently owned item)"
152-
RESERVED = "The title which is currently on hold for the patron is now available for checkout"
153-
REMOVED = "A copy of a item has expired or was deliberately removed from library stock"

tests/test_cloudlibrary_client.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,50 @@ def test_instance(self):
4040
"library_id", "account_id", "account_key")
4141

4242
def test_get_library_events_success_no_args(
43-
self, test_instance, requests_mock, caplog):
43+
self, test_instance, mocker, caplog):
4444
start = "2024-11-10T10:00:00"
4545
end = "2024-11-11T10:00:00"
46-
requests_mock.get(
47-
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
48-
text=_TEST_LIBRARY_EVENTS_RESPONSE)
46+
mock_request = mocker.patch(
47+
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa
48+
return_value=_TEST_LIBRARY_EVENTS_RESPONSE)
4949
response = test_instance.get_library_events()
5050

51-
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
51+
mock_request.assert_called_once_with(
52+
path=f"data/cloudevents?startdate={start}&enddate={end}",
53+
method_type="GET")
54+
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
5255
assert (f"Fetching all library events in time frame "
5356
f"{start} to {end}...") in caplog.text
5457

5558
def test_get_library_events_success_with_start_and_end_date(
56-
self, test_instance, requests_mock, caplog):
59+
self, test_instance, mocker, caplog):
5760
start = "2024-11-01T10:00:00"
5861
end = "2024-11-05T10:00:00"
59-
requests_mock.get(
60-
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
61-
text=_TEST_LIBRARY_EVENTS_RESPONSE)
62+
mock_request = mocker.patch(
63+
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa
64+
return_value=_TEST_LIBRARY_EVENTS_RESPONSE)
6265
response = test_instance.get_library_events(start, end)
6366

64-
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
67+
mock_request.assert_called_once_with(
68+
path=f"data/cloudevents?startdate={start}&enddate={end}",
69+
method_type="GET")
70+
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
6571
assert (f"Fetching all library events in time frame "
6672
f"{start} to {end}...") in caplog.text
6773

6874
def test_get_library_events_success_with_no_end_date(
69-
self, test_instance, requests_mock, caplog):
75+
self, test_instance, mocker, caplog):
7076
start = "2024-11-01T09:00:00"
7177
end = "2024-11-11T10:00:00"
72-
requests_mock.get(
73-
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
74-
text=_TEST_LIBRARY_EVENTS_RESPONSE)
78+
mock_request = mocker.patch(
79+
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa
80+
return_value=_TEST_LIBRARY_EVENTS_RESPONSE)
7581
response = test_instance.get_library_events(start)
7682

77-
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
83+
mock_request.assert_called_once_with(
84+
path=f"data/cloudevents?startdate={start}&enddate={end}",
85+
method_type="GET")
86+
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
7887
assert (f"Fetching all library events in time frame "
7988
f"{start} to {end}...") in caplog.text
8089

@@ -88,16 +97,50 @@ def test_get_library_events_exception_when_start_date_greater_than_end(
8897
assert (f"Start date {start} greater than end date "
8998
f"{end}, cannot retrieve library events") in caplog.text
9099

91-
def test_get_library_events_failure(self, test_instance, requests_mock):
100+
def test_get_library_events_exception_when_connection_timeout(
101+
self, test_instance, requests_mock):
92102
start = "2024-11-10T10:00:00"
93103
end = "2024-11-11T10:00:00"
104+
105+
# We're making sure that a separate error during a sub-method will
106+
# still result in CloudLibraryClientError
94107
requests_mock.get(
95108
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
96109
exc=ConnectTimeout)
97110

98111
with pytest.raises(CloudLibraryClientError):
99112
test_instance.get_library_events()
100113

114+
def test_request_success(self, test_instance, mocker):
115+
start = "2024-11-10T10:00:00"
116+
end = "2024-11-11T10:00:00"
117+
expected_headers = {'3mcl-Datetime': 'Mon, 11 Nov 2024 10:00:00 GMT',
118+
'3mcl-Authorization': '3MCLAUTH account_id:KipNmbVsmsT2xPjP4oHAaR3n00JgcszfF6mQRffBoRk=', # noqa
119+
'3mcl-APIVersion': '3.0.2',
120+
'Accept': 'application/xml'}
121+
mock_get = mocker.patch("requests.sessions.Session.get")
122+
test_instance.request(
123+
path=f"data/cloudevents?startdate={start}&enddate={end}",
124+
method_type="GET")
125+
126+
mock_get.assert_called_once_with(
127+
url=f"{_API_URL}library_id/data/cloudevents?startdate={start}&enddate={end}", # noqa
128+
data=None,
129+
headers=expected_headers,
130+
timeout=60)
131+
132+
def test_request_failure(self, test_instance, requests_mock, caplog):
133+
start = "2024-11-10T10:00:00"
134+
end = "2024-11-11T10:00:00"
135+
requests_mock.get(
136+
f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}", # noqa
137+
exc=ConnectTimeout)
138+
139+
with pytest.raises(CloudLibraryClientError):
140+
test_instance.request(
141+
path=f"data/cloudevents?startdate={start}&enddate={end}",
142+
method_type="GET")
143+
101144
def test_create_request_body_success(self, test_instance):
102145
request_type = "CheckoutRequest"
103146
item_id = "df45qw"

0 commit comments

Comments
 (0)