diff --git a/dcicutils/misc_utils.py b/dcicutils/misc_utils.py index 6f8100d51..e38f980df 100644 --- a/dcicutils/misc_utils.py +++ b/dcicutils/misc_utils.py @@ -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 diff --git a/test/test_misc_utils.py b/test/test_misc_utils.py index 52bed8f70..7d737793a 100644 --- a/test/test_misc_utils.py +++ b/test/test_misc_utils.py @@ -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