forked from inveniosoftware/dictdiffer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
141 lines (113 loc) · 4 KB
/
tests.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import unittest
from dictdiffer import diff, patch, revert, swap
class DictDifferTests(unittest.TestCase):
def test_addition(self):
first = {}
second = {'a': 'b'}
diffed = next(diff(first, second))
assert ('add', '', [('a', 'b')]) == diffed
def test_deletion(self):
first = {'a': 'b'}
second = {}
diffed = next(diff(first, second))
assert ('remove', '', [('a', 'b')]) == diffed
def test_change(self):
first = {'a': 'b'}
second = {'a': 'c'}
diffed = next(diff(first, second))
assert ('change', 'a', ('b', 'c')) == diffed
first = {'a': None}
second = {'a': 'c'}
diffed = next(diff(first, second))
assert ('change', 'a', (None, 'c')) == diffed
first = {'a': 'b'}
second = {'a': None}
diffed = next(diff(first, second))
assert ('change', 'a', ('b', None)) == diffed
def test_nodes(self):
first = {'a': {'b': {'c': 'd'}}}
second = {'a': {'b': {'c': 'd', 'e': 'f'}}}
diffed = next(diff(first, second))
assert ('add', 'a.b', [('e', 'f')]) == diffed
def test_push(self):
first = {'a': []}
second = {'a': ['b']}
diffed = next(diff(first, second))
assert ('push', 'a', ['b']) == diffed
def test_pull(self):
first = {'a': ['b']}
second = {'a': []}
diffed = next(diff(first, second))
assert ('pull', 'a', ['b']) == diffed
class DiffPatcherTests(unittest.TestCase):
def test_addition(self):
first = {}
second = {'a': 'b'}
assert second == patch(
[('add', '', [('a', 'b')])], first)
first = {'a': {'b': 'c'}}
second = {'a': {'b': 'c', 'd': 'e'}}
assert second == patch(
[('add', 'a', [('d', 'e')])], first)
def test_changes(self):
first = {'a': 'b'}
second = {'a': 'c'}
assert second == patch(
[('change', 'a', ('b', 'c'))], first)
first = {'a': {'b': {'c': 'd'}}}
second = {'a': {'b': {'c': 'e'}}}
assert second == patch(
[('change', 'a.b.c', ('d', 'e'))], first)
def test_remove(self):
first = {'a': {'b': 'c'}}
second = {'a': {}}
assert second == patch(
[('remove', 'a', [('b', 'c')])], first)
first = {'a': 'b'}
second = {}
assert second == patch(
[('remove', '', [('a', 'b')])], first)
def test_pull(self):
first = {'a': [1, 2, 3]}
second = {'a': [1, 2]}
assert second == patch(
[('pull', 'a', [3])], first)
def test_push(self):
first = {'a': [1]}
second = {'a': [1, 2]}
assert second == patch(
[('push', 'a', [2])], first)
first = {'a': {'b': [1]}}
second = {'a': {'b': [1, 2]}}
assert second == patch(
[('push', 'a.b', [2])], first)
class SwapperTests(unittest.TestCase):
def test_addition(self):
result = 'add', '', [('a', 'b')]
swapped = 'remove', '', [('a', 'b')]
assert next(swap([result])) == swapped
result = 'remove', 'a.b', [('c', 'd')]
swapped = 'add', 'a.b', [('c', 'd')]
assert next(swap([result])) == swapped
def test_changes(self):
result = 'change', '', ('a', 'b')
swapped = 'change', '', ('b', 'a')
assert next(swap([result])) == swapped
def test_lists(self):
result = 'pull', '', [1, 2]
swapped = 'push', '', [1, 2]
assert next(swap([result])) == swapped
result = 'push', '', [1, 2]
swapped = 'pull', '', [1, 2]
assert next(swap([result])) == swapped
def test_revert(self):
first = {'a': 'b'}
second = {'a': 'c'}
diffed = diff(first, second)
patched = patch(diffed, first)
assert patched == second
diffed = diff(first, second)
reverted = revert(diffed, second)
assert reverted == first
if __name__ == "__main__":
unittest.main()