Skip to content

Commit

Permalink
Modified Item_Hash func to hash nested Dict (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammokhov authored Sep 2, 2021
1 parent 1406fd9 commit f53256c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/rpdk/core/jsonutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ class FlatteningError(Exception):
pass


def item_hash(item):
"""MD5 hash for an item (Dictionary/Scalar)"""
def item_hash(
item,
): # assumption -> input is only json comparable type (dict/list/scalar)
"""MD5 hash for an item (Dictionary/Iterable/Scalar)"""
dhash = hashlib.md5() # nosec
if isinstance(item, dict):
item = {k: item_hash(v) for k, v in item.items()}
if isinstance(item, list):
item = [item_hash(i) for i in item].sort()
encoded = json.dumps(item, sort_keys=True).encode()
dhash.update(encoded)
return dhash.hexdigest()
Expand Down
27 changes: 27 additions & 0 deletions tests/contract/test_resource_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,33 @@ def test_compare_should_throw_exception(resource_client):
{"CollectionToCompare": ["item1", "item2", "item3"]},
{"properties": {"CollectionToCompare": {"insertionOrder": True}}},
),
(
{
"CollectionToCompare": [
"item1",
"item2",
"item3",
{"i": ["item1", "item2"]},
[
{"j1": {"z": {"l": 10}}, "k3": ["item5", "item4", "item1"]},
{"j": {"z": {"l": 10}}, "k": ["item4", "item3", "item2"]},
],
]
},
{
"CollectionToCompare": [
"item3",
"item2",
"item1",
{"i": ["item2", "item1"]},
[
{"j": {"k": ["item2", "item3", "item4"], "z": {"l": 10}}},
{"j1": {"k3": ["item1", "item5", "item4"], "z": {"l": 10}}},
],
]
},
{"properties": {"CollectionToCompare": {"insertionOrder": False}}},
),
],
)
def test_compare_collection(resource_client, inputs, outputs, schema_fragment):
Expand Down

0 comments on commit f53256c

Please sign in to comment.