|
15 | 15 | batch_processor,
|
16 | 16 | process_partial_response,
|
17 | 17 | )
|
18 |
| -from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError |
| 18 | +from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError, UnexpectedBatchTypeError |
19 | 19 | from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
|
20 | 20 | DynamoDBRecord,
|
21 | 21 | )
|
@@ -708,3 +708,69 @@ def test_async_process_partial_response_invalid_input(async_record_handler: Call
|
708 | 708 | # WHEN/THEN
|
709 | 709 | with pytest.raises(ValueError):
|
710 | 710 | async_process_partial_response(batch, record_handler, processor)
|
| 711 | + |
| 712 | + |
| 713 | +@pytest.mark.parametrize( |
| 714 | + "event", |
| 715 | + [ |
| 716 | + {}, |
| 717 | + {"Records": None}, |
| 718 | + {"Records": "not a list"}, |
| 719 | + ], |
| 720 | +) |
| 721 | +def test_process_partial_response_raises_unexpected_batch_type(event, record_handler): |
| 722 | + # GIVEN a batch processor configured for SQS events |
| 723 | + processor = BatchProcessor(event_type=EventType.SQS) |
| 724 | + |
| 725 | + # WHEN processing an event with invalid Records |
| 726 | + with pytest.raises(UnexpectedBatchTypeError) as exc_info: |
| 727 | + process_partial_response( |
| 728 | + event=event, |
| 729 | + record_handler=record_handler, |
| 730 | + processor=processor, |
| 731 | + ) |
| 732 | + |
| 733 | + # THEN the correct error message is raised |
| 734 | + assert "Unexpected batch type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(exc_info.value) |
| 735 | + |
| 736 | + |
| 737 | +@pytest.mark.asyncio |
| 738 | +@pytest.mark.parametrize( |
| 739 | + "event", |
| 740 | + [ |
| 741 | + {}, |
| 742 | + {"Records": None}, |
| 743 | + {"Records": "not a list"}, |
| 744 | + ], |
| 745 | +) |
| 746 | +async def test_async_process_partial_response_raises_unexpected_batch_type(event, async_record_handler): |
| 747 | + # GIVEN a batch processor configured for SQS events |
| 748 | + processor = BatchProcessor(event_type=EventType.SQS) |
| 749 | + |
| 750 | + # WHEN processing an event with invalid Records asynchronously |
| 751 | + with pytest.raises(UnexpectedBatchTypeError) as exc_info: |
| 752 | + await async_process_partial_response( |
| 753 | + event=event, |
| 754 | + record_handler=async_record_handler, |
| 755 | + processor=processor, |
| 756 | + ) |
| 757 | + |
| 758 | + # THEN the correct error message is raised |
| 759 | + assert "Unexpected batch type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(exc_info.value) |
| 760 | + |
| 761 | + |
| 762 | +def test_process_partial_response_should_not_raise_unexpected_batch_type(record_handler): |
| 763 | + # GIVEN a valid SQS event structure |
| 764 | + event = {"Records": [{"messageId": "1", "body": "test"}]} |
| 765 | + processor = BatchProcessor(event_type=EventType.SQS) |
| 766 | + |
| 767 | + # WHEN processing the event |
| 768 | + try: |
| 769 | + process_partial_response( |
| 770 | + event=event, |
| 771 | + record_handler=record_handler, |
| 772 | + processor=processor, |
| 773 | + ) |
| 774 | + except UnexpectedBatchTypeError: |
| 775 | + # THEN no UnexpectedBatchTypeError should be raised |
| 776 | + pytest.fail("UnexpectedBatchTypeError was raised with a valid event structure!") |
0 commit comments