Debian Codegraph @

tests/test_diff_include_paths.py

raw
1 import pytest
2 from deepdiff import DeepDiff
3
4 t1 = {
5 "foo": {
6 "bar": {
7 "veg": "potato",
8 "fruit": "apple"
9 }
10 },
11 "ingredients": [
12 {
13 "lunch": [
14 "bread",
15 "cheese"
16 ]
17 },
18 {
19 "dinner": [
20 "soup",
21 "meat"
22 ]
23 }
24 ]
25 }
26 t2 = {
27 "foo": {
28 "bar": {
29 "veg": "potato",
30 "fruit": "peach"
31 }
32 },
33 "ingredients": [
34 {
35 "lunch": [
36 "bread",
37 "cheese"
38 ]
39 },
40 {
41 "dinner": [
42 "soup",
43 "meat"
44 ]
45 }
46 ]
47 }
48
49
50 class TestDeepDiffIncludePaths:
51
52 @staticmethod
53 def deep_diff(dict1, dict2, include_paths):
54 diff = DeepDiff(dict1, dict2, include_paths=include_paths)
55 print(diff)
56 return diff
57
58 def test_include_paths_root_neg(self):
59 expected = {'values_changed': {"root['foo']['bar']['fruit']": {'new_value': 'peach', 'old_value': 'apple'}}}
60 actual = self.deep_diff(t1, t2, 'foo')
61 assert expected == actual
62
63 def test_include_paths_root_pos(self):
64 expected = {}
65 actual = self.deep_diff(t1, t2, 'ingredients')
66 assert expected == actual
67
68 def test_include_paths_nest00_neg(self):
69 expected = {'values_changed': {"root['foo']['bar']['fruit']": {'new_value': 'peach', 'old_value': 'apple'}}}
70 actual = self.deep_diff(t1, t2, "root['foo']['bar']")
71 assert expected == actual
72
73 def test_include_paths_nest01_neg(self):
74 expected = {'values_changed': {"root['foo']['bar']['fruit']": {'new_value': 'peach', 'old_value': 'apple'}}}
75 actual = self.deep_diff(t1, t2, "root['foo']['bar']['fruit']")
76 assert expected == actual
77
78 def test_include_paths_nest_pos(self):
79 expected = {}
80 actual = self.deep_diff(t1, t2, "root['foo']['bar']['veg']")
81 assert expected == actual
82
83 @pytest.mark.parametrize(
84 "test_num, data",
85 [
86 (
87 1, # test_num
88 {
89 "old": {
90 'name': 'Testname Old',
91 'desciption': 'Desc Old',
92 'sub_path': {
93 'name': 'Testname Subpath old',
94 'desciption': 'Desc Subpath old',
95 },
96 },
97 "new": {
98 'name': 'Testname New',
99 'desciption': 'Desc New',
100 'new_attribute': 'New Value',
101 'sub_path': {
102 'name': 'Testname Subpath old',
103 'desciption': 'Desc Subpath old',
104 },
105 },
106 "include_paths": "root['sub_path']",
107 "expected_result1": {'dictionary_item_added': ["root['new_attribute']"], 'values_changed': {"root['name']": {'new_value': 'Testname New', 'old_value': 'Testname Old'}, "root['desciption']": {'new_value': 'Desc New', 'old_value': 'Desc Old'}}},
108 "expected_result2": {},
109 },
110 ),
111 (
112 2, # test_num
113 {
114 "old": {
115 'name': 'Testname Old',
116 'desciption': 'Desc Old',
117 'sub_path': {
118 'name': 'Testname Subpath old',
119 'desciption': 'Desc Subpath old',
120 },
121 },
122 "new": {
123 'name': 'Testname New',
124 'desciption': 'Desc New',
125 'new_attribute': 'New Value',
126 'sub_path': {
127 'name': 'Testname Subpath New',
128 'desciption': 'Desc Subpath old',
129 },
130 },
131 "include_paths": "root['sub_path']",
132 "expected_result1": {'dictionary_item_added': ["root['new_attribute']"], 'values_changed': {"root['name']": {'new_value': 'Testname New', 'old_value': 'Testname Old'}, "root['desciption']": {'new_value': 'Desc New', 'old_value': 'Desc Old'}, "root['sub_path']['name']": {'new_value': 'Testname Subpath New', 'old_value': 'Testname Subpath old'}}},
133 "expected_result2": {"values_changed": {"root['sub_path']['name']": {"old_value": "Testname Subpath old", "new_value": "Testname Subpath New"}}},
134 },
135 ),
136 (
137 3, # test_num
138 {
139 "old": {
140 'name': 'Testname Old',
141 'desciption': 'Desc Old',
142 'sub_path': {
143 'name': 'Testname Subpath old',
144 'desciption': 'Desc Subpath old',
145 'old_attr': 'old attr value',
146 },
147 },
148 "new": {
149 'name': 'Testname New',
150 'desciption': 'Desc New',
151 'new_attribute': 'New Value',
152 'sub_path': {
153 'name': 'Testname Subpath old',
154 'desciption': 'Desc Subpath New',
155 'new_sub_path_attr': 'new sub path attr value',
156 },
157 },
158 "include_paths": "root['sub_path']['name']",
159 "expected_result1": {'dictionary_item_added': ["root['new_attribute']", "root['sub_path']['new_sub_path_attr']"], 'dictionary_item_removed': ["root['sub_path']['old_attr']"], 'values_changed': {"root['name']": {'new_value': 'Testname New', 'old_value': 'Testname Old'}, "root['desciption']": {'new_value': 'Desc New', 'old_value': 'Desc Old'}, "root['sub_path']['desciption']": {'new_value': 'Desc Subpath New', 'old_value': 'Desc Subpath old'}}},
160 "expected_result2": {},
161 },
162 ),
163 (
164 4, # test_num
165 {
166 "old": {
167 'name': 'Testname old',
168 'desciption': 'Desc old',
169 'new_attribute': 'old Value',
170 'sub_path': {
171 'name': 'Testname',
172 'removed_attr': 'revemod attr value',
173 },
174 },
175 "new": {
176 'name': 'Testname new',
177 'desciption': 'Desc new',
178 'new_attribute': 'new Value',
179 'sub_path': {
180 'added_attr': 'Added Attr Value',
181 'name': 'Testname',
182 },
183 },
184 "include_paths": "root['sub_path']['name']",
185 "expected_result1": {'dictionary_item_added': ["root['sub_path']['added_attr']"], 'dictionary_item_removed': ["root['sub_path']['removed_attr']"], 'values_changed': {"root['name']": {'new_value': 'Testname new', 'old_value': 'Testname old'}, "root['desciption']": {'new_value': 'Desc new', 'old_value': 'Desc old'}, "root['new_attribute']": {'new_value': 'new Value', 'old_value': 'old Value'}}},
186 "expected_result2": {},
187 },
188 ),
189 (
190 5, # test_num
191 {
192 "old": {
193 'name': 'Testname',
194 'removed_attr': 'revemod attr value',
195 },
196 "new": {
197 'added_attr': 'Added Attr Value',
198 'name': 'Testname',
199 },
200 "include_paths": "root['name']",
201 "expected_result1": {'dictionary_item_added': ["root['added_attr']"], 'dictionary_item_removed': ["root['removed_attr']"]},
202 "expected_result2": {},
203 },
204 ),
205 (
206 6, # test_num
207 {
208 "old": {
209 'name': 'Testname',
210 'removed_attr': 'revemod attr value',
211 'removed_attr_2': 'revemod attr value',
212 },
213 "new": {
214 'added_attr': 'Added Attr Value',
215 'name': 'Testname',
216 },
217 "include_paths": "root['name']",
218 "expected_result1": {'values_changed': {'root': {'new_value': {'added_attr': 'Added Attr Value', 'name': 'Testname'}, 'old_value': {'name': 'Testname', 'removed_attr': 'revemod attr value', 'removed_attr_2': 'revemod attr value'}}}},
219 "expected_result2": {},
220 },
221 ),
222 (
223 7, # test_num
224 {
225 "old": {
226 'name': 'Testname old',
227 'desciption': 'Desc old',
228 'new_attribute': 'old Value',
229 'sub_path': {
230 'name': 'Testname',
231 'removed_attr': 'revemod attr value',
232 'removed_attr_2': 'blu',
233 },
234 },
235 "new": {
236 'name': 'Testname new',
237 'desciption': 'Desc new',
238 'new_attribute': 'new Value',
239 'sub_path': {
240 'added_attr': 'Added Attr Value',
241 'name': 'Testname',
242 },
243 },
244 "include_paths": "root['sub_path']['name']",
245 "expected_result1": {'values_changed': {"root['name']": {'new_value': 'Testname new', 'old_value': 'Testname old'}, "root['desciption']": {'new_value': 'Desc new', 'old_value': 'Desc old'}, "root['new_attribute']": {'new_value': 'new Value', 'old_value': 'old Value'}, "root['sub_path']": {'new_value': {'added_attr': 'Added Attr Value', 'name': 'Testname'}, 'old_value': {'name': 'Testname', 'removed_attr': 'revemod attr value', 'removed_attr_2': 'blu'}}}},
246 "expected_result2": {},
247 },
248 ),
249 (
250 8, # test_num
251 {
252 "old": [{
253 'name': 'Testname old',
254 'desciption': 'Desc old',
255 'new_attribute': 'old Value',
256 'sub_path': {
257 'name': 'Testname',
258 'removed_attr': 'revemod attr value',
259 'removed_attr_2': 'blu',
260 },
261 }],
262 "new": [{
263 'name': 'Testname new',
264 'desciption': 'Desc new',
265 'new_attribute': 'new Value',
266 'sub_path': {
267 'added_attr': 'Added Attr Value',
268 'name': 'New Testname',
269 },
270 }],
271 "include_paths": "root[0]['sub_path']['name']",
272 "expected_result1": {'values_changed': {"root[0]['name']": {'new_value': 'Testname new', 'old_value': 'Testname old'}, "root[0]['desciption']": {'new_value': 'Desc new', 'old_value': 'Desc old'}, "root[0]['new_attribute']": {'new_value': 'new Value', 'old_value': 'old Value'}, "root[0]['sub_path']": {'new_value': {'added_attr': 'Added Attr Value', 'name': 'New Testname'}, 'old_value': {'name': 'Testname', 'removed_attr': 'revemod attr value', 'removed_attr_2': 'blu'}}}},
273 "expected_result2": {'values_changed': {"root[0]['sub_path']['name']": {'new_value': 'New Testname', 'old_value': 'Testname'}}},
274 },
275 ),
276 ]
277 )
278 def test_diff_include_paths_root(self, test_num, data):
279 diff1 = DeepDiff(data["old"], data["new"])
280 diff2 = DeepDiff(data["old"], data["new"], include_paths=data["include_paths"])
281 assert data['expected_result1'] == diff1, f"test_diff_include_paths_root test_num #{test_num} failed."
282 assert data['expected_result2'] == diff2, f"test_diff_include_paths_root test_num #{test_num} failed."