@@ -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