Skip to content

Commit 1d5ede4

Browse files
committed
chore: refactored testing
1 parent b271fb6 commit 1d5ede4

File tree

6 files changed

+145
-139
lines changed

6 files changed

+145
-139
lines changed

docs/utilities/batch.md

Lines changed: 6 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -339,33 +339,8 @@ When using Tracer to capture responses for each batch record processing, you mig
339339

340340
If that's the case, you can configure [Tracer to disable response auto-capturing](../core/tracer.md#disabling-response-auto-capture){target="_blank"}.
341341

342-
```python hl_lines="14" title="Disabling Tracer response auto-capturing"
343-
import json
344-
345-
from aws_lambda_powertools import Logger, Tracer
346-
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
347-
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
348-
from aws_lambda_powertools.utilities.typing import LambdaContext
349-
350-
351-
processor = BatchProcessor(event_type=EventType.SQS)
352-
tracer = Tracer()
353-
logger = Logger()
354-
355-
356-
@tracer.capture_method(capture_response=False)
357-
def record_handler(record: SQSRecord):
358-
payload: str = record.body
359-
if payload:
360-
item: dict = json.loads(payload)
361-
...
362-
363-
@logger.inject_lambda_context
364-
@tracer.capture_lambda_handler
365-
@batch_processor(record_handler=record_handler, processor=processor)
366-
def lambda_handler(event, context: LambdaContext):
367-
return processor.response()
368-
342+
```python hl_lines="17" title="Disabling Tracer response auto-capturing"
343+
--8<-- "examples/batch_processing/src/disable_tracing.py"
369344
```
370345

371346
## Testing your code
@@ -379,127 +354,19 @@ Given a SQS batch where the first batch record succeeds and the second fails pro
379354
=== "test_app.py"
380355

381356
```python
382-
import json
383-
384-
from pathlib import Path
385-
from dataclasses import dataclass
386-
387-
import pytest
388-
from src.app import lambda_handler, processor
389-
390-
391-
def load_event(path: Path):
392-
with path.open() as f:
393-
return json.load(f)
394-
395-
396-
@pytest.fixture
397-
def lambda_context():
398-
@dataclass
399-
class LambdaContext:
400-
function_name: str = "test"
401-
memory_limit_in_mb: int = 128
402-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
403-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
404-
405-
return LambdaContext()
406-
407-
@pytest.fixture()
408-
def sqs_event():
409-
"""Generates API GW Event"""
410-
return load_event(path=Path("events/sqs_event.json"))
411-
412-
413-
def test_app_batch_partial_response(sqs_event, lambda_context):
414-
# GIVEN
415-
processor = app.processor # access processor for additional assertions
416-
successful_record = sqs_event["Records"][0]
417-
failed_record = sqs_event["Records"][1]
418-
expected_response = {
419-
"batchItemFailures: [
420-
{
421-
"itemIdentifier": failed_record["messageId"]
422-
}
423-
]
424-
}
425-
426-
# WHEN
427-
ret = app.lambda_handler(sqs_event, lambda_context)
428-
429-
# THEN
430-
assert ret == expected_response
431-
assert len(processor.fail_messages) == 1
432-
assert processor.success_messages[0] == successful_record
357+
--8<-- "examples/batch_processing/testing/test_app.py"
433358
```
434359

435360
=== "src/app.py"
436361

437362
```python
438-
import json
439-
440-
from aws_lambda_powertools import Logger, Tracer
441-
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
442-
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
443-
from aws_lambda_powertools.utilities.typing import LambdaContext
444-
445-
446-
processor = BatchProcessor(event_type=EventType.SQS)
447-
tracer = Tracer()
448-
logger = Logger()
449-
450-
451-
@tracer.capture_method
452-
def record_handler(record: SQSRecord):
453-
payload: str = record.body
454-
if payload:
455-
item: dict = json.loads(payload)
456-
...
457-
458-
@logger.inject_lambda_context
459-
@tracer.capture_lambda_handler
460-
def lambda_handler(event, context: LambdaContext):
461-
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
363+
--8<-- "examples/batch_processing/testing/src/app.py"
462364
```
463365

464366
=== "Sample SQS event"
465367

466-
```json title="events/sqs_sample.json"
467-
{
468-
"Records": [
469-
{
470-
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
471-
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
472-
"body": "{\"Message\": \"success\"}",
473-
"attributes": {
474-
"ApproximateReceiveCount": "1",
475-
"SentTimestamp": "1545082649183",
476-
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
477-
"ApproximateFirstReceiveTimestamp": "1545082649185"
478-
},
479-
"messageAttributes": {},
480-
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
481-
"eventSource": "aws:sqs",
482-
"eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
483-
"awsRegion": "us-east-1"
484-
},
485-
{
486-
"messageId": "244fc6b4-87a3-44ab-83d2-361172410c3a",
487-
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
488-
"body": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
489-
"attributes": {
490-
"ApproximateReceiveCount": "1",
491-
"SentTimestamp": "1545082649183",
492-
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
493-
"ApproximateFirstReceiveTimestamp": "1545082649185"
494-
},
495-
"messageAttributes": {},
496-
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
497-
"eventSource": "aws:sqs",
498-
"eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
499-
"awsRegion": "us-east-1"
500-
}
501-
]
502-
}
368+
```json title="events/sqs_event.json"
369+
--8<-- "examples/batch_processing/testing/events/sqs_event.json"
503370
```
504371

