Skip to content

Commit 4b27f14

Browse files
committed
Update log-event tests for CustomStorage migration
Align test mocks with the APIHarnessV2-to-CustomStorage migration in main.py: patch CustomStorage instead of APIHarnessV2, mock individual PutObject/SearchObjects methods instead of .command() side_effect, and verify ext_headers construction instead of per-request headers kwargs.
1 parent d53e5e5 commit 4b27f14

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

functions/log-event/test_main.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,37 @@ def setUp(self):
2929

3030
importlib.reload(main)
3131

32-
@patch('main.APIHarnessV2')
32+
@patch('main.CustomStorage')
3333
@patch('main.uuid.uuid4')
3434
@patch('main.time.time')
35-
def test_on_post_success(self, mock_time, mock_uuid, mock_api_harness_class):
35+
def test_on_post_success(self, mock_time, mock_uuid, mock_custom_storage_class):
3636
"""Test successful POST request with valid event_data in body."""
3737
# Mock dependencies
3838
mock_uuid.return_value = MagicMock()
3939
mock_uuid.return_value.__str__ = MagicMock(return_value="test-event-id-123")
4040
mock_time.return_value = 1690123456
4141

42-
# Mock APIHarnessV2 instance
42+
# Mock CustomStorage instance
4343
mock_api_instance = MagicMock()
44-
mock_api_harness_class.return_value = mock_api_instance
44+
mock_custom_storage_class.return_value = mock_api_instance
4545

