|
|
@ -26,27 +26,6 @@ _TEST_DATA = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTraversal:
|
|
|
|
class TestTraversal:
|
|
|
|
def test_dict_get(self):
|
|
|
|
|
|
|
|
FALSE_VALUES = {
|
|
|
|
|
|
|
|
'none': None,
|
|
|
|
|
|
|
|
'false': False,
|
|
|
|
|
|
|
|
'zero': 0,
|
|
|
|
|
|
|
|
'empty_string': '',
|
|
|
|
|
|
|
|
'empty_list': [],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
d = {**FALSE_VALUES, 'a': 42}
|
|
|
|
|
|
|
|
assert dict_get(d, 'a') == 42
|
|
|
|
|
|
|
|
assert dict_get(d, 'b') is None
|
|
|
|
|
|
|
|
assert dict_get(d, 'b', 42) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('a',)) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'a')) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', 'a', 'd')) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c')) is None
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c'), 42) == 42
|
|
|
|
|
|
|
|
for key, false_value in FALSE_VALUES.items():
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', key)) is None
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', key), skip_false_values=False) == false_value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_traversal_base(self):
|
|
|
|
def test_traversal_base(self):
|
|
|
|
assert traverse_obj(_TEST_DATA, ('str',)) == 'str', \
|
|
|
|
assert traverse_obj(_TEST_DATA, ('str',)) == 'str', \
|
|
|
|
'allow tuple path'
|
|
|
|
'allow tuple path'
|
|
|
@ -440,3 +419,26 @@ class TestTraversal:
|
|
|
|
'function key should yield all values'
|
|
|
|
'function key should yield all values'
|
|
|
|
assert traverse_obj(morsel, [(None,), any]) == morsel, \
|
|
|
|
assert traverse_obj(morsel, [(None,), any]) == morsel, \
|
|
|
|
'Morsel should not be implicitly changed to dict on usage'
|
|
|
|
'Morsel should not be implicitly changed to dict on usage'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDictGet:
|
|
|
|
|
|
|
|
def test_dict_get(self):
|
|
|
|
|
|
|
|
FALSE_VALUES = {
|
|
|
|
|
|
|
|
'none': None,
|
|
|
|
|
|
|
|
'false': False,
|
|
|
|
|
|
|
|
'zero': 0,
|
|
|
|
|
|
|
|
'empty_string': '',
|
|
|
|
|
|
|
|
'empty_list': [],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
d = {**FALSE_VALUES, 'a': 42}
|
|
|
|
|
|
|
|
assert dict_get(d, 'a') == 42
|
|
|
|
|
|
|
|
assert dict_get(d, 'b') is None
|
|
|
|
|
|
|
|
assert dict_get(d, 'b', 42) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('a',)) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'a')) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', 'a', 'd')) == 42
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c')) is None
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c'), 42) == 42
|
|
|
|
|
|
|
|
for key, false_value in FALSE_VALUES.items():
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', key)) is None
|
|
|
|
|
|
|
|
assert dict_get(d, ('b', 'c', key), skip_false_values=False) == false_value
|
|
|
|