|
| 1 | +from typing import Callable |
| 2 | +import pytest |
| 3 | + |
| 4 | +from botocore.stub import Stubber |
| 5 | + |
| 6 | +from aws_lambda_powertools.utilities.batch import PartialSQSProcessor, partial_sqs_processor |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def sqs_event_factory() -> Callable: |
| 11 | + def factory(body: str): |
| 12 | + return { |
| 13 | + "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", |
| 14 | + "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a", |
| 15 | + "body": body, |
| 16 | + "attributes": {}, |
| 17 | + "messageAttributes": {}, |
| 18 | + "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", |
| 19 | + "eventSource": "aws:sqs", |
| 20 | + "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", |
| 21 | + "awsRegion": "us-east-1", |
| 22 | + } |
| 23 | + |
| 24 | + return factory |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def record_handler() -> Callable: |
| 29 | + def handler(record): |
| 30 | + body = record["body"] |
| 31 | + if "fail" in body: |
| 32 | + raise Exception("Failed to process record.") |
| 33 | + return body |
| 34 | + |
| 35 | + return handler |
| 36 | + |
| 37 | + |
| 38 | +def test_partial_sqs_processor_context_with_failure(sqs_event_factory, record_handler): |
| 39 | + """ Test processor with one record failing """ |
| 40 | + processor = PartialSQSProcessor() |
| 41 | + |
| 42 | + fail_record = sqs_event_factory("fail") |
| 43 | + success_record = sqs_event_factory("success") |
| 44 | + |
| 45 | + records = [fail_record, success_record] |
| 46 | + |
| 47 | + response = {"Successful": [{"Id": fail_record["messageId"]},], "Failed": []} |
| 48 | + |
| 49 | + with Stubber(processor.client) as stubber: |
| 50 | + stubber.add_response("delete_message_batch", response) |
| 51 | + |
| 52 | + with processor(records, record_handler) as ctx: |
| 53 | + result = ctx.process() |
| 54 | + |
| 55 | + stubber.assert_no_pending_responses() |
| 56 | + |
| 57 | + assert result == [ |
| 58 | + ("fail", ("Failed to process record.",), fail_record), |
| 59 | + ("success", success_record["body"], success_record), |
| 60 | + ] |
| 61 | + |
| 62 | + |
| 63 | +def test_partial_sqs_processor_context_only_success(sqs_event_factory, record_handler): |
| 64 | + """ Test processor without failure """ |
| 65 | + processor = PartialSQSProcessor() |
| 66 | + |
| 67 | + first_record = sqs_event_factory("success") |
| 68 | + second_record = sqs_event_factory("success") |
| 69 | + |
| 70 | + records = [first_record, second_record] |
| 71 | + |
| 72 | + with processor(records, record_handler) as ctx: |
| 73 | + result = ctx.process() |
| 74 | + |
| 75 | + assert result == [ |
| 76 | + ("success", first_record["body"], first_record), |
| 77 | + ("success", second_record["body"], second_record), |
| 78 | + ] |
| 79 | + |
| 80 | + |
| 81 | +def test_partial_sqs_processor_context_multiple_calls(sqs_event_factory, record_handler): |
| 82 | + """ Test processor without failure """ |
| 83 | + processor = PartialSQSProcessor() |
| 84 | + |
| 85 | + first_record = sqs_event_factory("success") |
| 86 | + second_record = sqs_event_factory("success") |
| 87 | + |
| 88 | + records = [first_record, second_record] |
| 89 | + |
| 90 | + with processor(records, record_handler) as ctx: |
| 91 | + ctx.process() |
| 92 | + |
| 93 | + with processor([first_record], record_handler) as ctx: |
| 94 | + ctx.process() |
| 95 | + |
| 96 | + assert processor.success_messages == [first_record] |
| 97 | + |
| 98 | + |
| 99 | +def test_partial_sqs_processor_middleware_with_default(sqs_event_factory, record_handler): |
| 100 | + """ Test middleware with default partial processor """ |
| 101 | + processor = PartialSQSProcessor() |
| 102 | + |
| 103 | + @partial_sqs_processor(record_handler=record_handler, processor=processor) |
| 104 | + def lambda_handler(event, context): |
| 105 | + return True |
| 106 | + |
| 107 | + fail_record = sqs_event_factory("fail") |
| 108 | + |
| 109 | + event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]} |
| 110 | + response = {"Successful": [{"Id": fail_record["messageId"]},], "Failed": []} |
| 111 | + |
| 112 | + with Stubber(processor.client) as stubber: |
| 113 | + stubber.add_response("delete_message_batch", response) |
| 114 | + |
| 115 | + result = lambda_handler(event, {}) |
| 116 | + |
| 117 | + stubber.assert_no_pending_responses() |
| 118 | + |
| 119 | + assert result is True |
| 120 | + |
| 121 | + |
| 122 | +def test_partial_sqs_processor_middleware_with_custom(capsys, sqs_event_factory, record_handler): |
| 123 | + """ Test middle with custom partial processor """ |
| 124 | + class CustomProcessor(PartialSQSProcessor): |
| 125 | + def failure_handler(self, record, exception): |
| 126 | + print("Oh no ! It's a failure.") |
| 127 | + return super().failure_handler(record, exception) |
| 128 | + |
| 129 | + processor = CustomProcessor() |
| 130 | + |
| 131 | + @partial_sqs_processor(record_handler=record_handler, processor=processor) |
| 132 | + def lambda_handler(event, context): |
| 133 | + return True |
| 134 | + |
| 135 | + fail_record = sqs_event_factory("fail") |
| 136 | + |
| 137 | + event = {"Records": [sqs_event_factory("fail"), sqs_event_factory("success")]} |
| 138 | + response = {"Successful": [{"Id": fail_record["messageId"]},], "Failed": []} |
| 139 | + |
| 140 | + with Stubber(processor.client) as stubber: |
| 141 | + stubber.add_response("delete_message_batch", response) |
| 142 | + |
| 143 | + result = lambda_handler(event, {}) |
| 144 | + |
| 145 | + stubber.assert_no_pending_responses() |
| 146 | + |
| 147 | + assert result is True |
| 148 | + assert capsys.readouterr().out == "Oh no ! It's a failure.\n" |
0 commit comments