1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import dpath
import sys
def test_broken_afilter():
def afilter(x):
if x in [1, 2]:
return True
return False
dict = {
"a": {
"view_failure": "a",
"b": {
"c": {
"d": 0,
"e": 1,
"f": 2,
},
},
},
}
paths = [
'a/b/c/e',
'a/b/c/f',
]
for (path, value) in dpath.search(dict, '/**', yielded=True, afilter=afilter):
assert path in paths
assert "view_failure" not in dpath.search(dict, '/**', afilter=afilter)['a']
assert "d" not in dpath.search(dict, '/**', afilter=afilter)['a']['b']['c']
for (path, value) in dpath.search(dict, ['**'], yielded=True, afilter=afilter):
assert path in paths
assert "view_failure" not in dpath.search(dict, ['**'], afilter=afilter)['a']
assert "d" not in dpath.search(dict, ['**'], afilter=afilter)['a']['b']['c']
def filter(x):
sys.stderr.write(str(x))
if hasattr(x, 'get'):
return x.get('type', None) == 'correct'
return False
a = {
'actions': [
{
'type': 'correct'
},
{
'type': 'incorrect'
},
],
}
results = [[x[0], x[1]] for x in dpath.search(a, 'actions/*', yielded=True)]
results = [[x[0], x[1]] for x in dpath.search(a, 'actions/*', afilter=filter, yielded=True)]
assert len(results) == 1
assert results[0][1]['type'] == 'correct'
|