Skip to content

Commit 1f356b7

Browse files
committed
Update tests and method descriptions
1 parent 4c82ae1 commit 1f356b7

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

src/nypl_py_utils/classes/cloudlibrary_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_library_events(self, start_date=None,
3232
end_date=None) -> requests.Response:
3333
"""
3434
Retrieves all the events related to library-owned items within the
35-
optional timeframe. Pulls yesterday's events by default.
35+
optional timeframe. Pulls past 24 hours of events by default.
3636
3737
start_date and end_date are optional parameters, and must be
3838
formatted either YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS
@@ -80,8 +80,8 @@ def request(self, path, method_type="GET",
8080
This method is necessary for building headers/authorization.
8181
Example usage of this method is in the get_library_events function.
8282
83-
Returns Response object by default -- you will need to parse the object
84-
to retrieve text, status code, etc.
83+
Returns Response object by default -- you will need to parse this
84+
object to retrieve response text, status codes, etc.
8585
"""
8686
extended_path = f"/cirrus/library/{self.library_id}/{path}"
8787
headers = self._build_headers(method_type, extended_path)

tests/test_cloudlibrary_client.py

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,72 @@ def test_get_library_events_exception_when_connection_timeout(
103103
with pytest.raises(CloudLibraryClientError):
104104
test_instance.get_library_events()
105105

106-
def test_request_success(self, test_instance, mocker):
106+
def test_get_request_success(self, test_instance, requests_mock):
107107
start = "2024-11-10T10:00:00"
108108
end = "2024-11-11T10:00:00"
109-
expected_headers = {'3mcl-Datetime': 'Mon, 11 Nov 2024 10:00:00 GMT',
110-
'3mcl-Authorization': '3MCLAUTH account_id:KipNmbVsmsT2xPjP4oHAaR3n00JgcszfF6mQRffBoRk=', # noqa
111-
'3mcl-APIVersion': '3.0.2',
112-
'Accept': 'application/xml'}
113-
mock_get = mocker.patch("requests.sessions.Session.get")
114-
test_instance.request(
109+
url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa
110+
expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT",
111+
"3mcl-Authorization": "3MCLAUTH account_id:KipNmbVsmsT2xPjP4oHAaR3n00JgcszfF6mQRffBoRk=", # noqa
112+
"3mcl-APIVersion": "3.0.2",
113+
"Accept": "application/xml"}
114+
requests_mock.get(
115+
url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE)
116+
117+
response = test_instance.request(
115118
path=f"data/cloudevents?startdate={start}&enddate={end}",
116119
method_type="GET")
117120

118-
mock_get.assert_called_once_with(
119-
url=f"{_API_URL}library_id/data/cloudevents?startdate={start}&enddate={end}", # noqa
120-
data=None,
121-
headers=expected_headers,
122-
timeout=60)
121+
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
122+
assert requests_mock.request_history[0].method == "GET"
123+
assert requests_mock.request_history[0].url == url
124+
assert expected_headers.items() <= dict(
125+
requests_mock.request_history[0].headers).items()
126+
127+
def test_put_request_success(self, test_instance, requests_mock):
128+
start = "2024-11-10T10:00:00"
129+
end = "2024-11-11T10:00:00"
130+
url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa
131+
expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT",
132+
"3mcl-Authorization": "3MCLAUTH account_id:3M773C6ZVWmB/ISoSjQy9iBp48T4tUWhoNOwXaseMtE=", # noqa
133+
"3mcl-APIVersion": "3.0.2",
134+
"Content-Type": "application/xml"}
135+
requests_mock.put(
136+
url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE)
137+
138+
response = test_instance.request(
139+
path=f"data/cloudevents?startdate={start}&enddate={end}",
140+
method_type="PUT",
141+
body={"test": "test"})
142+
143+
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
144+
assert requests_mock.request_history[0].method == "PUT"
145+
assert requests_mock.request_history[0].url == url
146+
assert requests_mock.request_history[0].body == "test=test"
147+
assert expected_headers.items() <= dict(
148+
requests_mock.request_history[0].headers).items()
149+
150+
def test_post_request_success(self, test_instance, requests_mock):
151+
start = "2024-11-10T10:00:00"
152+
end = "2024-11-11T10:00:00"
153+
url = f"{_API_URL}{test_instance.library_id}/data/cloudevents?startdate={start}&enddate={end}" # noqa
154+
expected_headers = {"3mcl-Datetime": "Mon, 11 Nov 2024 10:00:00 GMT",
155+
"3mcl-Authorization": "3MCLAUTH account_id:vF0zI6ee1w1PbTLQ9EVvtxRly2vpCRxdBdAHb8DZQ4E=", # noqa
156+
"3mcl-APIVersion": "3.0.2",
157+
"Content-Type": "application/xml"}
158+
requests_mock.post(
159+
url=url, text=_TEST_LIBRARY_EVENTS_RESPONSE)
160+
161+
response = test_instance.request(
162+
path=f"data/cloudevents?startdate={start}&enddate={end}",
163+
method_type="POST",
164+
body={"test": "test"})
165+
166+
assert response.text == _TEST_LIBRARY_EVENTS_RESPONSE
167+
assert requests_mock.request_history[0].method == "POST"
168+
assert requests_mock.request_history[0].url == url
169+
assert requests_mock.request_history[0].body == "test=test"
170+
assert expected_headers.items() <= dict(
171+
requests_mock.request_history[0].headers).items()
123172

124173
def test_request_failure(self, test_instance, requests_mock):
125174
start = "2024-11-10T10:00:00"

0 commit comments

Comments
 (0)