-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
63 lines (49 loc) · 1.89 KB
/
helper.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
from nose.tools import ok_, eq_
__author__ = 'sukrit'
def dict_compare(actual, expected, key_path='',):
"""
:param actual:
:param expected:
:param nested_path:
:return: None
>>> dict_compare('A', 'B')
Traceback (most recent call last):
...
AssertionError: Actual 'A' != Expected 'B' at path: ''
>>> dict_compare({}, 'B')
Traceback (most recent call last):
...
AssertionError: Actual {} != Expected 'B' at path: ''
>>> dict_compare({}, {})
True
>>> dict_compare({'key1':'value1'}, {'key2':'value2'})
Traceback (most recent call last):
...
AssertionError: Key 'key1' was not expected at path: ''
>>> dict_compare({'key1':'value1', 'key2': 'value2'},
... {'key2':'value2', 'key1': 'value1'})
True
>>> dict_compare({'key1':{'key1.1': 'value1.1', 'key1.2': 'value1.2'}},
... {'key1':{'key1.2': 'value1.2', 'key1.1': 'value1.1'}})
True
>>> dict_compare({'key1':'value1'}, {'key2':'value2', 'key1':'value1'})
Traceback (most recent call last):
...
AssertionError: Key 'key2' was not found in actual at path: ''
>>> dict_compare({'key1':'value1'}, {'key1':'value2'})
Traceback (most recent call last):
...
AssertionError: Actual 'value1' != Expected 'value2' at path: '.key1'
"""
if isinstance(actual, dict) and isinstance(expected, dict):
for key in actual:
ok_(key in expected, 'Key \'%s\' was not expected at '
'path: \'%s\'' % (key, key_path))
dict_compare(actual[key], expected[key], '%s.%s' % (key_path, key))
for key in expected:
ok_(key in actual, 'Key \'%s\' was not found in actual at '
'path: \'%s\'' % (key, key_path))
else:
eq_(actual, expected, 'Actual %r != Expected %r at path: \'%s\''
% (actual, expected, key_path))
return True