Skip to content

Commit 95715d0

Browse files
committed
docs: add testing section
1 parent 58d78ca commit 95715d0

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Diff for: docs/utilities/batch.md

+135
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,141 @@ def lambda_handler(event, context: LambdaContext):
10641064

10651065
```
10661066

1067+
## Testing your code
1068+
1069+
As there is no external calls, you can unit test your code with `BatchProcessor` quite easily.
1070+
1071+
**Example**: Given a SQS batch where the first batch record succeeds and the second fails processing, we should have a single item reported in the function response.
1072+
1073+
=== "test_app.py"
1074+
1075+
```python
1076+
import json
1077+
1078+
from pathlib import Path
1079+
from dataclasses import dataclass
1080+
1081+
import pytest
1082+
from src.app import lambda_handler, processor
1083+
1084+
1085+
def load_event(path: Path):
1086+
with path.open() as f:
1087+
return json.load(f)
1088+
1089+
1090+
@pytest.fixture
1091+
def lambda_context():
1092+
@dataclass
1093+
class LambdaContext:
1094+
function_name: str = "test"
1095+
memory_limit_in_mb: int = 128
1096+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
1097+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
1098+
1099+
return LambdaContext()
1100+
1101+
@pytest.fixture()
1102+
def sqs_event():
1103+
"""Generates API GW Event"""
1104+
return load_event(path=Path("events/sqs_event.json"))
1105+
1106+
1107+
def test_app_batch_partial_response(sqs_event, lambda_context):
1108+
# GIVEN
1109+
processor = app.processor # access processor for additional assertions
1110+
successful_record = sqs_event["Records"][0]
1111+
failed_record = sqs_event["Records"][1]
1112+
expected_response = {
1113+
"batchItemFailures: [
1114+
{
1115+
"itemIdentifier": failed_record["messageId"]
1116+
}
1117+
]
1118+
}
1119+
1120+
# WHEN
1121+
ret = app.lambda_handler(sqs_event, lambda_context)
1122+
1123+
# THEN
1124+
assert ret == expected_response
1125+
assert len(processor.fail_messages) == 1
1126+
assert processor.success_messages[0] == successful_record
1127+
```
1128+
1129+
=== "src/app.py"
1130+
1131+
```python
1132+
import json
1133+
1134+
from aws_lambda_powertools import Logger, Tracer
1135+
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
1136+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
1137+
from aws_lambda_powertools.utilities.typing import LambdaContext
1138+
1139+
1140+
processor = BatchProcessor(event_type=EventType.SQS)
1141+
tracer = Tracer()
1142+
logger = Logger()
1143+
1144+
1145+
@tracer.capture_method
1146+
def record_handler(record: SQSRecord):
1147+
payload: str = record.body
1148+
if payload:
1149+
item: dict = json.loads(payload)
1150+
...
1151+
1152+
@logger.inject_lambda_context
1153+
@tracer.capture_lambda_handler
1154+
@batch_processor(record_handler=record_handler, processor=processor)
1155+
def lambda_handler(event, context: LambdaContext):
1156+
return processor.response()
1157+
```
1158+
1159+
=== "Sample SQS event"
1160+
1161+
```json title="events/sqs_sample.json"
1162+
{
1163+
"Records": [
1164+
{
1165+
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
1166+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
1167+
"body": "{\"Message\": \"success\"}",
1168+
"attributes": {
1169+
"ApproximateReceiveCount": "1",
1170+
"SentTimestamp": "1545082649183",
1171+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
1172+
"ApproximateFirstReceiveTimestamp": "1545082649185"
1173+
},
1174+
"messageAttributes": {},
1175+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
1176+
"eventSource": "aws:sqs",
1177+
"eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
1178+
"awsRegion": "us-east-1"
1179+
},
1180+
{
1181+
"messageId": "244fc6b4-87a3-44ab-83d2-361172410c3a",
1182+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
1183+
"body": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
1184+
"attributes": {
1185+
"ApproximateReceiveCount": "1",
1186+
"SentTimestamp": "1545082649183",
1187+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
1188+
"ApproximateFirstReceiveTimestamp": "1545082649185"
1189+
},
1190+
"messageAttributes": {},
1191+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
1192+
"eventSource": "aws:sqs",
1193+
"eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
1194+
"awsRegion": "us-east-1"
1195+
}
1196+
]
1197+
}
1198+
```
1199+
1200+
1201+
10671202
## FAQ
10681203

10691204
### Choosing between decorator and context manager

0 commit comments

Comments
 (0)