|
2 | 2 | import unittest |
3 | 3 |
|
4 | 4 | from pydu.dict import (AttrDict, LookupDict, CaseInsensitiveDict, |
5 | | - OrderedDefaultDict, attrify, pick, omit) |
| 5 | + OrderedDefaultDict, attrify, pick, omit, |
| 6 | + get_path, set_path, deep_merge) |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class TestAttrDict: |
@@ -151,3 +152,48 @@ def test_omit_accepts_single_string_key(): |
151 | 152 | data = {'name': 'pydu', 'private': True} |
152 | 153 |
|
153 | 154 | assert omit(data, 'private') == {'name': 'pydu'} |
| 155 | + |
| 156 | + |
| 157 | +def test_get_path_reads_dotted_and_iterable_paths(): |
| 158 | + data = {'user': {'profile': {'name': 'pydu'}}} |
| 159 | + |
| 160 | + assert get_path(data, 'user.profile.name') == 'pydu' |
| 161 | + assert get_path(data, ('user', 'profile', 'name')) == 'pydu' |
| 162 | + |
| 163 | + |
| 164 | +def test_get_path_returns_default_when_missing(): |
| 165 | + data = {'user': {'profile': {}}} |
| 166 | + |
| 167 | + assert get_path(data, 'user.profile.name', default='unknown') == 'unknown' |
| 168 | + assert get_path(data, 'user.profile.name') is None |
| 169 | + |
| 170 | + |
| 171 | +def test_set_path_creates_nested_dicts_and_returns_mapping(): |
| 172 | + data = {} |
| 173 | + |
| 174 | + result = set_path(data, 'user.profile.name', 'pydu') |
| 175 | + |
| 176 | + assert result is data |
| 177 | + assert data == {'user': {'profile': {'name': 'pydu'}}} |
| 178 | + |
| 179 | + |
| 180 | +def test_set_path_replaces_non_mapping_intermediate_values(): |
| 181 | + data = {'user': 'legacy'} |
| 182 | + |
| 183 | + set_path(data, ('user', 'profile', 'name'), 'pydu') |
| 184 | + |
| 185 | + assert data == {'user': {'profile': {'name': 'pydu'}}} |
| 186 | + |
| 187 | + |
| 188 | +def test_deep_merge_recursively_combines_mappings_without_mutating_inputs(): |
| 189 | + base = {'user': {'name': 'pydu', 'active': True}, 'tags': ['old']} |
| 190 | + override = {'user': {'active': False, 'role': 'admin'}, 'tags': ['new']} |
| 191 | + |
| 192 | + result = deep_merge(base, override) |
| 193 | + |
| 194 | + assert result == { |
| 195 | + 'user': {'name': 'pydu', 'active': False, 'role': 'admin'}, |
| 196 | + 'tags': ['new'], |
| 197 | + } |
| 198 | + assert base == {'user': {'name': 'pydu', 'active': True}, 'tags': ['old']} |
| 199 | + assert override == {'user': {'active': False, 'role': 'admin'}, 'tags': ['new']} |
0 commit comments