|
1 | 1 | from typing import Callable
|
| 2 | +from unittest.mock import patch |
2 | 3 |
|
3 | 4 | import pytest
|
4 | 5 | from botocore.config import Config
|
5 | 6 | from botocore.stub import Stubber
|
6 | 7 |
|
7 |
| -from aws_lambda_powertools.utilities.batch import PartialSQSProcessor, batch_processor |
| 8 | +from aws_lambda_powertools.utilities.batch import PartialSQSProcessor, batch_processor, sqs_batch_processor |
8 | 9 |
|
9 | 10 |
|
10 | 11 | @pytest.fixture(scope="module")
|
@@ -46,6 +47,13 @@ def partial_processor(config) -> PartialSQSProcessor:
|
46 | 47 | return PartialSQSProcessor(config=config)
|
47 | 48 |
|
48 | 49 |
|
| 50 | +@pytest.fixture(scope="function") |
| 51 | +def stubbed_partial_processor(config) -> PartialSQSProcessor: |
| 52 | + processor = PartialSQSProcessor(config=config) |
| 53 | + with Stubber(processor.client) as stubber: |
| 54 | + yield stubber, processor |
| 55 | + |
| 56 | + |
49 | 57 | def test_partial_sqs_processor_context_with_failure(sqs_event_factory, record_handler, partial_processor):
|
50 | 58 | """
|
51 | 59 | Test processor with one failing record
|
@@ -131,6 +139,32 @@ def lambda_handler(event, context):
|
131 | 139 | assert result is True
|
132 | 140 |
|
133 | 141 |
|
| 142 | +@patch("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor") |
| 143 | +def test_sqs_batch_processor_middleware( |
| 144 | + patched_sqs_processor, sqs_event_factory, record_handler, stubbed_partial_processor |
| 145 | +): |
| 146 | + """ |
| 147 | + Test middleware's integration with PartialSQSProcessor |
| 148 | + """ |
| 149 | + |
| 150 | + @sqs_batch_processor(record_handler=record_handler) |
| 151 | + def lambda_handler(event, context): |
| 152 | + return True |
| 153 | + |
| 154 | + stubber, processor = stubbed_partial_processor |
| 155 | + patched_sqs_processor.return_value = processor |
| 156 | + |
| 157 | + fail_record = sqs_event_factory("fail") |
| 158 | + |
| 159 | + event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]} |
| 160 | + response = {"Successful": [{"Id": fail_record["messageId"]}], "Failed": []} |
| 161 | + stubber.add_response("delete_message_batch", response) |
| 162 | + result = lambda_handler(event, {}) |
| 163 | + stubber.assert_no_pending_responses() |
| 164 | + |
| 165 | + assert result is True |
| 166 | + |
| 167 | + |
134 | 168 | def test_batch_processor_middleware_with_custom_processor(capsys, sqs_event_factory, record_handler, config):
|
135 | 169 | """
|
136 | 170 | Test middlewares' integration with custom batch processor
|
|
0 commit comments