|
6 | 6 | @pytest.mark.parametrize( |
7 | 7 | "thedicts,expected", |
8 | 8 | [ |
9 | | - # 1 |
| 9 | + # 1: two dicts with a common key (last one wins) |
10 | 10 | ([{"a": 1}, {"a": 2}], {"a": 2}), |
11 | | - # 2 |
| 11 | + # 2: two dicts with a common key and another key |
| 12 | + # (last one wins for common key, other key is preserved) |
12 | 13 | ([{"a": 1, "b": 2}, {"a": 2}], {"a": 2, "b": 2}), |
13 | | - # 3 |
| 14 | + # 3: nested dictionaries |
14 | 15 | ( |
15 | 16 | [{"db": {"host": "localhost", "port": 1234}}, {"db": {"port": 5432}}], |
16 | 17 | {"db": {"host": "localhost", "port": 5432}}, |
17 | 18 | ), |
18 | | - # |
| 19 | + # 4: Lists |
| 20 | + ( |
| 21 | + [{"a": [1, 2]}, {"a": [3, 4]}], |
| 22 | + {"a": [1, 2, 3, 4]}, |
| 23 | + ), |
| 24 | + # 5: Mixed types (overwrite list with int) |
| 25 | + ( |
| 26 | + [{"a": [1, 2]}, {"a": 3}], |
| 27 | + {"a": 3}, |
| 28 | + ), |
| 29 | + # 6: Mixed types (overwrite int with list) |
| 30 | + ( |
| 31 | + [{"a": 1}, {"a": [2, 3]}], |
| 32 | + {"a": [2, 3]}, |
| 33 | + ), |
| 34 | + # 7: Nested lists |
| 35 | + ( |
| 36 | + [{"x": {"l": ["a"]}}, {"x": {"l": ["b"]}}], |
| 37 | + {"x": {"l": ["a", "b"]}}, |
| 38 | + ), |
| 39 | + # 8: Lists containing dictionaries |
| 40 | + ( |
| 41 | + [{"a": [{"x": 1}]}, {"a": [{"y": 2}]}], |
| 42 | + {"a": [{"x": 1}, {"y": 2}]}, |
| 43 | + ), |
| 44 | + # 9: Tuples |
| 45 | + ( |
| 46 | + [{"t": (1, 2)}, {"t": (3, 4)}], |
| 47 | + {"t": (1, 2, 3, 4)}, |
| 48 | + ), |
| 49 | + # 10: Sets |
| 50 | + ( |
| 51 | + [{"s": {1, 2}}, {"s": {3, 4}}], |
| 52 | + {"s": {1, 2, 3, 4}}, |
| 53 | + ), |
| 54 | + # 11: Sets with overlap |
| 55 | + ( |
| 56 | + [{"s": {1, 2}}, {"s": {2, 3}}], |
| 57 | + {"s": {1, 2, 3}}, |
| 58 | + ), |
19 | 59 | ], |
20 | 60 | ) |
21 | 61 | def test_deep_merge(thedicts, expected): |
22 | 62 | assert deep_merge(*thedicts) == expected |
| 63 | + |
| 64 | + |
| 65 | +def test_deep_merge_is_truly_deep(): |
| 66 | + d1 = {"nested": [1, 2], "deep": {"a": 1}} |
| 67 | + d2 = {} |
| 68 | + merged = deep_merge(d1, d2) |
| 69 | + |
| 70 | + # Modify result |
| 71 | + merged["nested"].append(3) |
| 72 | + merged["deep"]["b"] = 2 |
| 73 | + |
| 74 | + # Assert original is untouched |
| 75 | + assert d1["nested"] == [1, 2] |
| 76 | + assert "b" not in d1["deep"] |
| 77 | + |
| 78 | + |
| 79 | +def test_three_way_merge(): |
| 80 | + d1 = {"a": 1} |
| 81 | + d2 = {"b": 2} |
| 82 | + d3 = {"c": 3} |
| 83 | + assert deep_merge(d1, d2, d3) == {"a": 1, "b": 2, "c": 3} |
| 84 | + |
| 85 | + |
| 86 | +def test_deep_merge_inputs_remain_independent(): |
| 87 | + # Verify that subsequent dictionaries (not just the first) are also treated as immutable sources |
| 88 | + d1 = {} |
| 89 | + d2 = {"a": [1, 2], "b": {"x": 1}} |
| 90 | + merged = deep_merge(d1, d2) |
| 91 | + |
| 92 | + # Modify result |
| 93 | + merged["a"].append(3) |
| 94 | + merged["b"]["y"] = 2 |
| 95 | + |
| 96 | + # Assert input d2 is untouched |
| 97 | + assert d2["a"] == [1, 2] |
| 98 | + assert "y" not in d2["b"] |
| 99 | + |
| 100 | + |
| 101 | +def test_deep_merge_return_type(): |
| 102 | + class MyMap(dict): |
| 103 | + pass |
| 104 | + |
| 105 | + m1 = MyMap({"a": 1}) |
| 106 | + m2 = {"b": 2} |
| 107 | + result = deep_merge(m1, m2) |
| 108 | + |
| 109 | + assert isinstance(result, dict) |
| 110 | + assert not isinstance(result, MyMap) |
| 111 | + assert result == {"a": 1, "b": 2} |
| 112 | + |
0 commit comments