Skip to content

Commit

Permalink
ISSUE-1503: ISSUE-1503: ISSUE-1503: fix: add Mapping abc and missing …
Browse files Browse the repository at this point in the history
…methods to DictWrapper

ISSUE-1503: Add DictWrapper Mapping abc tests

ISSUE-1503: add StreamRecord tests
  • Loading branch information
Tankanow committed Sep 13, 2022
1 parent 9e54eb8 commit c5394f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
10 changes: 8 additions & 2 deletions aws_lambda_powertools/utilities/data_classes/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import base64
import json
from typing import Any, Dict, Optional
from typing import Any, Dict, Iterator, Mapping, Optional


class DictWrapper:
class DictWrapper(Mapping):
"""Provides a single read only access to a wrapper dict"""

def __init__(self, data: Dict[str, Any]):
Expand All @@ -19,6 +19,12 @@ def __eq__(self, other: Any) -> bool:

return self._data == other._data

def __iter__(self) -> Iterator:
return iter(self._data)

def __len__(self) -> int:
return len(self._data)

def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
return self._data.get(key, default)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ def approximate_creation_date_time(self) -> Optional[int]:
item = self.get("ApproximateCreationDateTime")
return None if item is None else int(item)

# This override breaks the Mapping protocol of DictWrapper, it's left here for backwards compatibility with
# a 'type: ignore' comment. This is currently the only subclass of DictWrapper that breaks this protocol.
@property
def keys(self) -> Optional[Dict[str, AttributeValue]]:
def keys(self) -> Optional[Dict[str, AttributeValue]]: # type: ignore
"""The primary key attribute(s) for the DynamoDB item that was modified."""
return _attribute_value_dict(self._data, "Keys")

Expand Down
31 changes: 31 additions & 0 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
AttributeValueType,
DynamoDBRecordEventName,
DynamoDBStreamEvent,
StreamRecord,
StreamViewType,
)
from aws_lambda_powertools.utilities.data_classes.event_source import event_source
Expand Down Expand Up @@ -101,6 +102,19 @@ def message(self) -> str:
assert DataClassSample(data1).raw_event is data1


def test_dict_wrapper_imlements_mapping():
class DataClassSample(DictWrapper):
pass

data = {"message": "foo1"}
dcs = DataClassSample(data)
assert len(dcs) == len(data)
assert list(dcs) == list(data)
assert dcs.keys() == data.keys()
assert list(dcs.values()) == list(data.values())
assert dcs.items() == data.items()


def test_cloud_watch_dashboard_event():
event = CloudWatchDashboardCustomWidgetEvent(load_event("cloudWatchDashboardEvent.json"))
assert event.describe is False
Expand Down Expand Up @@ -617,6 +631,23 @@ def test_dynamo_attribute_value_type_error():
print(attribute_value.get_type)


def test_stream_record_keys_with_valid_keys():
attribute_value = {"Foo": "Bar"}
sr = StreamRecord({"Keys": {"Key1": attribute_value}})
assert sr.keys == {"Key1": AttributeValue(attribute_value)}


def test_stream_record_keys_with_no_keys():
sr = StreamRecord({})
assert sr.keys is None


def test_stream_record_keys_overrides_dict_wrapper_keys():
data = {"Keys": {"key1": {"attr1": "value1"}}}
sr = StreamRecord(data)
assert sr.keys != data.keys()


def test_event_bridge_event():
event = EventBridgeEvent(load_event("eventBridgeEvent.json"))

Expand Down

0 comments on commit c5394f5

Please sign in to comment.