505372
## FAQ
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
from aws_lambda_powertools import Logger, Tracer
4+
from aws_lambda_powertools.utilities.batch import (
5+
BatchProcessor,
6+
EventType,
7+
batch_processor,
8+
)
9+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
processor = BatchProcessor(event_type=EventType.SQS)
13+
tracer = Tracer()
14+
logger = Logger()
15+
16+
17+
@tracer.capture_method(capture_response=False)
18+
def record_handler(record: SQSRecord):
19+
payload: str = record.body
20+
if payload:
21+
item: dict = json.loads(payload)
22+
logger.info(item)
23+
24+
25+
@logger.inject_lambda_context
26+
@tracer.capture_lambda_handler
27+
@batch_processor(record_handler=record_handler, processor=processor)
28+
def lambda_handler(event, context: LambdaContext):
29+
return processor.response()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
5+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
6+
"body": "{\"Message\": \"success\"}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1545082649183",
10+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
11+
"ApproximateFirstReceiveTimestamp": "1545082649185"
12+
},
13+
"messageAttributes": {},
14+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
15+
"eventSource": "aws:sqs",
16+
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
17+
"awsRegion": "us-east-1"
18+
},
19+
{
20+
"messageId": "244fc6b4-87a3-44ab-83d2-361172410c3a",
21+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
22+
"body": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
23+
"attributes": {
24+
"ApproximateReceiveCount": "1",
25+
"SentTimestamp": "1545082649183",
26+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
27+
"ApproximateFirstReceiveTimestamp": "1545082649185"
28+
},
29+
"messageAttributes": {},
30+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
31+
"eventSource": "aws:sqs",
32+
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
33+
"awsRegion": "us-east-1"
34+
}
35+
]
36+
}

examples/batch_processing/testing/src/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
3+
from aws_lambda_powertools import Logger, Tracer
4+
from aws_lambda_powertools.utilities.batch import (
5+
BatchProcessor,
6+
EventType,
7+
process_partial_response,
8+
)
9+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
processor = BatchProcessor(event_type=EventType.SQS)
13+
tracer = Tracer()
14+
logger = Logger()
15+
16+
17+
@tracer.capture_method
18+
def record_handler(record: SQSRecord):
19+
payload: str = record.body
20+
if payload:
21+
item: dict = json.loads(payload)
22+
logger.info(item)
23+
24+
25+
@logger.inject_lambda_context
26+
@tracer.capture_lambda_handler
27+
def lambda_handler(event, context: LambdaContext):
28+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
from dataclasses import dataclass
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from examples.batch_processing.testing.src import app
8+
9+
10+
def load_event(path: Path):
11+
with path.open() as f:
12+
return json.load(f)
13+
14+
15+
@pytest.fixture
16+
def lambda_context():
17+
@dataclass
18+
class LambdaContext:
19+
function_name: str = "test"
20+
memory_limit_in_mb: int = 128
21+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
22+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
23+
24+
return LambdaContext()
25+
26+
27+
@pytest.fixture()
28+
def sqs_event():
29+
"""Generates API GW Event"""
30+
return load_event(path=Path("events/sqs_event.json"))
31+
32+
33+
def test_app_batch_partial_response(sqs_event, lambda_context):
34+
# GIVEN
35+
processor = app.processor # access processor for additional assertions
36+
successful_record = sqs_event["Records"][0]
37+
failed_record = sqs_event["Records"][1]
38+
expected_response = {"batchItemFailures": [{"itemIdentifier": failed_record["messageId"]}]}
39+
40+
# WHEN
41+
ret = app.lambda_handler(sqs_event, lambda_context)
42+
43+
# THEN
44+
assert ret == expected_response
45+
assert len(processor.fail_messages) == 1
46+
assert processor.success_messages[0] == successful_record

0 commit comments

Comments
 (0)