|
1 | 1 | from nose.tools import assert_raises |
| 2 | + |
| 3 | +import datetime |
| 4 | +import decimal |
2 | 5 | import dpath.util |
3 | 6 | import mock |
| 7 | +import time |
4 | 8 |
|
5 | 9 |
|
6 | 10 | def test_util_get_root(): |
@@ -140,3 +144,66 @@ def test_values_list(): |
140 | 144 | ret = dpath.util.values(a, 'actions/*') |
141 | 145 | assert(isinstance(ret, list)) |
142 | 146 | assert(len(ret) == 2) |
| 147 | + |
| 148 | + |
| 149 | +def test_non_leaf_leaf(): |
| 150 | + # The leaves in this test aren't leaf(thing) == True, but we should still |
| 151 | + # be able to get them. They should also not prevent fetching other values. |
| 152 | + |
| 153 | + def func(x): |
| 154 | + return x |
| 155 | + |
| 156 | + testdict = { |
| 157 | + 'a': func, |
| 158 | + 'b': lambda x: x, |
| 159 | + 'c': [ |
| 160 | + { |
| 161 | + 'a', |
| 162 | + 'b', |
| 163 | + }, |
| 164 | + ], |
| 165 | + 'd': [ |
| 166 | + decimal.Decimal(1.5), |
| 167 | + decimal.Decimal(2.25), |
| 168 | + ], |
| 169 | + 'e': datetime.datetime(2020, 1, 1), |
| 170 | + 'f': { |
| 171 | + 'config': 'something', |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + # It should be possible to get the callables: |
| 176 | + assert dpath.util.get(testdict, 'a') == func |
| 177 | + assert dpath.util.get(testdict, 'b')(42) == 42 |
| 178 | + |
| 179 | + # It should be possible to get other values: |
| 180 | + assert dpath.util.get(testdict, 'c/0') == testdict['c'][0] |
| 181 | + assert dpath.util.get(testdict, 'd')[0] == testdict['d'][0] |
| 182 | + assert dpath.util.get(testdict, 'd/0') == testdict['d'][0] |
| 183 | + assert dpath.util.get(testdict, 'd/1') == testdict['d'][1] |
| 184 | + assert dpath.util.get(testdict, 'e') == testdict['e'] |
| 185 | + |
| 186 | + # Values should also still work: |
| 187 | + assert dpath.util.values(testdict, 'f/config') == ['something'] |
| 188 | + |
| 189 | + # Data classes should also be retrievable: |
| 190 | + try: |
| 191 | + import dataclasses |
| 192 | + except: |
| 193 | + return |
| 194 | + |
| 195 | + @dataclasses.dataclass |
| 196 | + class Connection: |
| 197 | + group_name: str |
| 198 | + channel_name: str |
| 199 | + last_seen: float |
| 200 | + |
| 201 | + testdict['g'] = { |
| 202 | + 'my-key': Connection( |
| 203 | + group_name='foo', |
| 204 | + channel_name='bar', |
| 205 | + last_seen=time.time(), |
| 206 | + ), |
| 207 | + } |
| 208 | + |
| 209 | + assert dpath.util.search(testdict, 'g/my*')['g']['my-key'] == testdict['g']['my-key'] |
0 commit comments