-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathtest_operators.py
428 lines (338 loc) · 15.4 KB
/
test_operators.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import re
import math
import pytest
from copy import deepcopy
from typing import List, Any
from deepdiff import DeepDiff
from deepdiff.operator import BaseOperator, PrefixOrSuffixOperator, BaseOperatorPlus
class TestOperators:
def test_custom_operators_prevent_default(self):
t1 = {
"coordinates": [
{"x": 5, "y": 5},
{"x": 8, "y": 8}
]
}
t2 = {
"coordinates": [
{"x": 6, "y": 6},
{"x": 88, "y": 88}
]
}
class L2DistanceDifferWithPreventDefault(BaseOperator):
def __init__(self, regex_paths: List[str], distance_threshold: float):
super().__init__(regex_paths)
self.distance_threshold = distance_threshold
def _l2_distance(self, c1, c2):
return math.sqrt(
(c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2
)
def give_up_diffing(self, level, diff_instance) -> bool:
l2_distance = self._l2_distance(level.t1, level.t2)
if l2_distance > self.distance_threshold:
diff_instance.custom_report_result('distance_too_far', level, {
"l2_distance": l2_distance
})
return True
ddiff = DeepDiff(t1, t2, custom_operators=[L2DistanceDifferWithPreventDefault(
["^root\\['coordinates'\\]\\[\\d+\\]$"],
1
)])
expected = {
'distance_too_far': {
"root['coordinates'][0]": {'l2_distance': 1.4142135623730951},
"root['coordinates'][1]": {'l2_distance': 113.13708498984761}
}
}
assert expected == ddiff
def test_custom_operators_not_prevent_default(self):
t1 = {
"coordinates": [
{"x": 5, "y": 5},
{"x": 8, "y": 8}
]
}
t2 = {
"coordinates": [
{"x": 6, "y": 6},
{"x": 88, "y": 88}
]
}
class L2DistanceDifferWithPreventDefault(BaseOperator):
def __init__(self, regex_paths, distance_threshold):
super().__init__(regex_paths)
self.distance_threshold = distance_threshold
def _l2_distance(self, c1, c2):
return math.sqrt(
(c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2
)
def give_up_diffing(self, level, diff_instance) -> bool:
l2_distance = self._l2_distance(level.t1, level.t2)
if l2_distance > self.distance_threshold:
diff_instance.custom_report_result('distance_too_far', level, {
"l2_distance": l2_distance
})
#
return False
ddiff = DeepDiff(t1, t2, custom_operators=[L2DistanceDifferWithPreventDefault(
["^root\\['coordinates'\\]\\[\\d+\\]$"],
1
)
])
expected = {
'values_changed': {
"root['coordinates'][0]['x']": {'new_value': 6, 'old_value': 5},
"root['coordinates'][0]['y']": {'new_value': 6, 'old_value': 5},
"root['coordinates'][1]['x']": {'new_value': 88, 'old_value': 8},
"root['coordinates'][1]['y']": {'new_value': 88, 'old_value': 8}
},
'distance_too_far': {
"root['coordinates'][0]": {'l2_distance': 1.4142135623730951},
"root['coordinates'][1]": {'l2_distance': 113.13708498984761}
}
}
assert expected == ddiff
def test_custom_operators_should_not_equal(self):
t1 = {
"id": 5,
"expect_change_pos": 10,
"expect_change_neg": 10,
}
t2 = {
"id": 5,
"expect_change_pos": 100,
"expect_change_neg": 10,
}
class ExpectChangeOperator(BaseOperator):
def __init__(self, regex_paths):
super().__init__(regex_paths)
def give_up_diffing(self, level, diff_instance) -> bool:
if level.t1 == level.t2:
diff_instance.custom_report_result('unexpected:still', level, {
"old": level.t1,
"new": level.t2
})
return True
ddiff = DeepDiff(t1, t2, custom_operators=[
ExpectChangeOperator(regex_paths=["root\\['expect_change.*'\\]"])
])
assert ddiff == {'unexpected:still': {"root['expect_change_neg']": {'old': 10, 'new': 10}}}
def test_custom_operator2(self):
class CustomClass:
def __init__(self, d: dict, l: list):
self.dict = d
self.dict['list'] = l
def __repr__(self):
return "Class list is " + str(self.dict['list'])
custom1 = CustomClass(d=dict(a=1, b=2), l=[1, 2, 3])
custom2 = CustomClass(d=dict(c=3, d=4), l=[1, 2, 3, 2])
custom3 = CustomClass(d=dict(a=1, b=2), l=[1, 2, 3, 4])
class ListMatchOperator(BaseOperator):
def give_up_diffing(self, level, diff_instance) -> bool:
if set(level.t1.dict['list']) == set(level.t2.dict['list']):
return True
return False
ddiff = DeepDiff(custom1, custom2, custom_operators=[
ListMatchOperator(types=[CustomClass])
])
assert {} == ddiff
ddiff2 = DeepDiff(custom2, custom3, threshold_to_diff_deeper=0, custom_operators=[
ListMatchOperator(types=[CustomClass])
])
expected = {
'dictionary_item_added': ["root.dict['a']", "root.dict['b']"],
'dictionary_item_removed': ["root.dict['c']", "root.dict['d']"],
'values_changed': {"root.dict['list'][3]": {'new_value': 4, 'old_value': 2}}}
assert expected == ddiff2
def test_include_only_certain_path(self):
class MyOperator:
def __init__(self, include_paths):
self.include_paths = include_paths
def match(self, level) -> bool:
return True
def give_up_diffing(self, level, diff_instance) -> bool:
return level.path() not in self.include_paths
t1 = {'a': [10, 11], 'b': [20, 21], 'c': [30, 31]}
t2 = {'a': [10, 22], 'b': [20, 33], 'c': [30, 44]}
ddiff = DeepDiff(t1, t2, custom_operators=[
MyOperator(include_paths="root['a'][1]")
])
expected = {'values_changed': {"root['a'][1]": {'new_value': 22, 'old_value': 11}}}
assert expected == ddiff
def test_give_up_diffing_on_first_diff(self):
class MyOperator:
def match(self, level) -> bool:
return True
def give_up_diffing(self, level, diff_instance) -> bool:
return any(diff_instance.tree.values())
t1 = [[1, 2], [3, 4], [5, 6]]
t2 = [[1, 3], [3, 5], [5, 7]]
ddiff = DeepDiff(t1, t2, custom_operators=[
MyOperator()
])
expected = {'values_changed': {'root[0][1]': {'new_value': 3, 'old_value': 2}}}
assert expected == ddiff
def test_prefix_or_suffix_diff(self):
t1 = {
"key1": ["foo", "bar's food", "jack", "joe"]
}
t2 = {
"key1": ["foo", "bar", "jill", "joe'car"]
}
ddiff = DeepDiff(t1, t2, custom_operators=[
PrefixOrSuffixOperator()
])
expected = {'values_changed': {"root['key1'][2]": {'new_value': 'jill', 'old_value': 'jack'}}}
assert expected == ddiff
with pytest.raises(NotImplementedError) as exp:
DeepDiff(t1, t2, ignore_order=True, custom_operators=[
PrefixOrSuffixOperator()
])
expected2 = 'PrefixOrSuffixOperator needs to define a normalize_value_for_hashing method to be compatible with ignore_order=True or iterable_compare_func.'
assert expected2 == str(exp.value)
def test_custom_operator3_small_numbers(self):
x = [2.0000000000000027, 2.500000000000005, 2.000000000000002, 3.000000000000001]
y = [2.000000000000003, 2.500000000000005, 2.0000000000000027, 3.0000000000000027]
result = DeepDiff(x, y)
expected = {
'values_changed': {
'root[0]': {'new_value': 2.000000000000003, 'old_value': 2.0000000000000027},
'root[2]': {'new_value': 2.0000000000000027, 'old_value': 2.000000000000002},
'root[3]': {'new_value': 3.0000000000000027, 'old_value': 3.000000000000001}}}
assert expected == result
class CustomCompare(BaseOperatorPlus):
def __init__(self, tolerance, types):
self.tolerance = tolerance
self.types = types
def match(self, level) -> bool:
if type(level.t1) in self.types:
return True
return False
def give_up_diffing(self, level, diff_instance) -> bool:
relative = abs(abs(level.t1 - level.t2) / level.t1)
if not max(relative, self.tolerance) == self.tolerance:
custom_report = f'relative diff: {relative:.8e}'
diff_instance.custom_report_result('diff', level, custom_report)
return True
def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any:
return obj
def compare_func(x, y, level):
return True
operators = [CustomCompare(types=[float], tolerance=5.5e-5)]
result2 = DeepDiff(x, y, custom_operators=operators, iterable_compare_func=compare_func)
assert {} == result2
result3 = DeepDiff(x, y, custom_operators=operators, zip_ordered_iterables=True)
assert {} == result3, "We should get the same result as result2 when zip_ordered_iterables is True."
def test_custom_operator_and_ignore_order1_using_base_operator_plus(self):
d1 = {
"Name": "SUB_OBJECT_FILES",
"Values": {
"Value": [
"{f254498b-b752-4f35-bef5-6f1844b61eb7}",
"{7fb2a550-1849-45c0-b273-9aa5e4eb9f2b}",
"{3a614c62-4252-48eb-b279-1450ee8af182}",
"{208f22c4-c256-4311-9a45-e6c37d343458}",
"{1fcf5d37-ef19-43a7-a1ad-d17c7c1713c6}",
]
}
}
d2 = {
"Name": "SUB_OBJECT_FILES",
"Values": {
"Value": [
"{e5d18917-1a2c-4abe-b601-8ec002629953}",
"{ea71ba1f-1339-4fae-bc28-a9ce9b8a8c67}",
"{66bb6192-9cd2-4074-8be1-f2ac52877c70}",
"{0c88b900-3755-4d10-93ef-b6a96dbcba90}",
"{e39fdfc5-be6c-4f97-9345-9a8286381fe7}"
]
}
}
class RemoveGUIDsOperator(BaseOperatorPlus):
_pattern = r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
_substitute = "guid"
def match(self, level) -> bool:
return isinstance(level.t1, str) and isinstance(level.t2, str)
@classmethod
def _remove_pattern(cls, t: str):
return re.sub(cls._pattern, cls._substitute, t)
def give_up_diffing(self, level, diff_instance):
t1 = self._remove_pattern(level.t1)
t2 = self._remove_pattern(level.t2)
return t1 == t2
def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any:
"""
Used for ignore_order=True
"""
if isinstance(obj, str):
return self._remove_pattern(obj)
return obj
operator = RemoveGUIDsOperator()
diff1 = DeepDiff(d1, d2, custom_operators=[operator], log_stacktrace=True)
assert not diff1
diff2 = DeepDiff(d1, d2, ignore_order=True, custom_operators=[operator], log_stacktrace=True)
assert not diff2
def test_custom_operator_and_ignore_order2(self):
d1 = {
"Entity": {
"Property": {
"Name": "SUB_OBJECT_FILES",
"Values": {
"Value": [
"{f254498b-b752-4f35-bef5-6f1844b61eb7}",
"{7fb2a550-1849-45c0-b273-9aa5e4eb9f2b}",
"{3a614c62-4252-48eb-b279-1450ee8af182}",
"{208f22c4-c256-4311-9a45-e6c37d343458}",
"{1fcf5d37-ef19-43a7-a1ad-d17c7c1713c6}",
"{a9cbecc0-21dc-49ce-8b2c-d36352dae139}"
]
}
}
}
}
d2 = {
"Entity": {
"Property": {
"Name": "SUB_OBJECT_FILES",
"Values": {
"Value": [
"{e5d18917-1a2c-4abe-b601-8ec002629953}",
"{ea71ba1f-1339-4fae-bc28-a9ce9b8a8c67}",
"{d7778018-a7b5-4246-8caa-f590138d99e5}",
"{66bb6192-9cd2-4074-8be1-f2ac52877c70}",
"{0c88b900-3755-4d10-93ef-b6a96dbcba90}",
"{e39fdfc5-be6c-4f97-9345-9a8286381fe7}"
]
}
}
}
}
class RemovePatternOperator(BaseOperator):
_pattern: str = ""
_substitute: str = ""
@classmethod
def _remove_pattern(cls, t: str):
return re.sub(cls._pattern, cls._substitute, t)
def give_up_diffing(self, level, diff_instance):
if isinstance(level.t1, str) and isinstance(level.t2, str):
t1 = self._remove_pattern(level.t1)
t2 = self._remove_pattern(level.t2)
return t1 == t2
return False
class RemoveGUIDsOperator(RemovePatternOperator):
_pattern = r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
_substitute = "guid"
diff1 = DeepDiff(deepcopy(d1), deepcopy(d2), ignore_order=False, custom_operators=[RemoveGUIDsOperator(types=[str])])
assert not diff1
with pytest.raises(NotImplementedError) as exp:
DeepDiff(deepcopy(d1), deepcopy(d2), ignore_order=True, custom_operators=[RemoveGUIDsOperator(types=[str])])
expected2 = 'RemoveGUIDsOperator needs to define a normalize_value_for_hashing method to be compatible with ignore_order=True or iterable_compare_func.'
assert expected2 == str(exp.value)
# --------- Let's implement the normalize_value_for_hashing to make it work with ignore_order=True ---------
class RemoveGUIDsOperatorIgnoreOrderReady(RemoveGUIDsOperator):
def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any:
if isinstance(obj, str):
return self._remove_pattern(obj)
return obj
diff3 = DeepDiff(deepcopy(d1), deepcopy(d2), ignore_order=True, custom_operators=[RemoveGUIDsOperatorIgnoreOrderReady(types=[str])])
assert not diff3, "We shouldn't have a diff because we have normalized the string values to be all the same vlues."