forked from dpath-maintainers/dpath-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util_new.py
More file actions
83 lines (62 loc) · 1.84 KB
/
test_util_new.py
File metadata and controls
83 lines (62 loc) · 1.84 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import dpath.util
def test_set_new_separator():
dict = {
"a": {
},
}
dpath.util.new(dict, ';a;b', 1, separator=";")
assert(dict['a']['b'] == 1)
dpath.util.new(dict, ['a', 'b'], 1, separator=";")
assert(dict['a']['b'] == 1)
def test_set_new_dict():
dict = {
"a": {
},
}
dpath.util.new(dict, '/a/b', 1)
assert(dict['a']['b'] == 1)
dpath.util.new(dict, ['a', 'b'], 1)
assert(dict['a']['b'] == 1)
def test_set_new_list():
dict = {
"a": [
],
}
dpath.util.new(dict, '/a/1', 1)
assert(dict['a'][1] == 1)
assert(dict['a'][0] is None)
dpath.util.new(dict, ['a', 1], 1)
assert(dict['a'][1] == 1)
assert(dict['a'][0] is None)
def test_set_new_list_path_with_separator():
# This test kills many birds with one stone, forgive me
dict = {
"a": {
},
}
dpath.util.new(dict, ['a', 'b/c/d', 0], 1)
assert(len(dict['a']) == 1)
assert(len(dict['a']['b/c/d']) == 1)
assert(dict['a']['b/c/d'][0] == 1)
def test_set_new_list_integer_path_with_creator():
d = {}
def mycreator(obj, pathcomp, nextpathcomp, hints):
print(hints)
print(pathcomp)
print(nextpathcomp)
print("...")
target = pathcomp[0]
if isinstance(obj, list) and (target.isdigit()):
target = int(target)
if ((nextpathcomp is not None) and (isinstance(nextpathcomp, int) or str(nextpathcomp).isdigit())):
obj[target] = [None] * (int(nextpathcomp) + 1)
print("Created new list in target")
else:
print("Created new dict in target")
obj[target] = {}
print(obj)
dpath.util.new(d, '/a/2', 3, creator=mycreator)
print(d)
assert(isinstance(d['a'], list))
assert(len(d['a']) == 3)
assert(d['a'][2] == 3)