6
6
from botocore .stub import Stubber
7
7
8
8
from aws_lambda_powertools .utilities .batch import PartialSQSProcessor , batch_processor , sqs_batch_processor
9
+ from aws_lambda_powertools .utilities .batch .exceptions import SQSBatchProcessingError
9
10
10
11
11
12
@pytest .fixture (scope = "module" )
@@ -47,13 +48,25 @@ def partial_processor(config) -> PartialSQSProcessor:
47
48
return PartialSQSProcessor (config = config )
48
49
49
50
51
+ @pytest .fixture (scope = "function" )
52
+ def partial_processor_suppressed (config ) -> PartialSQSProcessor :
53
+ return PartialSQSProcessor (config = config , suppress_exception = True )
54
+
55
+
50
56
@pytest .fixture (scope = "function" )
51
57
def stubbed_partial_processor (config ) -> PartialSQSProcessor :
52
58
processor = PartialSQSProcessor (config = config )
53
59
with Stubber (processor .client ) as stubber :
54
60
yield stubber , processor
55
61
56
62
63
+ @pytest .fixture (scope = "function" )
64
+ def stubbed_partial_processor_suppressed (config ) -> PartialSQSProcessor :
65
+ processor = PartialSQSProcessor (config = config , suppress_exception = True )
66
+ with Stubber (processor .client ) as stubber :
67
+ yield stubber , processor
68
+
69
+
57
70
def test_partial_sqs_processor_context_with_failure (sqs_event_factory , record_handler , partial_processor ):
58
71
"""
59
72
Test processor with one failing record
@@ -68,16 +81,13 @@ def test_partial_sqs_processor_context_with_failure(sqs_event_factory, record_ha
68
81
with Stubber (partial_processor .client ) as stubber :
69
82
stubber .add_response ("delete_message_batch" , response )
70
83
71
- with partial_processor (records , record_handler ) as ctx :
72
- result = ctx .process ()
84
+ with pytest .raises (SQSBatchProcessingError ) as error :
85
+ with partial_processor (records , record_handler ) as ctx :
86
+ ctx .process ()
73
87
88
+ assert len (error .value .args [0 ]) == 1
74
89
stubber .assert_no_pending_responses ()
75
90
76
- assert result == [
77
- ("fail" , ("Failed to process record." ,), fail_record ),
78
- ("success" , success_record ["body" ], success_record ),
79
- ]
80
-
81
91
82
92
def test_partial_sqs_processor_context_only_success (sqs_event_factory , record_handler , partial_processor ):
83
93
"""
@@ -126,18 +136,17 @@ def lambda_handler(event, context):
126
136
127
137
fail_record = sqs_event_factory ("fail" )
128
138
129
- event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("success" )]}
139
+ event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("fail" ), sqs_event_factory ( " success" )]}
130
140
response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
131
141
132
142
with Stubber (partial_processor .client ) as stubber :
133
143
stubber .add_response ("delete_message_batch" , response )
144
+ with pytest .raises (SQSBatchProcessingError ) as error :
145
+ lambda_handler (event , {})
134
146
135
- result = lambda_handler (event , {})
136
-
147
+ assert len (error .value .args [0 ]) == 2
137
148
stubber .assert_no_pending_responses ()
138
149
139
- assert result is True
140
-
141
150
142
151
@patch ("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor" )
143
152
def test_sqs_batch_processor_middleware (
@@ -159,10 +168,11 @@ def lambda_handler(event, context):
159
168
event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("success" )]}
160
169
response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
161
170
stubber .add_response ("delete_message_batch" , response )
162
- result = lambda_handler ( event , {})
163
- stubber . assert_no_pending_responses ( )
171
+ with pytest . raises ( SQSBatchProcessingError ) as error :
172
+ lambda_handler ( event , {} )
164
173
165
- assert result is True
174
+ assert len (error .value .args [0 ]) == 1
175
+ stubber .assert_no_pending_responses ()
166
176
167
177
168
178
def test_batch_processor_middleware_with_custom_processor (capsys , sqs_event_factory , record_handler , config ):
@@ -188,10 +198,80 @@ def lambda_handler(event, context):
188
198
189
199
with Stubber (processor .client ) as stubber :
190
200
stubber .add_response ("delete_message_batch" , response )
201
+ with pytest .raises (SQSBatchProcessingError ) as error :
202
+ lambda_handler (event , {})
203
+
204
+ stubber .assert_no_pending_responses ()
205
+
206
+ assert len (error .value .args [0 ]) == 1
207
+ assert capsys .readouterr ().out == "Oh no ! It's a failure.\n "
208
+
209
+
210
+ def test_batch_processor_middleware_suppressed_exceptions (
211
+ sqs_event_factory , record_handler , partial_processor_suppressed
212
+ ):
213
+ """
214
+ Test middleware's integration with PartialSQSProcessor
215
+ """
216
+
217
+ @batch_processor (record_handler = record_handler , processor = partial_processor_suppressed )
218
+ def lambda_handler (event , context ):
219
+ return True
220
+
221
+ fail_record = sqs_event_factory ("fail" )
222
+
223
+ event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("fail" ), sqs_event_factory ("success" )]}
224
+ response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
191
225
226
+ with Stubber (partial_processor_suppressed .client ) as stubber :
227
+ stubber .add_response ("delete_message_batch" , response )
192
228
result = lambda_handler (event , {})
193
229
194
230
stubber .assert_no_pending_responses ()
231
+ assert result is True
232
+
233
+
234
+ def test_partial_sqs_processor_suppressed_exceptions (sqs_event_factory , record_handler , partial_processor_suppressed ):
235
+ """
236
+ Test processor without failure
237
+ """
195
238
239
+ first_record = sqs_event_factory ("success" )
240
+ second_record = sqs_event_factory ("fail" )
241
+ records = [first_record , second_record ]
242
+
243
+ fail_record = sqs_event_factory ("fail" )
244
+ response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
245
+
246
+ with Stubber (partial_processor_suppressed .client ) as stubber :
247
+ stubber .add_response ("delete_message_batch" , response )
248
+ with partial_processor_suppressed (records , record_handler ) as ctx :
249
+ ctx .process ()
250
+
251
+ assert partial_processor_suppressed .success_messages == [first_record ]
252
+
253
+
254
+ @patch ("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor" )
255
+ def test_sqs_batch_processor_middleware_suppressed_exception (
256
+ patched_sqs_processor , sqs_event_factory , record_handler , stubbed_partial_processor_suppressed
257
+ ):
258
+ """
259
+ Test middleware's integration with PartialSQSProcessor
260
+ """
261
+
262
+ @sqs_batch_processor (record_handler = record_handler )
263
+ def lambda_handler (event , context ):
264
+ return True
265
+
266
+ stubber , processor = stubbed_partial_processor_suppressed
267
+ patched_sqs_processor .return_value = processor
268
+
269
+ fail_record = sqs_event_factory ("fail" )
270
+
271
+ event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("success" )]}
272
+ response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
273
+ stubber .add_response ("delete_message_batch" , response )
274
+ result = lambda_handler (event , {})
275
+
276
+ stubber .assert_no_pending_responses ()
196
277
assert result is True
197
- assert capsys .readouterr ().out == "Oh no ! It's a failure.\n "
0 commit comments