-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathtest_new.py
More file actions
112 lines (81 loc) · 2.45 KB
/
test_new.py
File metadata and controls
112 lines (81 loc) · 2.45 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from nose2.tools.such import helper
import dpath
from dpath import options
def test_set_new_separator():
dict = {
"a": {
},
}
dpath.new(dict, ';a;b', 1, separator=";")
assert dict['a']['b'] == 1
dpath.new(dict, ['a', 'b'], 1, separator=";")
assert dict['a']['b'] == 1
def test_set_new_dict():
dict = {
"a": {
},
}
dpath.new(dict, '/a/b', 1)
assert dict['a']['b'] == 1
dpath.new(dict, ['a', 'b'], 1)
assert dict['a']['b'] == 1
def test_set_new_list():
dict = {
"a": [
],
}
dpath.new(dict, '/a/1', 1)
assert dict['a'][1] == 1
assert dict['a'][0] is None
dpath.new(dict, ['a', 1], 1)
assert dict['a'][1] == 1
assert dict['a'][0] is None
def test_set_list_with_dict_int_ambiguity():
d = {"list": [{"root": {"1": {"k": None}}}]}
dpath.new(d, "list/0/root/1/k", "new")
expected = {"list": [{"root": {"1": {"k": "new"}}}]}
assert d == expected
def test_set_new_list_path_with_separator():
# This test kills many birds with one stone, forgive me
dict = {
"a": {
},
}
dpath.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.new(d, '/a/2', 3, creator=mycreator)
print(d)
assert isinstance(d['a'], list)
assert len(d['a']) == 3
assert d['a'][2] == 3
def test_new_overwrite_none_in_list():
a = {}
dpath.new(a, ['b'], [])
dpath.new(a, ['b', 3], 5)
with helper.assertRaises(dpath.exceptions.PathNotFound):
dpath.new(a, ['b', 1, "c"], 5)
options.REPLACE_NONE_VALUES_IN_LISTS = True
a = {}
dpath.new(a, ['b'], [])
dpath.new(a, ['b', 3], 5)
dpath.new(a, ['b', 1, "c"], 5)
assert a["b"][1]["c"] == 5