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