Skip to content

Commit 3a11563

Browse files
Addressing Heitor's feedback
1 parent d8be53e commit 3a11563

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

aws_lambda_powertools/utilities/serialization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
from typing import Any, Callable
55

66

7+
def base64_encode(data: str) -> str:
8+
"""Encode a string and returns Base64-encoded encoded value.
9+
10+
Parameters
11+
----------
12+
data: str
13+
The string to encode.
14+
15+
Returns
16+
-------
17+
str
18+
The Base64-encoded encoded value.
19+
"""
20+
return base64.b64encode(data.encode()).decode("utf-8")
21+
22+
723
def base64_decode(data: str) -> str:
824
"""Decodes a Base64-encoded string and returns the decoded value.
925

tests/unit/data_classes/test_kinesis_firehose_response.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
KinesisFirehoseDataTransformationResponse,
55
KinesisFirehoseEvent,
66
)
7+
from aws_lambda_powertools.utilities.serialization import base64_encode, base64_from_str
78
from tests.functional.utils import load_event
89

910

@@ -42,30 +43,38 @@ def test_kinesis_firehose_response():
4243

4344

4445
def test_kinesis_firehose_create_response():
46+
# GIVEN a Kinesis Firehose Event with two records
4547
raw_event = load_event("kinesisFirehoseKinesisEvent.json")
4648
parsed_event = KinesisFirehoseEvent(data=raw_event)
4749

50+
# WHEN we create a Data Transformation Response changing the data
51+
# WHEN we add partitions keys
52+
53+
arbitrary_data = "arbitrary data"
54+
4855
response = KinesisFirehoseDataTransformationResponse()
4956
for record in parsed_event.records:
50-
# if data was delivered as json; caches loaded value
51-
data = record.data_as_text
5257
metadata_partition = KinesisFirehoseDataTransformationRecordMetadata(partition_keys={"year": 2023})
5358
processed_record = record.build_data_transformation_response(
5459
result="Ok",
5560
metadata=metadata_partition,
61+
data=base64_from_str(arbitrary_data),
5662
)
57-
processed_record.data_from_text(data=data)
5863
response.add_record(record=processed_record)
59-
response_dict = response.asdict()
60-
61-
res_records = list(response_dict["records"])
62-
assert len(res_records) == 2
63-
record_01, record_02 = res_records[:]
64-
record01_raw = raw_event["records"][0]
65-
assert record_01["result"] == "Ok"
66-
assert record_01["recordId"] == record01_raw["recordId"]
67-
assert record_01["data"] == record01_raw["data"]
68-
assert record_01["metadata"]["partitionKeys"]["year"] == 2023
69-
70-
assert response.records[0].data_as_bytes == b"Hello World"
71-
assert response.records[0].data_as_text == "Hello World"
64+
65+
# THEN we should have the same record data
66+
record_01, record_02 = response.records[0], response.records[1]
67+
raw_record_01, raw_record_02 = raw_event["records"][0], raw_event["records"][1]
68+
69+
assert len(response.records) == 2
70+
71+
assert record_01.result == "Ok"
72+
assert record_02.result == "Ok"
73+
74+
assert record_01.record_id == raw_record_01["recordId"]
75+
assert record_02.record_id == raw_record_02["recordId"]
76+
77+
assert record_01.data == base64_encode(arbitrary_data)
78+
assert record_02.data == base64_encode(arbitrary_data)
79+
80+
assert record_01.metadata.partition_keys["year"] == 2023

0 commit comments

Comments
 (0)