66from botocore .stub import Stubber
77
88from aws_lambda_powertools .utilities .batch import PartialSQSProcessor , batch_processor , sqs_batch_processor
9+ from aws_lambda_powertools .utilities .batch .exceptions import SQSBatchProcessingError
910
1011
1112@pytest .fixture (scope = "module" )
@@ -47,13 +48,25 @@ def partial_processor(config) -> PartialSQSProcessor:
4748 return PartialSQSProcessor (config = config )
4849
4950
51+ @pytest .fixture (scope = "function" )
52+ def partial_processor_suppressed (config ) -> PartialSQSProcessor :
53+ return PartialSQSProcessor (config = config , suppress_exception = True )
54+
55+
5056@pytest .fixture (scope = "function" )
5157def stubbed_partial_processor (config ) -> PartialSQSProcessor :
5258 processor = PartialSQSProcessor (config = config )
5359 with Stubber (processor .client ) as stubber :
5460 yield stubber , processor
5561
5662
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+
5770def test_partial_sqs_processor_context_with_failure (sqs_event_factory , record_handler , partial_processor ):
5871 """
5972 Test processor with one failing record
@@ -68,16 +81,13 @@ def test_partial_sqs_processor_context_with_failure(sqs_event_factory, record_ha
6881 with Stubber (partial_processor .client ) as stubber :
6982 stubber .add_response ("delete_message_batch" , response )
7083
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 ()
7387
88+ assert len (error .value .args [0 ]) == 1
7489 stubber .assert_no_pending_responses ()
7590
76- assert result == [
77- ("fail" , ("Failed to process record." ,), fail_record ),
78- ("success" , success_record ["body" ], success_record ),
79- ]
80-
8191
8292def test_partial_sqs_processor_context_only_success (sqs_event_factory , record_handler , partial_processor ):
8393 """
@@ -126,18 +136,17 @@ def lambda_handler(event, context):
126136
127137 fail_record = sqs_event_factory ("fail" )
128138
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" )]}
130140 response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
131141
132142 with Stubber (partial_processor .client ) as stubber :
133143 stubber .add_response ("delete_message_batch" , response )
144+ with pytest .raises (SQSBatchProcessingError ) as error :
145+ lambda_handler (event , {})
134146
135- result = lambda_handler (event , {})
136-
147+ assert len (error .value .args [0 ]) == 2
137148 stubber .assert_no_pending_responses ()
138149
139- assert result is True
140-
141150
142151@patch ("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor" )
143152def test_sqs_batch_processor_middleware (
@@ -159,10 +168,11 @@ def lambda_handler(event, context):
159168 event = {"Records" : [sqs_event_factory ("fail" ), sqs_event_factory ("success" )]}
160169 response = {"Successful" : [{"Id" : fail_record ["messageId" ]}], "Failed" : []}
161170 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 , {} )
164173
165- assert result is True
174+ assert len (error .value .args [0 ]) == 1
175+ stubber .assert_no_pending_responses ()
166176
167177
168178def test_batch_processor_middleware_with_custom_processor (capsys , sqs_event_factory , record_handler , config ):
@@ -188,10 +198,80 @@ def lambda_handler(event, context):
188198
189199 with Stubber (processor .client ) as stubber :
190200 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" : []}
191225
226+ with Stubber (partial_processor_suppressed .client ) as stubber :
227+ stubber .add_response ("delete_message_batch" , response )
192228 result = lambda_handler (event , {})
193229
194230 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+ """
195238
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 ()
196277 assert result is True
197- assert capsys .readouterr ().out == "Oh no ! It's a failure.\n "
0 commit comments