-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_utilities_batch.py
292 lines (208 loc) · 9.71 KB
/
test_utilities_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from typing import Callable
from unittest.mock import patch
import pytest
from botocore.config import Config
from botocore.stub import Stubber
from aws_lambda_powertools.utilities.batch import PartialSQSProcessor, batch_processor, sqs_batch_processor
from aws_lambda_powertools.utilities.batch.exceptions import SQSBatchProcessingError
@pytest.fixture(scope="module")
def sqs_event_factory() -> Callable:
def factory(body: str):
return {
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
"body": body,
"attributes": {},
"messageAttributes": {},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-1",
}
return factory
@pytest.fixture(scope="module")
def record_handler() -> Callable:
def handler(record):
body = record["body"]
if "fail" in body:
raise Exception("Failed to process record.")
return body
return handler
@pytest.fixture(scope="module")
def config() -> Config:
return Config(region_name="us-east-1")
@pytest.fixture(scope="function")
def partial_processor(config) -> PartialSQSProcessor:
return PartialSQSProcessor(config=config)
@pytest.fixture(scope="function")
def partial_processor_suppressed(config) -> PartialSQSProcessor:
return PartialSQSProcessor(config=config, suppress_exception=True)
@pytest.fixture(scope="function")
def stubbed_partial_processor(config) -> PartialSQSProcessor:
processor = PartialSQSProcessor(config=config)
with Stubber(processor.client) as stubber:
yield stubber, processor
@pytest.fixture(scope="function")
def stubbed_partial_processor_suppressed(config) -> PartialSQSProcessor:
processor = PartialSQSProcessor(config=config, suppress_exception=True)
with Stubber(processor.client) as stubber:
yield stubber, processor
def test_partial_sqs_processor_context_with_failure(sqs_event_factory, record_handler, partial_processor):
"""
Test processor with one failing record
"""
fail_record = sqs_event_factory("fail")
success_record = sqs_event_factory("success")
records = [fail_record, success_record]
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
with Stubber(partial_processor.client) as stubber:
stubber.add_response("delete_message_batch", response)
with pytest.raises(SQSBatchProcessingError) as error:
with partial_processor(records, record_handler) as ctx:
ctx.process()
assert len(error.value.child_exceptions) == 1
stubber.assert_no_pending_responses()
def test_partial_sqs_processor_context_only_success(sqs_event_factory, record_handler, partial_processor):
"""
Test processor without failure
"""
first_record = sqs_event_factory("success")
second_record = sqs_event_factory("success")
records = [first_record, second_record]
with partial_processor(records, record_handler) as ctx:
result = ctx.process()
assert result == [
("success", first_record["body"], first_record),
("success", second_record["body"], second_record),
]
def test_partial_sqs_processor_context_multiple_calls(sqs_event_factory, record_handler, partial_processor):
"""
Test processor without failure
"""
first_record = sqs_event_factory("success")
second_record = sqs_event_factory("success")
records = [first_record, second_record]
with partial_processor(records, record_handler) as ctx:
ctx.process()
with partial_processor([first_record], record_handler) as ctx:
ctx.process()
assert partial_processor.success_messages == [first_record]
def test_batch_processor_middleware_with_partial_sqs_processor(sqs_event_factory, record_handler, partial_processor):
"""
Test middleware's integration with PartialSQSProcessor
"""
@batch_processor(record_handler=record_handler, processor=partial_processor)
def lambda_handler(event, context):
return True
fail_record = sqs_event_factory("fail")
event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("fail"), sqs_event_factory("success")]}
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
with Stubber(partial_processor.client) as stubber:
stubber.add_response("delete_message_batch", response)
with pytest.raises(SQSBatchProcessingError) as error:
lambda_handler(event, {})
assert len(error.value.child_exceptions) == 2
stubber.assert_no_pending_responses()
@patch("aws_lambda_powertools.utilities.batch.sqs.PartialSQSProcessor")
def test_sqs_batch_processor_middleware(
patched_sqs_processor, sqs_event_factory, record_handler, stubbed_partial_processor
):
"""
Test middleware's integration with PartialSQSProcessor
"""
@sqs_batch_processor(record_handler=record_handler)
def lambda_handler(event, context):
return True
stubber, processor = stubbed_partial_processor
patched_sqs_processor.return_value = processor
fail_record = sqs_event_factory("fail")
event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]}
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
stubber.add_response("delete_message_batch", response)
with pytest.raises(SQSBatchProcessingError) as error:
lambda_handler(event, {})
assert len(error.value.child_exceptions) == 1
stubber.assert_no_pending_responses()
def test_batch_processor_middleware_with_custom_processor(capsys, sqs_event_factory, record_handler, config):
"""
Test middlewares' integration with custom batch processor
"""
class CustomProcessor(PartialSQSProcessor):
def failure_handler(self, record, exception):
print("Oh no ! It's a failure.")
return super().failure_handler(record, exception)
processor = CustomProcessor(config=config)
@batch_processor(record_handler=record_handler, processor=processor)
def lambda_handler(event, context):
return True
fail_record = sqs_event_factory("fail")
event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]}
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
with Stubber(processor.client) as stubber:
stubber.add_response("delete_message_batch", response)
with pytest.raises(SQSBatchProcessingError) as error:
lambda_handler(event, {})
stubber.assert_no_pending_responses()
assert len(error.value.child_exceptions) == 1
assert capsys.readouterr().out == "Oh no ! It's a failure.\n"
def test_batch_processor_middleware_suppressed_exceptions(
sqs_event_factory, record_handler, partial_processor_suppressed
):
"""
Test middleware's integration with PartialSQSProcessor
"""
@batch_processor(record_handler=record_handler, processor=partial_processor_suppressed)
def lambda_handler(event, context):
return True
fail_record = sqs_event_factory("fail")
event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("fail"), sqs_event_factory("success")]}
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
with Stubber(partial_processor_suppressed.client) as stubber:
stubber.add_response("delete_message_batch", response)
result = lambda_handler(event, {})
stubber.assert_no_pending_responses()
assert result is True
def test_partial_sqs_processor_suppressed_exceptions(sqs_event_factory, record_handler, partial_processor_suppressed):
"""
Test processor without failure
"""
first_record = sqs_event_factory("success")
second_record = sqs_event_factory("fail")
records = [first_record, second_record]
fail_record = sqs_event_factory("fail")
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
with Stubber(partial_processor_suppressed.client) as stubber:
stubber.add_response("delete_message_batch", response)
with partial_processor_suppressed(records, record_handler) as ctx:
ctx.process()
assert partial_processor_suppressed.success_messages == [first_record]
@patch("aws_lambda_powertools.utilities.batch.sqs.PartialSQSProcessor")
def test_sqs_batch_processor_middleware_suppressed_exception(
patched_sqs_processor, sqs_event_factory, record_handler, stubbed_partial_processor_suppressed
):
"""
Test middleware's integration with PartialSQSProcessor
"""
@sqs_batch_processor(record_handler=record_handler)
def lambda_handler(event, context):
return True
stubber, processor = stubbed_partial_processor_suppressed
patched_sqs_processor.return_value = processor
fail_record = sqs_event_factory("fail")
event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]}
response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []}
stubber.add_response("delete_message_batch", response)
result = lambda_handler(event, {})
stubber.assert_no_pending_responses()
assert result is True
def test_partial_sqs_processor_context_only_failure(sqs_event_factory, record_handler, partial_processor):
"""
Test processor with only failures
"""
first_record = sqs_event_factory("fail")
second_record = sqs_event_factory("fail")
records = [first_record, second_record]
with pytest.raises(SQSBatchProcessingError) as error:
with partial_processor(records, record_handler) as ctx:
ctx.process()
assert len(error.value.child_exceptions) == 2