-
Notifications
You must be signed in to change notification settings - Fork 433
feat(parser): add support for SQS-wrapped S3 event notifications #2108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
502ce6d
72b127d
a13e190
665a99d
6c426dd
be69e4c
ad2ba2c
70e7b1c
251d8ba
1116552
598b90d
31eaa2e
f118583
26cf2b7
9c759d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import List | ||
|
||
from pydantic import Json | ||
|
||
from aws_lambda_powertools.utilities.parser.models.s3 import S3Model | ||
from aws_lambda_powertools.utilities.parser.models.sqs import SqsModel, SqsRecordModel | ||
|
||
|
||
class SqsS3EventNotificationRecordModel(SqsRecordModel): | ||
body: Json[S3Model] | ||
|
||
|
||
class SqsS3EventNotificationModel(SqsModel): | ||
Records: List[SqsS3EventNotificationRecordModel] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
|
||
from aws_lambda_powertools.utilities.parser import ValidationError, event_parser | ||
from aws_lambda_powertools.utilities.parser.models import SqsS3EventNotificationModel | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from tests.functional.parser.test_s3 import assert_s3 | ||
from tests.functional.utils import json_serialize, load_event | ||
|
||
|
||
@event_parser(model=SqsS3EventNotificationModel) | ||
def handle_sqs_json_body_containing_s3_notifications(event: SqsS3EventNotificationModel, _: LambdaContext): | ||
return event | ||
|
||
|
||
def test_handle_sqs_json_body_containing_s3_notifications(): | ||
sqs_event_dict = load_event("sqsEvent.json") | ||
s3_event_notification_dict = load_event("s3Event.json") | ||
for record in sqs_event_dict["Records"]: | ||
record["body"] = json_serialize(s3_event_notification_dict) | ||
|
||
parsed_event: SqsS3EventNotificationModel = handle_sqs_json_body_containing_s3_notifications( | ||
sqs_event_dict, LambdaContext() | ||
) | ||
|
||
assert len(parsed_event.Records) == 2 | ||
for parsed_sqs_record in parsed_event.Records: | ||
assert_s3(parsed_sqs_record.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like an opinion here @rubenfonseca @heitorlessa. I think this test is ok, do you see another way to optimize this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks ok to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good opportunity to move to the new unit tests since all we should care here is whether the new Model can be instantiated correctly (along with its ramifications, JSON deserialization etc.). Following this, all you'd need to do here is:
Previously, we were shoving everything under Hope that makes sense ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes perfect sense @heitorlessa! I changed the files as per this feedback |
||
|
||
|
||
def test_handle_sqs_body_invalid_json(): | ||
sqs_event_dict = load_event("sqsEvent.json") | ||
|
||
with pytest.raises(ValidationError): | ||
handle_sqs_json_body_containing_s3_notifications(sqs_event_dict, LambdaContext()) | ||
|
||
|
||
def test_handle_sqs_json_body_containing_arbitrary_json(): | ||
sqs_event_dict = load_event("sqsEvent.json") | ||
for record in sqs_event_dict["Records"]: | ||
record["body"] = json_serialize({"foo": "bar"}) | ||
|
||
with pytest.raises(ValidationError): | ||
handle_sqs_json_body_containing_s3_notifications(sqs_event_dict, LambdaContext()) |
Uh oh!
There was an error while loading. Please reload this page.