-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
66 lines (50 loc) · 2.39 KB
/
test.py
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
from src.dict_path import extract_dict, inject_dict, DictPath
def test_extract_value():
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
assert extract_dict(test_dict, 'foo1/foo2/foo3/foo4') == 'bar'
def test_extract_dict():
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
assert extract_dict(test_dict, 'foo1/foo2/foo3') == {'foo4':'bar'}
def test_inject_value():
test_dict = {'foo1':{'foo2':{'foo3':{}}}}
inject_dict(test_dict, 'foo1/foo2/foo3', {'foo4':'bar'})
assert extract_dict(test_dict, 'foo1/foo2/foo3/foo4') == 'bar'
def test_extract_array():
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
assert extract_dict(test_dict, 'foo1/foo2/1/foo4/0/0/foo5') == 'bar'
assert extract_dict(test_dict, 'foo1/foo2/1/foo4/0/0/foo5/') == 'bar'
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
data = DictPath(test_dict)
def test_get_with_path():
assert data.get('foo1/foo2/foo3/foo4') == 'bar'
def test_get_with_path_start_with_slash():
assert data.get('/foo1/foo2/foo3/foo4') == 'bar'
def test_get_with_get():
assert data.get('foo1').get('foo2').get('foo3') == {'foo4':'bar'}
def test_get_with_get1():
assert data['foo1']['foo2']['foo3'] == {'foo4':'bar'}
def test_set_with_path():
data.set('foo1/foo2/foo3/foo5', 'bar1')
assert data.get('foo1/foo2/foo3/foo5') == 'bar1'
def test_set_with_path_start_with_slash():
data.set('/foo1/foo2/foo3/foo5', 'bar1')
assert data.get('foo1/foo2/foo3/foo5') == 'bar1'
def test_set_with_get():
data.get('foo1').get('foo2').get('foo3')['foo5'] = 'bar1'
assert data.get('foo1/foo2/foo3/foo5') == 'bar1'
def test_set_with_get1():
data['foo1']['foo2']['foo3']['foo5'] = 'bar1'
assert data.get('foo1/foo2/foo3/foo5') == 'bar1'
def test_equalness_with_normal_dict():
normal_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
dic_path = DictPath(normal_dict)
assert normal_dict == dic_path
assert dic_path.dict is normal_dict
def test_extract_array_with_dict():
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
dic_path = DictPath(test_dict)
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo5/') == 'bar'
def test_extract_array_with_dict_none():
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
dic_path = DictPath(test_dict)
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo6/foo7/') == None