Skip to content

Commit

Permalink
Comments and tests for misc_utils.merge_objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 27, 2023
1 parent 5db0985 commit 59673ca
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,9 @@ def merge_key_value_dict_lists(x, y):
def merge_objects(target: Union[dict, List[Any]], source: Union[dict, List[Any]], full: bool = False) -> dict:
"""
Merges the given source dictionary or list into the target dictionary or list.
This MAY change the given target (dictionary or list) IN PLACE.
This MAY well change the given target (dictionary or list) IN PLACE.
The the full argument is True then any target lists longer than the
source be will be filled out with the last element(s) of the source.
"""
if target is None:
return source
Expand Down
47 changes: 47 additions & 0 deletions test/test_misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3625,3 +3625,50 @@ def test_merge_objects_2():
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}, {"goo": None}]}
merge_objects(target, source, True)
assert target == expected


def test_merge_objects_3():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": [None, None]}
source = {"xyzzy": [{"foo": None}, {"goo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}]}
merge_objects(target, source, False)
assert target == expected


def test_merge_objects_4():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": [None, None]}
source = {"xyzzy": [{"foo": None}, {"goo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}]}
merge_objects(target, source, True)
assert target == expected


def test_merge_objects_5():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": ["mno"]}
source = {"xyzzy": [{"foo": None}, {"goo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}]}
merge_objects(target, source, False)
assert target == expected

def test_merge_objects_6():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": ["mno"]}
source = {"xyzzy": [{"foo": None}, {"goo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}]}
merge_objects(target, source, True)
assert target == expected


def test_merge_objects_7():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": [None, None, "abc", "def", 123]}
source = {"xyzzy": [{"foo": None}, {"goo": None}, {"hoo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}, {"hoo": None}, "def", 123]}
merge_objects(target, source, False)
assert target == expected


def test_merge_objects_8():
target = {"abc": {"def": {"ghi": None}}, "xyzzy": [None, None, "abc", "def", 123]}
source = {"xyzzy": [{"foo": None}, {"goo": None}, {"hoo": None}]}
expected = {"abc": {"def": {"ghi": None}}, "xyzzy": [{"foo": None}, {"goo": None}, {"hoo": None}, {"hoo": None}, {"hoo": None}]}
merge_objects(target, source, True)
assert target == expected

0 comments on commit 59673ca

Please sign in to comment.