4646
# Mock successful PutObject response
47-
mock_api_instance.command.side_effect = [
48-
{ # PutObject response
49-
"status_code": 200,
50-
"body": {"success": True}
51-
},
52-
{ # SearchObjects response
53-
"status_code": 200,
54-
"body": {
55-
"resources": [{
56-
"event_id": "test-event-id-123",
57-
"data": {"test": "data"},
58-
"timestamp": 1690123456
59-
}]
60-
}
47+
mock_api_instance.PutObject.return_value = {
48+
"status_code": 200,
49+
"body": {"success": True}
50+
}
51+
52+
# Mock successful SearchObjects response
53+
mock_api_instance.SearchObjects.return_value = {
54+
"status_code": 200,
55+
"body": {
56+
"resources": [{
57+
"event_id": "test-event-id-123",
58+
"data": {"test": "data"},
59+
"timestamp": 1690123456
60+
}]
6161
}
62-
]
62+
}
6363

6464
request = Request()
6565
request.body = {
@@ -73,21 +73,18 @@ def test_on_post_success(self, mock_time, mock_uuid, mock_api_harness_class):
7373
self.assertIn("metadata", response.body)
7474
self.assertEqual(len(response.body["metadata"]), 1)
7575

76-
# Verify API calls
77-
self.assertEqual(mock_api_instance.command.call_count, 2)
78-
7976
# Verify PutObject call
80-
put_call = mock_api_instance.command.call_args_list[0]
81-
self.assertEqual(put_call[0][0], "PutObject")
77+
mock_api_instance.PutObject.assert_called_once()
78+
put_call = mock_api_instance.PutObject.call_args
8279
self.assertEqual(put_call[1]["collection_name"], "event_logs")
8380
self.assertEqual(put_call[1]["object_key"], "test-event-id-123")
8481
self.assertEqual(put_call[1]["body"]["event_id"], "test-event-id-123")
8582
self.assertEqual(put_call[1]["body"]["data"], {"test": "data", "message": "test event"})
8683
self.assertEqual(put_call[1]["body"]["timestamp"], 1690123456)
8784

8885
# Verify SearchObjects call
89-
search_call = mock_api_instance.command.call_args_list[1]
90-
self.assertEqual(search_call[0][0], "SearchObjects")
86+
mock_api_instance.SearchObjects.assert_called_once()
87+
search_call = mock_api_instance.SearchObjects.call_args
9188
self.assertEqual(search_call[1]["filter"], "event_id:'test-event-id-123'")
9289
self.assertEqual(search_call[1]["collection_name"], "event_logs")
9390

@@ -101,20 +98,20 @@ def test_on_post_missing_event_data(self):
10198
self.assertEqual(len(response.errors), 1)
10299
self.assertEqual(response.errors[0].message, "missing event_data")
103100

104-
@patch('main.APIHarnessV2')
101+
@patch('main.CustomStorage')
105102
@patch('main.uuid.uuid4')
106103
@patch('main.time.time')
107-
def test_on_post_put_object_error(self, mock_time, mock_uuid, mock_api_harness_class):
104+
def test_on_post_put_object_error(self, mock_time, mock_uuid, mock_custom_storage_class):
108105
"""Test POST request when PutObject API returns an error."""
109106
# Mock dependencies
110107
mock_uuid.return_value = MagicMock()
111108
mock_uuid.return_value.__str__ = MagicMock(return_value="test-event-id-123")
112109
mock_time.return_value = 1690123456
113110

114-
# Mock APIHarnessV2 instance with error response
111+
# Mock CustomStorage instance with error response
115112
mock_api_instance = MagicMock()
116-
mock_api_harness_class.return_value = mock_api_instance
117-
mock_api_instance.command.return_value = {
113+
mock_custom_storage_class.return_value = mock_api_instance
114+
mock_api_instance.PutObject.return_value = {
118115
"status_code": 500,
119116
"error": {"message": "Internal server error"}
120117
}
@@ -130,18 +127,18 @@ def test_on_post_put_object_error(self, mock_time, mock_uuid, mock_api_harness_c
130127
self.assertEqual(len(response.errors), 1)
131128
self.assertIn("Failed to store event: Internal server error", response.errors[0].message)
132129

133-
@patch('main.APIHarnessV2')
130+
@patch('main.CustomStorage')
134131
@patch('main.uuid.uuid4')
135132
@patch('main.time.time')
136-
def test_on_post_exception_handling(self, mock_time, mock_uuid, mock_api_harness_class):
133+
def test_on_post_exception_handling(self, mock_time, mock_uuid, mock_custom_storage_class):
137134
"""Test POST request when an exception is raised."""
138135
# Mock dependencies
139136
mock_uuid.return_value = MagicMock()
140137
mock_uuid.return_value.__str__ = MagicMock(return_value="test-event-id-123")
141138
mock_time.return_value = 1690123456
142139

143-
# Mock APIHarnessV2 to raise an exception
144-
mock_api_harness_class.side_effect = ConnectionError("Connection failed")
140+
# Mock CustomStorage to raise an exception
141+
mock_custom_storage_class.side_effect = ConnectionError("Connection failed")
145142

146143
request = Request()
147144
request.body = {
@@ -155,23 +152,27 @@ def test_on_post_exception_handling(self, mock_time, mock_uuid, mock_api_harness
155152
self.assertIn("Error saving collection: Connection failed", response.errors[0].message)
156153

157154
@patch.dict('main.os.environ', {'APP_ID': 'test-app-123'})
158-
@patch('main.APIHarnessV2')
155+
@patch('main.CustomStorage')
159156
@patch('main.uuid.uuid4')
160157
@patch('main.time.time')
161-
def test_on_post_with_app_id_header(self, mock_time, mock_uuid, mock_api_harness_class):
158+
def test_on_post_with_app_id_header(self, mock_time, mock_uuid, mock_custom_storage_class):
162159
"""Test POST request with APP_ID environment variable set."""
163160
# Mock dependencies
164161
mock_uuid.return_value = MagicMock()
165162
mock_uuid.return_value.__str__ = MagicMock(return_value="test-event-id-123")
166163
mock_time.return_value = 1690123456
167164

168-
# Mock APIHarnessV2 instance
165+
# Mock CustomStorage instance
169166
mock_api_instance = MagicMock()
170-
mock_api_harness_class.return_value = mock_api_instance
171-
mock_api_instance.command.side_effect = [
172-
{"status_code": 200, "body": {"success": True}},
173-
{"status_code": 200, "body": {"resources": []}}
174-
]
167+
mock_custom_storage_class.return_value = mock_api_instance
168+
mock_api_instance.PutObject.return_value = {
169+
"status_code": 200,
170+
"body": {"success": True}
171+
}
172+
mock_api_instance.SearchObjects.return_value = {
173+
"status_code": 200,
174+
"body": {"resources": []}
175+
}
175176

176177
request = Request()
177178
request.body = {
@@ -182,9 +183,10 @@ def test_on_post_with_app_id_header(self, mock_time, mock_uuid, mock_api_harness
182183

183184
self.assertEqual(response.code, 200)
184185

185-
# Verify that headers with APP_ID were passed to both API calls
186-
for call in mock_api_instance.command.call_args_list:
187-
self.assertEqual(call[1]["headers"], {"X-CS-APP-ID": "test-app-123"})
186+
# Verify that CustomStorage was constructed with ext_headers containing APP_ID
187+
mock_custom_storage_class.assert_called_once_with(
188+
ext_headers={"X-CS-APP-ID": "test-app-123"}
189+
)
188190

189191

190192
if __name__ == "__main__":

0 commit comments

Comments
 (0)