| 1 |
import pytest |
| 2 |
import re |
| 3 |
import datetime |
| 4 |
from unittest import mock |
| 5 |
from deepdiff.helper import number_to_string, CannotCompare |
| 6 |
from deepdiff import DeepDiff |
| 7 |
from decimal import Decimal |
| 8 |
from deepdiff.deephash import sha256hex |
| 9 |
from tests import CustomClass2 |
| 10 |
|
| 11 |
|
| 12 |
class TestIgnoreOrder: |
| 13 |
|
| 14 |
@pytest.mark.parametrize("t1, t2, significant_digits, ignore_order, result", [ |
| 15 |
(10, 10.0, 5, False, {}), |
| 16 |
({10: 'a', 11.1: 'b'}, {10.0: 'a', Decimal('11.1000003'): 'b'}, 5, False, {}), |
| 17 |
]) |
| 18 |
def test_type_change_numeric_ignored(self, t1, t2, significant_digits, ignore_order, result): |
| 19 |
ddiff = DeepDiff(t1, t2, ignore_numeric_type_changes=True, |
| 20 |
significant_digits=significant_digits, ignore_order=ignore_order) |
| 21 |
assert result == ddiff |
| 22 |
|
| 23 |
@pytest.mark.parametrize("t1, t2, expected_result", |
| 24 |
[ |
| 25 |
(10, 10.0, {}), |
| 26 |
(10, 10.2, {'values_changed': {'root': {'new_value': 10.2, 'old_value': 10}}}), |
| 27 |
(Decimal(10), 10.0, {}), |
| 28 |
({"a": Decimal(10), "b": 12, 11.0: None}, {b"b": 12, "a": 10.0, Decimal(11): None}, {}), |
| 29 |
]) |
| 30 |
def test_type_change_numeric_when_ignore_order(self, t1, t2, expected_result): |
| 31 |
ddiff = DeepDiff(t1, t2, ignore_order=True, ignore_numeric_type_changes=True, ignore_string_type_changes=True, threshold_to_diff_deeper=0) |
| 32 |
assert expected_result == ddiff |
| 33 |
|
| 34 |
def test_ignore_order_depth1(self): |
| 35 |
t1 = [{1, 2, 3}, {4, 5}] |
| 36 |
t2 = [{4, 5, 6}, {1, 2, 3}] |
| 37 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 38 |
assert {'set_item_added': ["root[1][6]"]} == ddiff |
| 39 |
|
| 40 |
def test_ignore_order_depth2(self): |
| 41 |
t1 = [[1, 2, 3], [4, 5]] |
| 42 |
t2 = [[4, 5, 6], [1, 2, 3]] |
| 43 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 44 |
assert {'iterable_item_added': {'root[1][2]': 6}} == ddiff |
| 45 |
|
| 46 |
def test_ignore_order_depth3(self): |
| 47 |
t1 = [{1, 2, 3}, [{4, 5}]] |
| 48 |
t2 = [[{4, 5, 6}], {1, 2, 3}] |
| 49 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 50 |
assert {'set_item_added': ["root[1][0][6]"]} == ddiff |
| 51 |
assert {"root[1][0][6]"} == ddiff.affected_paths |
| 52 |
|
| 53 |
def test_ignore_order_depth4(self): |
| 54 |
t1 = [[1, 2, 3, 4], [4, 2, 2, 1]] |
| 55 |
t2 = [[4, 1, 1, 1], [1, 3, 2, 4]] |
| 56 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 57 |
assert {'iterable_item_removed': {'root[1][1]': 2}} == ddiff |
| 58 |
|
| 59 |
def test_ignore_order_depth5(self): |
| 60 |
t1 = [4, 2, 2, 1] |
| 61 |
t2 = [4, 1, 1, 1] |
| 62 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, cache_purge_level=0) |
| 63 |
expected = { |
| 64 |
'iterable_item_removed': { |
| 65 |
'root[1]': 2, |
| 66 |
'root[2]': 2 |
| 67 |
}, |
| 68 |
'repetition_change': { |
| 69 |
'root[3]': { |
| 70 |
'old_repeat': 1, |
| 71 |
'new_repeat': 3, |
| 72 |
'old_indexes': [3], |
| 73 |
'new_indexes': [1, 2, 3], |
| 74 |
'value': 1 |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
assert expected == ddiff |
| 79 |
assert {"root[1]", "root[2]", "root[3]"} == ddiff.affected_paths |
| 80 |
|
| 81 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=False, cache_purge_level=0) |
| 82 |
dist = ddiff._get_rough_distance() |
| 83 |
assert 0.1 == dist |
| 84 |
|
| 85 |
def test_ignore_order_depth6(self): |
| 86 |
t1 = [[1, 2, 3, 4], [4, 2, 2, 1]] |
| 87 |
t2 = [[4, 1, 1, 1], [1, 3, 2, 4]] |
| 88 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 89 |
expected = { |
| 90 |
'iterable_item_removed': { |
| 91 |
'root[1][1]': 2, |
| 92 |
'root[1][2]': 2 |
| 93 |
}, |
| 94 |
'repetition_change': { |
| 95 |
'root[1][3]': { |
| 96 |
'old_repeat': 1, |
| 97 |
'new_repeat': 3, |
| 98 |
'old_indexes': [3], |
| 99 |
'new_indexes': [1, 2, 3], |
| 100 |
'value': 1 |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
assert expected == ddiff |
| 106 |
|
| 107 |
def test_list_difference_ignore_order(self): |
| 108 |
t1 = {1: 1, 4: {"a": "hello", "b": [1, 2, 3]}} |
| 109 |
t2 = {1: 1, 4: {"a": "hello", "b": [1, 3, 2, 3]}} |
| 110 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 111 |
assert {} == ddiff |
| 112 |
|
| 113 |
@pytest.mark.parametrize('t1_0, t2_0', [ |
| 114 |
(1, 2), |
| 115 |
(True, False), |
| 116 |
('a', 'b'), |
| 117 |
]) |
| 118 |
def test_list_difference_of_bool_only_ignore_order(self, t1_0, t2_0): |
| 119 |
t1 = [t1_0] |
| 120 |
t2 = [t2_0] |
| 121 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 122 |
result = {'values_changed': {'root[0]': {'new_value': t2_0, 'old_value': t1_0}}} |
| 123 |
assert result == ddiff |
| 124 |
|
| 125 |
def test_dictionary_difference_ignore_order(self): |
| 126 |
t1 = {"a": [[{"b": 2, "c": 4}, {"b": 2, "c": 3}]]} |
| 127 |
t2 = {"a": [[{"b": 2, "c": 3}, {"b": 2, "c": 4}]]} |
| 128 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 129 |
assert {} == ddiff |
| 130 |
assert set() == ddiff.affected_paths |
| 131 |
|
| 132 |
def test_nested_list_ignore_order(self): |
| 133 |
t1 = [1, 2, [3, 4]] |
| 134 |
t2 = [[4, 3, 3], 2, 1] |
| 135 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 136 |
assert {} == ddiff |
| 137 |
|
| 138 |
def test_nested_list_difference_ignore_order(self): |
| 139 |
t1 = [1, 2, [3, 4]] |
| 140 |
t2 = [[4, 3], 2, 1] |
| 141 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 142 |
assert {} == ddiff |
| 143 |
|
| 144 |
def test_nested_list_with_dictionarry_difference_ignore_order(self): |
| 145 |
t1 = [1, 2, [3, 4, {1: 2}]] |
| 146 |
t2 = [[4, 3, {1: 2}], 2, 1] |
| 147 |
|
| 148 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 149 |
|
| 150 |
result = {} |
| 151 |
assert result == ddiff |
| 152 |
|
| 153 |
def test_list_difference_ignore_order_report_repetition1(self): |
| 154 |
t1 = [1, 3, 1, 4] |
| 155 |
t2 = [4, 4, 1] |
| 156 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 157 |
result = { |
| 158 |
'iterable_item_removed': { |
| 159 |
'root[1]': 3 |
| 160 |
}, |
| 161 |
'repetition_change': { |
| 162 |
'root[0]': { |
| 163 |
'old_repeat': 2, |
| 164 |
'old_indexes': [0, 2], |
| 165 |
'new_indexes': [2], |
| 166 |
'value': 1, |
| 167 |
'new_repeat': 1 |
| 168 |
}, |
| 169 |
'root[3]': { |
| 170 |
'old_repeat': 1, |
| 171 |
'old_indexes': [3], |
| 172 |
'new_indexes': [0, 1], |
| 173 |
'value': 4, |
| 174 |
'new_repeat': 2 |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
assert result == ddiff |
| 179 |
|
| 180 |
@pytest.mark.skip |
| 181 |
def test_list_difference_ignore_order_report_repetition2(self): |
| 182 |
t1 = [1, 1, 1] |
| 183 |
t2 = [2, 2] |
| 184 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 185 |
result = {'values_changed': {'root[0]': {'new_value': 2, 'old_value': 1}}} |
| 186 |
assert result == ddiff |
| 187 |
|
| 188 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1) |
| 189 |
result2 = { |
| 190 |
'iterable_item_removed': { |
| 191 |
'root[0]': 1, |
| 192 |
'root[1]': 1, |
| 193 |
'root[2]': 1 |
| 194 |
}, |
| 195 |
'iterable_item_added': { |
| 196 |
'root[0]': 2, |
| 197 |
'root[1]': 2, |
| 198 |
}, |
| 199 |
} |
| 200 |
assert result2 == ddiff2 |
| 201 |
|
| 202 |
@pytest.mark.skip |
| 203 |
def test_list_difference_ignore_order_report_repetition3(self): |
| 204 |
t1 = [{"id": 1}, {"id": 1}, {"id": 1}] |
| 205 |
t2 = [{"id": 1, "name": 1}] |
| 206 |
|
| 207 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1) |
| 208 |
result2 = { |
| 209 |
'iterable_item_removed': { |
| 210 |
'root[1]': {"id": 1}, |
| 211 |
'root[2]': {"id": 1}, |
| 212 |
}, |
| 213 |
'dictionary_item_added': ["root[0]['name']"] |
| 214 |
} |
| 215 |
assert result2 == ddiff2 |
| 216 |
|
| 217 |
@pytest.mark.skip |
| 218 |
def test_list_difference_ignore_order_report_repetition4(self): |
| 219 |
t1 = [{"id": 1}, {"id": 1}, {"id": 1}, {"name": "Joe"}, {"name": "Joe"}] |
| 220 |
t2 = [{"id": 1, "name": 1}, {"id": 1, "name": "Joe"}] |
| 221 |
|
| 222 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1) |
| 223 |
result2 = { |
| 224 |
'iterable_item_removed': { |
| 225 |
'root[2]': {"id": 1}, |
| 226 |
'root[3]': {"name": "Joe"}, |
| 227 |
'root[4]': {"name": "Joe"}, |
| 228 |
}, |
| 229 |
'dictionary_item_added': ["root[0]['name']", "root[1]['name']"] |
| 230 |
} |
| 231 |
assert result2 == ddiff2 |
| 232 |
|
| 233 |
def test_nested_list_ignore_order_report_repetition(self): |
| 234 |
t1 = [1, 2, [3, 4]] |
| 235 |
t2 = [[4, 3, 3], 2, 1] |
| 236 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=False) |
| 237 |
assert not ddiff |
| 238 |
|
| 239 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 240 |
result = { |
| 241 |
'repetition_change': { |
| 242 |
'root[2][0]': { |
| 243 |
'old_repeat': 1, |
| 244 |
'new_repeat': 2, |
| 245 |
'old_indexes': [0], |
| 246 |
'new_indexes': [1, 2], |
| 247 |
'value': 3 |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
assert result == ddiff2 |
| 252 |
assert {"root[2][0]"} == ddiff2.affected_paths |
| 253 |
|
| 254 |
@pytest.mark.skip |
| 255 |
def test_nested_list_and_dict_ignore_order_report_repetition(self): |
| 256 |
""" |
| 257 |
This test shows that ignore order is not doing the right thing. |
| 258 |
|
| 259 |
It should have said that root[1] and root[2] are removed. |
| 260 |
""" |
| 261 |
t1 = [{"id": 1}, {"id": 1}, {"id": 1}] |
| 262 |
t2 = [{"id": 1, "name": 1}] |
| 263 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 264 |
result = {'dictionary_item_added': ["root[0]['name']"]} |
| 265 |
assert result == ddiff |
| 266 |
|
| 267 |
# Here there is nothing that is "repeated" in an iterable |
| 268 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 269 |
assert result == ddiff2 |
| 270 |
assert {"root[2][0]"} == ddiff2.affected_paths |
| 271 |
|
| 272 |
def test_list_of_unhashable_difference_ignore_order(self): |
| 273 |
t1 = [{"a": 2}, {"b": [3, 4, {1: 1}]}] |
| 274 |
t2 = [{"b": [3, 4, {1: 1}]}, {"a": 2}] |
| 275 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 276 |
assert {} == ddiff |
| 277 |
|
| 278 |
def test_list_of_unhashable_difference_ignore_order2(self): |
| 279 |
t1 = [1, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"] |
| 280 |
t2 = [{"b": [3, 4, {1: 1}]}, {"a": 2}, {1: 1}] |
| 281 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 282 |
result = { |
| 283 |
'iterable_item_added': { |
| 284 |
'root[2]': { |
| 285 |
1: 1 |
| 286 |
} |
| 287 |
}, |
| 288 |
'iterable_item_removed': { |
| 289 |
'root[3]': 'B', |
| 290 |
'root[0]': 1 |
| 291 |
} |
| 292 |
} |
| 293 |
assert result == ddiff |
| 294 |
|
| 295 |
def test_list_of_unhashable_difference_ignore_order3(self): |
| 296 |
t1 = [1, {"a": 2}, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"] |
| 297 |
t2 = [{"b": [3, 4, {1: 1}]}, {1: 1}] |
| 298 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 299 |
result = { |
| 300 |
'values_changed': { |
| 301 |
'root[1]': { |
| 302 |
'new_value': { |
| 303 |
1: 1 |
| 304 |
}, |
| 305 |
'old_value': { |
| 306 |
'a': 2 |
| 307 |
} |
| 308 |
} |
| 309 |
}, |
| 310 |
'iterable_item_removed': { |
| 311 |
'root[0]': 1, |
| 312 |
'root[4]': 'B' |
| 313 |
} |
| 314 |
} |
| 315 |
assert result == ddiff |
| 316 |
|
| 317 |
def test_list_of_unhashable_difference_ignore_order_report_repetition( |
| 318 |
self): |
| 319 |
t1 = [1, {"a": 2}, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"] |
| 320 |
t2 = [{"b": [3, 4, {1: 1}]}, {1: 1}] |
| 321 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, threshold_to_diff_deeper=0) |
| 322 |
result = { |
| 323 |
'iterable_item_added': { |
| 324 |
'root[1]': { |
| 325 |
1: 1 |
| 326 |
} |
| 327 |
}, |
| 328 |
'iterable_item_removed': { |
| 329 |
'root[4]': 'B', |
| 330 |
'root[0]': 1, |
| 331 |
'root[1]': { |
| 332 |
'a': 2 |
| 333 |
}, |
| 334 |
'root[2]': { |
| 335 |
'a': 2 |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
assert result == ddiff |
| 340 |
|
| 341 |
def test_list_of_unhashable_difference_ignore_order4(self): |
| 342 |
t1 = [{"a": 2}, {"a": 2}] |
| 343 |
t2 = [{"a": 2}] |
| 344 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 345 |
result = {} |
| 346 |
assert result == ddiff |
| 347 |
|
| 348 |
def test_list_of_unhashable_difference_ignore_order_report_repetition2( |
| 349 |
self): |
| 350 |
t1 = [{"a": 2}, {"a": 2}] |
| 351 |
t2 = [{"a": 2}] |
| 352 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 353 |
result = { |
| 354 |
'repetition_change': { |
| 355 |
'root[0]': { |
| 356 |
'old_repeat': 2, |
| 357 |
'new_indexes': [0], |
| 358 |
'old_indexes': [0, 1], |
| 359 |
'value': { |
| 360 |
'a': 2 |
| 361 |
}, |
| 362 |
'new_repeat': 1 |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
assert result == ddiff |
| 367 |
|
| 368 |
def test_list_ignore_order_report_repetition(self): |
| 369 |
t1 = [5, 1, 3, 1, 4, 4, 6] |
| 370 |
t2 = [7, 4, 4, 1, 3, 4, 8] |
| 371 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 372 |
result = { |
| 373 |
'values_changed': { |
| 374 |
'root[6]': { |
| 375 |
'new_value': 7, |
| 376 |
'old_value': 6 |
| 377 |
}, |
| 378 |
'root[0]': { |
| 379 |
'new_value': 8, |
| 380 |
'old_value': 5 |
| 381 |
} |
| 382 |
}, |
| 383 |
'repetition_change': { |
| 384 |
'root[4]': { |
| 385 |
'old_repeat': 2, |
| 386 |
'new_repeat': 3, |
| 387 |
'old_indexes': [4, 5], |
| 388 |
'new_indexes': [1, 2, 5], |
| 389 |
'value': 4 |
| 390 |
}, |
| 391 |
'root[1]': { |
| 392 |
'old_repeat': 2, |
| 393 |
'new_repeat': 1, |
| 394 |
'old_indexes': [1, 3], |
| 395 |
'new_indexes': [3], |
| 396 |
'value': 1 |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
assert result == ddiff |
| 401 |
|
| 402 |
def test_list_of_sets_difference_ignore_order(self): |
| 403 |
t1 = [{1}, {2}, {3}] |
| 404 |
t2 = [{4}, {1}, {2}, {3}] |
| 405 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 406 |
result = {'iterable_item_added': {'root[0]': {4}}} |
| 407 |
assert result == ddiff |
| 408 |
|
| 409 |
def test_list_of_sets_difference_ignore_order_when_there_is_duplicate( |
| 410 |
self): |
| 411 |
t1 = [{1}, {2}, {3}] |
| 412 |
t2 = [{4}, {1}, {2}, {3}, {3}] |
| 413 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 414 |
result = {'iterable_item_added': {'root[0]': {4}}} |
| 415 |
assert result == ddiff |
| 416 |
|
| 417 |
def test_list_of_sets_difference_ignore_order_when_there_is_duplicate_and_mix_of_hashable_unhashable( |
| 418 |
self): |
| 419 |
t1 = [1, 1, {2}, {3}] |
| 420 |
t2 = [{4}, 1, {2}, {3}, {3}, 1, 1] |
| 421 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 422 |
result = {'iterable_item_added': {'root[0]': {4}}} |
| 423 |
assert result == ddiff |
| 424 |
|
| 425 |
def test_dictionary_of_list_of_dictionary_ignore_order(self): |
| 426 |
t1 = { |
| 427 |
'item': [{ |
| 428 |
'title': 1, |
| 429 |
'http://purl.org/rss/1.0/modules/content/:encoded': '1' |
| 430 |
}, { |
| 431 |
'title': 2, |
| 432 |
'http://purl.org/rss/1.0/modules/content/:encoded': '2' |
| 433 |
}] |
| 434 |
} |
| 435 |
|
| 436 |
t2 = { |
| 437 |
'item': [{ |
| 438 |
'http://purl.org/rss/1.0/modules/content/:encoded': '1', |
| 439 |
'title': 1 |
| 440 |
}, { |
| 441 |
'http://purl.org/rss/1.0/modules/content/:encoded': '2', |
| 442 |
'title': 2 |
| 443 |
}] |
| 444 |
} |
| 445 |
|
| 446 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 447 |
assert {} == ddiff |
| 448 |
|
| 449 |
def test_comprehensive_ignore_order(self): |
| 450 |
|
| 451 |
t1 = { |
| 452 |
'key1': 'val1', |
| 453 |
'key2': [ |
| 454 |
{ |
| 455 |
'key3': 'val3', |
| 456 |
'key4': 'val4', |
| 457 |
}, |
| 458 |
{ |
| 459 |
'key5': 'val5', |
| 460 |
'key6': 'val6', |
| 461 |
}, |
| 462 |
], |
| 463 |
} |
| 464 |
|
| 465 |
t2 = { |
| 466 |
'key1': 'val1', |
| 467 |
'key2': [ |
| 468 |
{ |
| 469 |
'key5': 'val5', |
| 470 |
'key6': 'val6', |
| 471 |
}, |
| 472 |
{ |
| 473 |
'key3': 'val3', |
| 474 |
'key4': 'val4', |
| 475 |
}, |
| 476 |
], |
| 477 |
} |
| 478 |
|
| 479 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 480 |
assert {} == ddiff |
| 481 |
|
| 482 |
def test_ignore_order_when_objects_similar(self): |
| 483 |
|
| 484 |
t1 = { |
| 485 |
'key1': 'val1', |
| 486 |
'key2': [ |
| 487 |
{ |
| 488 |
'key3': 'val3', |
| 489 |
'key4': 'val4', |
| 490 |
}, |
| 491 |
{ |
| 492 |
'key5': 'val5', |
| 493 |
'key6': 'val6', |
| 494 |
}, |
| 495 |
], |
| 496 |
} |
| 497 |
|
| 498 |
t2 = { |
| 499 |
'key1': 'val1', |
| 500 |
'key2': [ |
| 501 |
{ |
| 502 |
'key5': 'CHANGE', |
| 503 |
'key6': 'val6', |
| 504 |
}, |
| 505 |
{ |
| 506 |
'key3': 'val3', |
| 507 |
'key4': 'val4', |
| 508 |
}, |
| 509 |
], |
| 510 |
} |
| 511 |
|
| 512 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 513 |
assert {'values_changed': {"root['key2'][1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5'}}} == ddiff |
| 514 |
|
| 515 |
def test_set_ignore_order_report_repetition(self): |
| 516 |
""" |
| 517 |
If this test fails, it means that DeepDiff is not checking |
| 518 |
for set types before general iterables. |
| 519 |
So it forces creating the hashtable because of report_repetition=True. |
| 520 |
""" |
| 521 |
t1 = {2, 1, 8} |
| 522 |
t2 = {1, 2, 3, 5} |
| 523 |
ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 524 |
result = { |
| 525 |
'set_item_added': {'root[3]', 'root[5]'}, |
| 526 |
'set_item_removed': {'root[8]'} |
| 527 |
} |
| 528 |
assert result == ddiff |
| 529 |
|
| 530 |
def test_custom_objects2(self): |
| 531 |
cc_a = CustomClass2(prop1=["a"], prop2=["b"]) |
| 532 |
cc_b = CustomClass2(prop1=["b"], prop2=["b"]) |
| 533 |
t1 = [cc_a, CustomClass2(prop1=["c"], prop2=["d"])] |
| 534 |
t2 = [cc_b, CustomClass2(prop1=["c"], prop2=["d"])] |
| 535 |
|
| 536 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 537 |
|
| 538 |
result = {'values_changed': {'root[0].prop1[0]': {'new_value': 'b', 'old_value': 'a'}}} |
| 539 |
assert result == ddiff |
| 540 |
|
| 541 |
def test_custom_object_type_change_when_ignore_order(self): |
| 542 |
|
| 543 |
class Burrito: |
| 544 |
bread = 'flour' |
| 545 |
|
| 546 |
def __init__(self): |
| 547 |
self.spicy = True |
| 548 |
|
| 549 |
class Taco: |
| 550 |
bread = 'flour' |
| 551 |
|
| 552 |
def __init__(self): |
| 553 |
self.spicy = True |
| 554 |
|
| 555 |
burrito = Burrito() |
| 556 |
taco = Taco() |
| 557 |
|
| 558 |
burritos = [burrito] |
| 559 |
tacos = [taco] |
| 560 |
|
| 561 |
assert not DeepDiff(burritos, tacos, ignore_type_in_groups=[(Taco, Burrito)], ignore_order=True) |
| 562 |
|
| 563 |
def test_decimal_ignore_order(self): |
| 564 |
t1 = [{1: Decimal('10.1')}, {2: Decimal('10.2')}] |
| 565 |
t2 = [{2: Decimal('10.2')}, {1: Decimal('10.1')}] |
| 566 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 567 |
result = {} |
| 568 |
assert result == ddiff |
| 569 |
|
| 570 |
@pytest.mark.parametrize('log_scale_similarity_threshold, expected', [ |
| 571 |
( |
| 572 |
0.1, |
| 573 |
{} |
| 574 |
), |
| 575 |
( |
| 576 |
0.01, |
| 577 |
{'values_changed': {'root[1][2]': {'new_value': Decimal('268'), 'old_value': Decimal('290.2')}}} |
| 578 |
), |
| 579 |
]) |
| 580 |
def test_decimal_log_scale_ignore_order1(self, log_scale_similarity_threshold, expected): |
| 581 |
t1 = [{1: Decimal('10.143')}, {2: Decimal('290.2')}] |
| 582 |
t2 = [{2: Decimal('268')}, {1: Decimal('10.23')}] |
| 583 |
ddiff = DeepDiff(t1, t2, ignore_order=True, use_log_scale=True, log_scale_similarity_threshold=log_scale_similarity_threshold, cutoff_intersection_for_pairs=1) |
| 584 |
assert expected == ddiff |
| 585 |
|
| 586 |
@pytest.mark.parametrize("t1, t2, significant_digits, ignore_order", [ |
| 587 |
(100000, 100021, 3, False), |
| 588 |
([10, 12, 100000], [50, 63, 100021], 3, False), |
| 589 |
([10, 12, 100000], [50, 63, 100021], 3, True), |
| 590 |
]) |
| 591 |
def test_number_to_string_func(self, t1, t2, significant_digits, ignore_order): |
| 592 |
def custom_number_to_string(number, *args, **kwargs): |
| 593 |
number = 100 if number < 100 else number |
| 594 |
return number_to_string(number, *args, **kwargs) |
| 595 |
|
| 596 |
ddiff = DeepDiff(t1, t2, significant_digits=3, number_format_notation="e", |
| 597 |
number_to_string_func=custom_number_to_string) |
| 598 |
|
| 599 |
assert {} == ddiff |
| 600 |
|
| 601 |
def test_ignore_type_in_groups_numbers_and_strings_when_ignore_order(self): |
| 602 |
t1 = [1, 2, 3, 'a'] |
| 603 |
t2 = [1.0, 2.0, 3.3, b'a'] |
| 604 |
ddiff = DeepDiff(t1, t2, ignore_numeric_type_changes=True, ignore_string_type_changes=True, ignore_order=True) |
| 605 |
result = {'values_changed': {'root[2]': {'new_value': 3.3, 'old_value': 3}}} |
| 606 |
assert result == ddiff |
| 607 |
|
| 608 |
def test_ignore_string_type_changes_when_dict_keys_merge_is_not_deterministic(self): |
| 609 |
t1 = {'a': 10, b'a': 20} |
| 610 |
t2 = {'a': 11, b'a': 22} |
| 611 |
ddiff = DeepDiff(t1, t2, ignore_numeric_type_changes=True, ignore_string_type_changes=True, ignore_order=True) |
| 612 |
result = {'values_changed': {"root['a']": {'new_value': 22, 'old_value': 20}}} |
| 613 |
alternative_result = {'values_changed': {"root['a']": {'new_value': 11, 'old_value': 10}}} |
| 614 |
assert result == ddiff or alternative_result == ddiff |
| 615 |
|
| 616 |
def test_skip_exclude_path5(self): |
| 617 |
exclude_paths = ["root[0]['e']", "root[1]['e']"] |
| 618 |
|
| 619 |
t1 = [{'a': 1, 'b': 'randomString', 'e': "1111"}] |
| 620 |
t2 = [{'a': 1, 'b': 'randomString', 'e': "2222"}] |
| 621 |
|
| 622 |
ddiff = DeepDiff(t1, t2, exclude_paths=exclude_paths, |
| 623 |
ignore_order=True, report_repetition=False) |
| 624 |
result = {} |
| 625 |
assert result == ddiff |
| 626 |
|
| 627 |
def test_skip_str_type_in_dict_on_list_when_ignored_order(self): |
| 628 |
t1 = [{1: "a"}] |
| 629 |
t2 = [{}] |
| 630 |
ddiff = DeepDiff(t1, t2, exclude_types=[str], ignore_order=True) |
| 631 |
result = {} |
| 632 |
assert result == ddiff |
| 633 |
|
| 634 |
@mock.patch('deepdiff.diff.logger') |
| 635 |
@mock.patch('deepdiff.diff.DeepHash') |
| 636 |
def test_diff_when_hash_fails(self, mock_DeepHash, mock_logger): |
| 637 |
mock_DeepHash.side_effect = ValueError('Boom!') |
| 638 |
t1 = {"blah": {4}, 2: 1337} |
| 639 |
t2 = {"blah": {4}, 2: 1337} |
| 640 |
with pytest.raises(ValueError) as exp: |
| 641 |
DeepDiff(t1, t2, ignore_order=True) |
| 642 |
assert 'Boom!' == str(exp.value) |
| 643 |
|
| 644 |
def test_bool_vs_number(self): |
| 645 |
t1 = { |
| 646 |
"A List": [ |
| 647 |
{ |
| 648 |
"Value One": True, |
| 649 |
"Value Two": 1 |
| 650 |
} |
| 651 |
], |
| 652 |
} |
| 653 |
|
| 654 |
t2 = { |
| 655 |
"A List": [ |
| 656 |
{ |
| 657 |
"Value Two": 1, |
| 658 |
"Value One": True |
| 659 |
} |
| 660 |
], |
| 661 |
} |
| 662 |
|
| 663 |
ddiff = DeepDiff(t1, t2, ignore_order=True, cutoff_intersection_for_pairs=1) |
| 664 |
assert ddiff == {} |
| 665 |
|
| 666 |
@pytest.mark.parametrize('max_passes, expected', [ |
| 667 |
(0, {'values_changed': {'root[0]': {'new_value': {'key5': 'CHANGE', 'key6': 'val6'}, 'old_value': {'key3': [[[[[1, 2, 4, 5]]]]], 'key4': [7, 8]}}, 'root[1]': {'new_value': {'key3': [[[[[1, 3, 5, 4]]]]], 'key4': [7, 8]}, 'old_value': {'key5': 'val5', 'key6': 'val6'}}}}), |
| 668 |
(1, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0]": {'new_value': [[[[1, 3, 5, 4]]]], 'old_value': [[[[1, 2, 4, 5]]]], 'new_path': "root[1]['key3'][0]"}}}), |
| 669 |
(22, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2, 'new_path': "root[1]['key3'][0][0][0][0][1]"}}}) |
| 670 |
]) |
| 671 |
def test_ignore_order_max_passes(self, max_passes, expected): |
| 672 |
t1 = [ |
| 673 |
{ |
| 674 |
'key3': [[[[[1, 2, 4, 5]]]]], |
| 675 |
'key4': [7, 8], |
| 676 |
}, |
| 677 |
{ |
| 678 |
'key5': 'val5', |
| 679 |
'key6': 'val6', |
| 680 |
}, |
| 681 |
] |
| 682 |
|
| 683 |
t2 = [ |
| 684 |
{ |
| 685 |
'key5': 'CHANGE', |
| 686 |
'key6': 'val6', |
| 687 |
}, |
| 688 |
{ |
| 689 |
'key3': [[[[[1, 3, 5, 4]]]]], |
| 690 |
'key4': [7, 8], |
| 691 |
}, |
| 692 |
] |
| 693 |
|
| 694 |
ddiff = DeepDiff(t1, t2, ignore_order=True, max_passes=max_passes, verbose_level=2, cache_size=5000, cutoff_intersection_for_pairs=1, threshold_to_diff_deeper=0) |
| 695 |
assert expected == ddiff |
| 696 |
|
| 697 |
@pytest.mark.parametrize('max_diffs, expected', [ |
| 698 |
(1, {}), |
| 699 |
(65, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}}}), |
| 700 |
(80, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2, 'new_path': "root[1]['key3'][0][0][0][0][1]"}}}), |
| 701 |
]) |
| 702 |
def test_ignore_order_max_diffs(self, max_diffs, expected): |
| 703 |
t1 = [ |
| 704 |
{ |
| 705 |
'key3': [[[[[1, 2, 4, 5]]]]], |
| 706 |
'key4': [7, 8], |
| 707 |
}, |
| 708 |
{ |
| 709 |
'key5': 'val5', |
| 710 |
'key6': 'val6', |
| 711 |
}, |
| 712 |
] |
| 713 |
|
| 714 |
t2 = [ |
| 715 |
{ |
| 716 |
'key5': 'CHANGE', |
| 717 |
'key6': 'val6', |
| 718 |
}, |
| 719 |
{ |
| 720 |
'key3': [[[[[1, 3, 5, 4]]]]], |
| 721 |
'key4': [7, 8], |
| 722 |
}, |
| 723 |
] |
| 724 |
|
| 725 |
# Note: these tests are not exactly deterministic |
| 726 |
ddiff = DeepDiff(t1, t2, ignore_order=True, max_diffs=max_diffs, verbose_level=2, cache_size=5000, cutoff_intersection_for_pairs=1) |
| 727 |
assert expected == ddiff |
| 728 |
|
| 729 |
def test_stats_that_include_distance_cache_hits(self): |
| 730 |
t1 = [ |
| 731 |
[1, 2, 3, 9], [9, 8, 5, 9] |
| 732 |
] |
| 733 |
|
| 734 |
t2 = [ |
| 735 |
[1, 2, 4, 10], [4, 2, 5] |
| 736 |
] |
| 737 |
|
| 738 |
diff = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, cutoff_intersection_for_pairs=1) |
| 739 |
expected = { |
| 740 |
'PASSES COUNT': 7, |
| 741 |
'DIFF COUNT': 37, |
| 742 |
'DISTANCE CACHE HIT COUNT': 0, |
| 743 |
'MAX PASS LIMIT REACHED': False, |
| 744 |
'MAX DIFF LIMIT REACHED': False, |
| 745 |
} |
| 746 |
assert expected == diff.get_stats() |
| 747 |
|
| 748 |
def test_ignore_order_report_repetition_and_self_loop(self): |
| 749 |
t1 = [[1, 2, 1, 3]] |
| 750 |
t1.append(t1) |
| 751 |
|
| 752 |
t2 = [[1, 2, 2, 2, 4]] |
| 753 |
t2.append(t2) |
| 754 |
|
| 755 |
diff = DeepDiff(t1, t2, ignore_order=True, cutoff_intersection_for_pairs=1) |
| 756 |
expected = { |
| 757 |
'values_changed': { |
| 758 |
'root[0][3]': { |
| 759 |
'new_value': 4, |
| 760 |
'old_value': 3 |
| 761 |
}, |
| 762 |
'root[1]': { |
| 763 |
'new_value': t2, |
| 764 |
'old_value': t1 |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
assert expected == diff |
| 769 |
|
| 770 |
diff2 = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, cutoff_intersection_for_pairs=1) |
| 771 |
assert expected == diff2 |
| 772 |
|
| 773 |
def test_ignore_order_with_sha256_hash(self): |
| 774 |
t1 = [ |
| 775 |
[1, 2, 3, 9], [9, 8, 5, 9] |
| 776 |
] |
| 777 |
|
| 778 |
t2 = [ |
| 779 |
[1, 2, 3, 10], [8, 2, 5] |
| 780 |
] |
| 781 |
diff = DeepDiff(t1, t2, ignore_order=True, hasher=sha256hex, cutoff_intersection_for_pairs=1) |
| 782 |
expected = { |
| 783 |
'values_changed': { |
| 784 |
'root[0][3]': { |
| 785 |
'new_value': 10, |
| 786 |
'old_value': 9 |
| 787 |
}, |
| 788 |
'root[1][0]': { |
| 789 |
'new_value': 2, |
| 790 |
'old_value': 9 |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
assert expected == diff |
| 795 |
|
| 796 |
def test_ignore_order_cache_for_individual_distances(self): |
| 797 |
t1 = [[1, 2, 'B', 3], 'B'] |
| 798 |
t2 = [[1, 2, 3, 5], 5] |
| 799 |
diff = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, cutoff_intersection_for_pairs=1) |
| 800 |
expected = { |
| 801 |
'values_changed': { |
| 802 |
'root[1]': { |
| 803 |
'new_value': 5, |
| 804 |
'old_value': 'B' |
| 805 |
} |
| 806 |
}, |
| 807 |
'iterable_item_added': { |
| 808 |
'root[0][3]': 5 |
| 809 |
}, |
| 810 |
'iterable_item_removed': { |
| 811 |
'root[0][2]': 'B' |
| 812 |
} |
| 813 |
} |
| 814 |
assert expected == diff |
| 815 |
|
| 816 |
stats = diff.get_stats() |
| 817 |
expected_stats = { |
| 818 |
'PASSES COUNT': 3, |
| 819 |
'DIFF COUNT': 13, |
| 820 |
'DISTANCE CACHE HIT COUNT': 1, |
| 821 |
'MAX PASS LIMIT REACHED': False, |
| 822 |
'MAX DIFF LIMIT REACHED': False |
| 823 |
} |
| 824 |
assert expected_stats == stats |
| 825 |
|
| 826 |
t1 = [[1, 2, 'B', 3], 5] |
| 827 |
t2 = [[1, 2, 3, 5], 'B'] |
| 828 |
diff2 = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, cutoff_intersection_for_pairs=1) |
| 829 |
assert expected_stats == diff2.get_stats() |
| 830 |
|
| 831 |
def test_cutoff_distance_for_pairs(self): |
| 832 |
t1 = [[1.0]] |
| 833 |
t2 = [[20.0]] |
| 834 |
diff1 = DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.3) |
| 835 |
expected1 = {'values_changed': {'root[0][0]': {'new_value': 20.0, 'old_value': 1.0}}} |
| 836 |
assert expected1 == diff1 |
| 837 |
|
| 838 |
diff2 = DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.1) |
| 839 |
expected2 = {'values_changed': {'root[0]': {'new_value': [20.0], 'old_value': [1.0]}}} |
| 840 |
assert expected2 == diff2 |
| 841 |
|
| 842 |
diff_with_dist = DeepDiff(1.0, 20.0, get_deep_distance=True) |
| 843 |
expected = {'values_changed': {'root': {'new_value': 20.0, 'old_value': 1.0}}, 'deep_distance': 0.2714285714285714} |
| 844 |
|
| 845 |
assert expected == diff_with_dist |
| 846 |
|
| 847 |
def test_ignore_order_and_group_by1(self): |
| 848 |
t1 = [ |
| 849 |
{'id': 'AA', 'name': 'Joe', 'ate': ['Nothing']}, |
| 850 |
{'id': 'BB', 'name': 'James', 'ate': ['Chips', 'Cheese']}, |
| 851 |
{'id': 'CC', 'name': 'Mike', 'ate': ['Apple']}, |
| 852 |
] |
| 853 |
|
| 854 |
t2 = [ |
| 855 |
{'id': 'BB', 'name': 'James', 'ate': ['Chips', 'Brownies', 'Cheese']}, |
| 856 |
{'id': 'AA', 'name': 'Joe', 'ate': ['Nothing']}, |
| 857 |
{'id': 'CC', 'name': 'Mike', 'ate': ['Apple', 'Apple']}, |
| 858 |
] |
| 859 |
|
| 860 |
diff = DeepDiff(t1, t2, group_by='id', ignore_order=False) |
| 861 |
expected = {'iterable_item_added': {"root['BB']['ate'][1]": 'Brownies', "root['CC']['ate'][1]": 'Apple'}} |
| 862 |
assert expected == diff |
| 863 |
|
| 864 |
diff2 = DeepDiff(t1, t2, group_by='id', ignore_order=True) |
| 865 |
expected2 = {'iterable_item_added': {"root['BB']['ate'][1]": 'Brownies'}} |
| 866 |
assert expected2 == diff2 |
| 867 |
|
| 868 |
def test_ignore_order_and_group_by2(self): |
| 869 |
t1_data = [{'id': '1', 'codes': ['1', '2', '3']}] |
| 870 |
t2_data = [{'id': '1', 'codes': ['1', '2', '4']}] |
| 871 |
diff = DeepDiff(t1_data, t2_data, group_by='id', ignore_order=True) |
| 872 |
expected = {'values_changed': {"root['1']['codes'][2]": {'new_value': '4', 'old_value': '3'}}} |
| 873 |
assert expected == diff |
| 874 |
|
| 875 |
def test_ignore_order_and_group_by3(self): |
| 876 |
t1 = [{ |
| 877 |
'id': |
| 878 |
'5ec52e', |
| 879 |
'products': [{ |
| 880 |
'lineNumber': 1, |
| 881 |
'productPrice': '2.39', |
| 882 |
'productQuantity': 2 |
| 883 |
}, { |
| 884 |
'lineNumber': 2, |
| 885 |
'productPrice': '4.44', |
| 886 |
'productQuantity': 1 |
| 887 |
}], |
| 888 |
}] |
| 889 |
|
| 890 |
t2 = [{ |
| 891 |
'id': |
| 892 |
'5ec52e', |
| 893 |
'products': [ |
| 894 |
{ |
| 895 |
'lineNumber': 2, |
| 896 |
'productPrice': '4.44', |
| 897 |
'productQuantity': 1 |
| 898 |
}, |
| 899 |
{ |
| 900 |
'lineNumber': 1, |
| 901 |
'productPrice': '2.39', |
| 902 |
'productQuantity': 2 |
| 903 |
}, |
| 904 |
], |
| 905 |
}] |
| 906 |
|
| 907 |
diff = DeepDiff(t1, t2, group_by='id', ignore_order=True) |
| 908 |
assert {} == diff |
| 909 |
|
| 910 |
def test_ignore_order_and_group_by4(self): |
| 911 |
t1 = [ |
| 912 |
{ |
| 913 |
"id": "1", |
| 914 |
"field_01": { |
| 915 |
"subfield_01": { |
| 916 |
"subfield_02": {"subfield_03": "1"}, |
| 917 |
} |
| 918 |
}, |
| 919 |
}, |
| 920 |
{"id": "2", "field_01": ["1", "2", "3"]}, |
| 921 |
{"id": "3", "field_01": ["1", "2", "3"]}, |
| 922 |
] |
| 923 |
t2 = [ |
| 924 |
{ |
| 925 |
"id": "1", |
| 926 |
"field_01": { |
| 927 |
"subfield_01": { |
| 928 |
"subfield_02": {"subfield_03": "2"}, |
| 929 |
} |
| 930 |
}, |
| 931 |
}, |
| 932 |
{"id": "2", "field_01": ["4", "5", "6"]}, |
| 933 |
{"id": "3", "field_01": ["7", "8", "9"]}, |
| 934 |
] |
| 935 |
diff = DeepDiff(t1, t2, group_by='id', ignore_order=True) |
| 936 |
expected = { |
| 937 |
'values_changed': { |
| 938 |
"root['1']['field_01']['subfield_01']['subfield_02']['subfield_03']": { |
| 939 |
'new_value': '2', |
| 940 |
'old_value': '1' |
| 941 |
}, |
| 942 |
"root['2']['field_01'][1]": { |
| 943 |
'new_value': '5', |
| 944 |
'old_value': '2' |
| 945 |
}, |
| 946 |
"root['3']['field_01'][2]": { |
| 947 |
'new_value': '9', |
| 948 |
'old_value': '3' |
| 949 |
}, |
| 950 |
"root['2']['field_01'][0]": { |
| 951 |
'new_value': '4', |
| 952 |
'old_value': '1' |
| 953 |
}, |
| 954 |
"root['3']['field_01'][1]": { |
| 955 |
'new_value': '8', |
| 956 |
'old_value': '2' |
| 957 |
}, |
| 958 |
"root['3']['field_01'][0]": { |
| 959 |
'new_value': '7', |
| 960 |
'old_value': '1' |
| 961 |
}, |
| 962 |
"root['2']['field_01'][2]": { |
| 963 |
'new_value': '6', |
| 964 |
'old_value': '3' |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
assert expected == diff |
| 970 |
|
| 971 |
def test_math_epsilon_when_ignore_order_in_dictionary(self): |
| 972 |
a = {'x': 0.001} |
| 973 |
b = {'x': 0.0011} |
| 974 |
diff = DeepDiff(a, b, ignore_order=True) |
| 975 |
assert {'values_changed': {"root['x']": {'new_value': 0.0011, 'old_value': 0.001}}} == diff |
| 976 |
|
| 977 |
diff2 = DeepDiff(a, b, ignore_order=True, math_epsilon=0.01) |
| 978 |
assert {} == diff2 |
| 979 |
|
| 980 |
def test_math_epsilon_when_ignore_order_in_list(self): |
| 981 |
a = [0.001, 2] |
| 982 |
b = [2, 0.0011] |
| 983 |
diff = DeepDiff(a, b, ignore_order=True) |
| 984 |
assert {'values_changed': {'root[0]': {'new_value': 0.0011, 'old_value': 0.001}}} == diff |
| 985 |
|
| 986 |
diff2 = DeepDiff(a, b, ignore_order=True, math_epsilon=0.01) |
| 987 |
assert {} == diff2 |
| 988 |
|
| 989 |
def test_math_epsilon_when_ignore_order_in_nested_list(self): |
| 990 |
a = [{'x': 0.001}, {'y': 2.00002}] |
| 991 |
b = [{'x': 0.0011}, {'y': 2}] |
| 992 |
|
| 993 |
diff = DeepDiff(a, b, ignore_order=True, math_epsilon=0.01) |
| 994 |
expected = {'values_changed': {'root[0]': {'new_value': {'x': 0.0011}, 'old_value': {'x': 0.001}}, 'root[1]': {'new_value': {'y': 2}, 'old_value': {'y': 2.00002}}}} |
| 995 |
assert expected == diff |
| 996 |
|
| 997 |
def test_datetime_and_ignore_order(self): |
| 998 |
diff = DeepDiff( |
| 999 |
[{'due_date': datetime.date(2024, 2, 1)}], |
| 1000 |
[{'due_date': datetime.date(2024, 2, 2)}], |
| 1001 |
ignore_order=True, |
| 1002 |
ignore_numeric_type_changes=True |
| 1003 |
) |
| 1004 |
assert {} != diff |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
class TestCompareFuncIgnoreOrder: |
| 1009 |
|
| 1010 |
def test_ignore_order_with_compare_func_to_guide_comparison(self): |
| 1011 |
t1 = [ |
| 1012 |
{ |
| 1013 |
'id': 1, |
| 1014 |
'value': [1] |
| 1015 |
}, |
| 1016 |
{ |
| 1017 |
'id': 2, |
| 1018 |
'value': [7, 8, 1] |
| 1019 |
}, |
| 1020 |
{ |
| 1021 |
'id': 3, |
| 1022 |
'value': [7, 8], |
| 1023 |
}, |
| 1024 |
] |
| 1025 |
|
| 1026 |
t2 = [ |
| 1027 |
{ |
| 1028 |
'id': 2, |
| 1029 |
'value': [7, 8] |
| 1030 |
}, |
| 1031 |
{ |
| 1032 |
'id': 3, |
| 1033 |
'value': [7, 8, 1], |
| 1034 |
}, |
| 1035 |
{ |
| 1036 |
'id': 1, |
| 1037 |
'value': [1] |
| 1038 |
}, |
| 1039 |
] |
| 1040 |
|
| 1041 |
expected = { |
| 1042 |
'values_changed': { |
| 1043 |
"root[2]['id']": { |
| 1044 |
'new_value': 2, |
| 1045 |
'old_value': 3 |
| 1046 |
}, |
| 1047 |
"root[1]['id']": { |
| 1048 |
'new_value': 3, |
| 1049 |
'old_value': 2 |
| 1050 |
} |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
expected_with_compare_func = { |
| 1055 |
'iterable_item_added': { |
| 1056 |
"root[2]['value'][2]": 1 |
| 1057 |
}, |
| 1058 |
'iterable_item_removed': { |
| 1059 |
"root[1]['value'][2]": 1 |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 1064 |
|
| 1065 |
assert expected == ddiff |
| 1066 |
|
| 1067 |
def compare_func(x, y, level=None): |
| 1068 |
try: |
| 1069 |
return x['id'] == y['id'] |
| 1070 |
except Exception: |
| 1071 |
raise CannotCompare() from None |
| 1072 |
|
| 1073 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func) |
| 1074 |
assert expected_with_compare_func == ddiff2 |
| 1075 |
assert ddiff != ddiff2 |
| 1076 |
|
| 1077 |
ddiff3 = DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func, view='tree') |
| 1078 |
assert 1 == ddiff3['iterable_item_removed'][0].t1 |
| 1079 |
assert 1 == ddiff3['iterable_item_added'][0].t2 |
| 1080 |
|
| 1081 |
def test_ignore_order_with_compare_func_can_throw_cannot_compare(self): |
| 1082 |
t1 = [ |
| 1083 |
{1}, |
| 1084 |
{ |
| 1085 |
'id': 2, |
| 1086 |
'value': [7, 8, 1] |
| 1087 |
}, |
| 1088 |
{ |
| 1089 |
'id': 3, |
| 1090 |
'value': [7, 8], |
| 1091 |
}, |
| 1092 |
] |
| 1093 |
|
| 1094 |
t2 = [ |
| 1095 |
{ |
| 1096 |
'id': 2, |
| 1097 |
'value': [7, 8] |
| 1098 |
}, |
| 1099 |
{ |
| 1100 |
'id': 3, |
| 1101 |
'value': [7, 8, 1], |
| 1102 |
}, |
| 1103 |
{}, |
| 1104 |
] |
| 1105 |
|
| 1106 |
expected = { |
| 1107 |
'type_changes': { |
| 1108 |
'root[0]': { |
| 1109 |
'old_type': set, |
| 1110 |
'new_type': dict, |
| 1111 |
'old_value': {1}, |
| 1112 |
'new_value': {} |
| 1113 |
} |
| 1114 |
}, |
| 1115 |
'values_changed': { |
| 1116 |
"root[2]['id']": { |
| 1117 |
'new_value': 2, |
| 1118 |
'old_value': 3 |
| 1119 |
}, |
| 1120 |
"root[1]['id']": { |
| 1121 |
'new_value': 3, |
| 1122 |
'old_value': 2 |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
expected_with_compare_func = { |
| 1127 |
'type_changes': { |
| 1128 |
'root[0]': { |
| 1129 |
'old_type': set, |
| 1130 |
'new_type': dict, |
| 1131 |
'old_value': {1}, |
| 1132 |
'new_value': {} |
| 1133 |
} |
| 1134 |
}, |
| 1135 |
'iterable_item_added': { |
| 1136 |
"root[2]['value'][2]": 1 |
| 1137 |
}, |
| 1138 |
'iterable_item_removed': { |
| 1139 |
"root[1]['value'][2]": 1 |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
ddiff = DeepDiff(t1, t2, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1, ignore_order=True, threshold_to_diff_deeper=0) |
| 1144 |
assert expected == ddiff |
| 1145 |
|
| 1146 |
def compare_func(x, y, level=None): |
| 1147 |
try: |
| 1148 |
return x['id'] == y['id'] |
| 1149 |
except Exception: |
| 1150 |
raise CannotCompare() from None |
| 1151 |
|
| 1152 |
ddiff2 = DeepDiff(t1, t2, ignore_order=True, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1, iterable_compare_func=compare_func, threshold_to_diff_deeper=0) |
| 1153 |
assert expected_with_compare_func == ddiff2 |
| 1154 |
assert ddiff != ddiff2 |
| 1155 |
|
| 1156 |
def test_ignore_order_with_compare_func_with_one_each_hashes_added_hashes_removed(self): |
| 1157 |
""" |
| 1158 |
Scenario: |
| 1159 |
In this example which demonstrates the problem... We have two dictionaries containing lists for |
| 1160 |
individualNames. Each list contains exactly 2 elements. The effective change is that we are |
| 1161 |
replacing the 2nd element in the list. |
| 1162 |
NOTE: This is considered a REPLACEMENT of the second element and not an UPDATE of the element |
| 1163 |
because we are providing a custom compare_func which will determine matching elements based on |
| 1164 |
the value of the nameIdentifier field. If the custom compare_func is not used, then |
| 1165 |
deepdiff.diff will mistakenly treat the difference as being individual field updates for every |
| 1166 |
field in the second element of the list. |
| 1167 |
|
| 1168 |
Intent: |
| 1169 |
Use our custom compare_func, since we have provided it. |
| 1170 |
We need to fall into self._precalculate_distance_by_custom_compare_func |
| 1171 |
To do this, we are proposing a change to deepdiff.diff line 1128: |
| 1172 |
|
| 1173 |
Original: |
| 1174 |
if hashes_added and hashes_removed and self.iterable_compare_func and len(hashes_added) > 1 and len(hashes_removed) > 1: |
| 1175 |
|
| 1176 |
Proposed/Updated: |
| 1177 |
if hashes_added and hashes_removed \ |
| 1178 |
and self.iterable_compare_func \ |
| 1179 |
and len(hashes_added) > 0 and len(hashes_removed) > 0: |
| 1180 |
|
| 1181 |
NOTE: It is worth mentioning that deepdiff.diff line 1121, might also benefit by changing the length conditions |
| 1182 |
to evaluate for > 0 (rather than > 1). |
| 1183 |
""" |
| 1184 |
|
| 1185 |
t1 = { |
| 1186 |
"individualNames": [ |
| 1187 |
{ |
| 1188 |
"firstName": "Johnathan", |
| 1189 |
"lastName": "Doe", |
| 1190 |
"prefix": "COLONEL", |
| 1191 |
"middleName": "A", |
| 1192 |
"primaryIndicator": True, |
| 1193 |
"professionalDesignation": "PHD", |
| 1194 |
"suffix": "SR", |
| 1195 |
"nameIdentifier": "00001" |
| 1196 |
}, |
| 1197 |
{ |
| 1198 |
"firstName": "John", |
| 1199 |
"lastName": "Doe", |
| 1200 |
"prefix": "", |
| 1201 |
"middleName": "", |
| 1202 |
"primaryIndicator": False, |
| 1203 |
"professionalDesignation": "", |
| 1204 |
"suffix": "SR", |
| 1205 |
"nameIdentifier": "00002" |
| 1206 |
} |
| 1207 |
] |
| 1208 |
} |
| 1209 |
|
| 1210 |
t2 = { |
| 1211 |
"individualNames": [ |
| 1212 |
{ |
| 1213 |
"firstName": "Johnathan", |
| 1214 |
"lastName": "Doe", |
| 1215 |
"prefix": "COLONEL", |
| 1216 |
"middleName": "A", |
| 1217 |
"primaryIndicator": True, |
| 1218 |
"professionalDesignation": "PHD", |
| 1219 |
"suffix": "SR", |
| 1220 |
"nameIdentifier": "00001" |
| 1221 |
}, |
| 1222 |
{ |
| 1223 |
"firstName": "Johnny", |
| 1224 |
"lastName": "Doe", |
| 1225 |
"prefix": "", |
| 1226 |
"middleName": "A", |
| 1227 |
"primaryIndicator": False, |
| 1228 |
"professionalDesignation": "", |
| 1229 |
"suffix": "SR", |
| 1230 |
"nameIdentifier": "00003" |
| 1231 |
} |
| 1232 |
] |
| 1233 |
} |
| 1234 |
def compare_func(item1, item2, level=None): |
| 1235 |
print("*** inside compare ***") |
| 1236 |
it1_keys = item1.keys() |
| 1237 |
|
| 1238 |
try: |
| 1239 |
|
| 1240 |
# --- individualNames --- |
| 1241 |
if 'nameIdentifier' in it1_keys and 'lastName' in it1_keys: |
| 1242 |
match_result = item1['nameIdentifier'] == item2['nameIdentifier'] |
| 1243 |
print("individualNames - matching result:", match_result) |
| 1244 |
return match_result |
| 1245 |
else: |
| 1246 |
print("Unknown list item...", "matching result:", item1 == item2) |
| 1247 |
return item1 == item2 |
| 1248 |
except Exception: |
| 1249 |
raise CannotCompare() from None |
| 1250 |
# ---------------------------- End of nested function |
| 1251 |
|
| 1252 |
actual_diff = DeepDiff(t1, t2, report_repetition=True, |
| 1253 |
ignore_order=True, iterable_compare_func=compare_func, cutoff_intersection_for_pairs=1) |
| 1254 |
|
| 1255 |
old_invalid_diff = { |
| 1256 |
'values_changed': {"root['individualNames'][1]['firstName']": {'new_value': 'Johnny', 'old_value': 'John'}, |
| 1257 |
"root['individualNames'][1]['middleName']": {'new_value': 'A', 'old_value': ''}, |
| 1258 |
"root['individualNames'][1]['nameIdentifier']": {'new_value': '00003', |
| 1259 |
'old_value': '00002'}}} |
| 1260 |
new_expected_diff = {'iterable_item_added': { |
| 1261 |
"root['individualNames'][1]": {'firstName': 'Johnny', 'lastName': 'Doe', 'prefix': '', 'middleName': 'A', |
| 1262 |
'primaryIndicator': False, 'professionalDesignation': '', 'suffix': 'SR', |
| 1263 |
'nameIdentifier': '00003'}}, 'iterable_item_removed': { |
| 1264 |
"root['individualNames'][1]": {'firstName': 'John', 'lastName': 'Doe', 'prefix': '', 'middleName': '', |
| 1265 |
'primaryIndicator': False, 'professionalDesignation': '', 'suffix': 'SR', |
| 1266 |
'nameIdentifier': '00002'}}} |
| 1267 |
|
| 1268 |
assert old_invalid_diff != actual_diff |
| 1269 |
assert new_expected_diff == actual_diff |
| 1270 |
|
| 1271 |
|
| 1272 |
class TestDynamicIgnoreOrder: |
| 1273 |
def test_ignore_order_func(self): |
| 1274 |
t1 = { |
| 1275 |
"order_matters": [ |
| 1276 |
{1}, |
| 1277 |
{ |
| 1278 |
'id': 2, |
| 1279 |
'value': [7, 8, 1] |
| 1280 |
}, |
| 1281 |
{ |
| 1282 |
'id': 3, |
| 1283 |
'value': [7, 8], |
| 1284 |
}, |
| 1285 |
], |
| 1286 |
"order_does_not_matter": [ |
| 1287 |
{1}, |
| 1288 |
{ |
| 1289 |
'id': 2, |
| 1290 |
'value': [7, 8, 1] |
| 1291 |
}, |
| 1292 |
{ |
| 1293 |
'id': 3, |
| 1294 |
'value': [7, 8], |
| 1295 |
}, |
| 1296 |
] |
| 1297 |
} |
| 1298 |
|
| 1299 |
t2 = { |
| 1300 |
"order_matters": [ |
| 1301 |
{ |
| 1302 |
'id': 2, |
| 1303 |
'value': [7, 8] |
| 1304 |
}, |
| 1305 |
{ |
| 1306 |
'id': 3, |
| 1307 |
'value': [7, 8, 1], |
| 1308 |
}, |
| 1309 |
{}, |
| 1310 |
], |
| 1311 |
"order_does_not_matter": [ |
| 1312 |
{ |
| 1313 |
'id': 2, |
| 1314 |
'value': [7, 8] |
| 1315 |
}, |
| 1316 |
{ |
| 1317 |
'id': 3, |
| 1318 |
'value': [7, 8, 1], |
| 1319 |
}, |
| 1320 |
{}, |
| 1321 |
] |
| 1322 |
} |
| 1323 |
|
| 1324 |
def ignore_order_func(level): |
| 1325 |
return "order_does_not_matter" in level.path() |
| 1326 |
|
| 1327 |
ddiff = DeepDiff(t1, t2, cutoff_intersection_for_pairs=1, cutoff_distance_for_pairs=1, ignore_order_func=ignore_order_func, threshold_to_diff_deeper=0) |
| 1328 |
|
| 1329 |
expected = { |
| 1330 |
'type_changes': { |
| 1331 |
"root['order_matters'][0]": { |
| 1332 |
'old_type': set, |
| 1333 |
'new_type': dict, |
| 1334 |
'old_value': {1}, |
| 1335 |
'new_value': {'id': 2, 'value': [7, 8]} |
| 1336 |
}, |
| 1337 |
"root['order_does_not_matter'][0]": { |
| 1338 |
'old_type': set, |
| 1339 |
'new_type': dict, |
| 1340 |
'old_value': {1}, |
| 1341 |
'new_value': {} |
| 1342 |
} |
| 1343 |
}, |
| 1344 |
'dictionary_item_removed': [ |
| 1345 |
"root['order_matters'][2]['id']", |
| 1346 |
"root['order_matters'][2]['value']" |
| 1347 |
], |
| 1348 |
'values_changed': { |
| 1349 |
"root['order_matters'][1]['id']": {'new_value': 3, 'old_value': 2}, |
| 1350 |
"root['order_does_not_matter'][2]['id']": {'new_value': 2, 'old_value': 3}, |
| 1351 |
"root['order_does_not_matter'][1]['id']": {'new_value': 3, 'old_value': 2} |
| 1352 |
} |
| 1353 |
} |
| 1354 |
assert expected == ddiff |
| 1355 |
|
| 1356 |
|
| 1357 |
class TestDecodingErrorIgnoreOrder: |
| 1358 |
|
| 1359 |
EXPECTED_MESSAGE1 = ( |
| 1360 |
"'utf-8' codec can't decode byte 0xc3 in position 0: Can not produce a hash for root: invalid continuation byte in '('. " |
| 1361 |
"Please either pass ignore_encoding_errors=True or pass the encoding via encodings=['utf-8', '...'].") |
| 1362 |
|
| 1363 |
EXPECTED_MESSAGE2 = ( |
| 1364 |
"'utf-8' codec can't decode byte 0xbc in position 0: Can not produce a hash for root: invalid start byte in ' cup of flour'. " |
| 1365 |
"Please either pass ignore_encoding_errors=True or pass the encoding via encodings=['utf-8', '...'].") |
| 1366 |
|
| 1367 |
@pytest.mark.parametrize('test_num, item, encodings, ignore_encoding_errors, expected_result, expected_message', [ |
| 1368 |
(1, b'\xc3\x28', None, False, UnicodeDecodeError, EXPECTED_MESSAGE1), |
| 1369 |
(2, b'\xc3\x28', ['utf-8'], False, UnicodeDecodeError, EXPECTED_MESSAGE1), |
| 1370 |
(3, b'\xc3\x28', ['utf-8'], True, {'values_changed': {'root[0]': {'new_value': b'\xc3(', 'old_value': b'foo'}}}, None), |
| 1371 |
(4, b"\xbc cup of flour", ['utf-8'], False, UnicodeDecodeError, EXPECTED_MESSAGE2), |
| 1372 |
(5, b"\xbc cup of flour", ['utf-8'], True, {'values_changed': {'root[0]': {'new_value': b'\xbc cup of flour', 'old_value': b'foo'}}}, None), |
| 1373 |
(6, b"\xbc cup of flour", ['utf-8', 'latin-1'], False, {'values_changed': {'root[0]': {'new_value': b'\xbc cup of flour', 'old_value': b'foo'}}}, None), |
| 1374 |
]) |
| 1375 |
@mock.patch('deepdiff.diff.logger') |
| 1376 |
def test_diff_encodings(self, mock_logger, test_num, item, encodings, ignore_encoding_errors, expected_result, expected_message): |
| 1377 |
if UnicodeDecodeError == expected_result: |
| 1378 |
with pytest.raises(expected_result) as exc_info: |
| 1379 |
DeepDiff([b'foo'], [item], encodings=encodings, ignore_encoding_errors=ignore_encoding_errors, ignore_order=True) |
| 1380 |
assert expected_message == str(exc_info.value), f"test_diff_encodings test #{test_num} failed." |
| 1381 |
else: |
| 1382 |
result = DeepDiff([b'foo'], [item], encodings=encodings, ignore_encoding_errors=ignore_encoding_errors, ignore_order=True) |
| 1383 |
assert expected_result == result, f"test_diff_encodings test #{test_num} failed." |
| 1384 |
|
| 1385 |
|
| 1386 |
class TestErrorMessagesWhenIgnoreOrder: |
| 1387 |
|
| 1388 |
@mock.patch('deepdiff.diff.logger') |
| 1389 |
def test_error_messages_when_ignore_order(self, mock_logger): |
| 1390 |
t1 = {'x': 0, 'y': [0, 'a', 'b', 'c']} |
| 1391 |
t2 = {'x': 1, 'y': [1, 'c', 'b', 'a']} |
| 1392 |
|
| 1393 |
exclude = [re.compile(r"\['x'\]"), re.compile(r"\['y'\]\[0\]")] |
| 1394 |
|
| 1395 |
result = DeepDiff(t1, t2, ignore_order=True, exclude_regex_paths=exclude) |
| 1396 |
assert {} == result |
| 1397 |
|
| 1398 |
assert not mock_logger.error.called |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
class TestIgnoreOrderNumericTypeChange: |
| 1403 |
"""Regression tests for GitHub issue #485. |
| 1404 |
|
| 1405 |
When ignore_order=True, numerically-equal values of different numeric types |
| 1406 |
(e.g. int 1 vs float 1.0) must still be reported as type_changes. |
| 1407 |
|
| 1408 |
Root cause: Python's hash equality (hash(1) == hash(1.0)) and value equality |
| 1409 |
(1 == 1.0) caused both items to land in the same DeepHash bucket, so they |
| 1410 |
were treated as identical and silently dropped from the diff result. |
| 1411 |
""" |
| 1412 |
|
| 1413 |
def test_int_vs_float_in_list_of_dicts(self): |
| 1414 |
"""Core regression: type change inside a dict nested in a list.""" |
| 1415 |
result = DeepDiff([{"a": 1}], [{"a": 1.0}], ignore_order=True) |
| 1416 |
assert "type_changes" in result, ( |
| 1417 |
"Expected type_changes between int 1 and float 1.0, got: %s" % result |
| 1418 |
) |
| 1419 |
assert result["type_changes"]["root[0]['a']"]["old_type"] is int |
| 1420 |
assert result["type_changes"]["root[0]['a']"]["new_type"] is float |
| 1421 |
|
| 1422 |
def test_ignore_numeric_type_changes_suppresses_report(self): |
| 1423 |
"""When ignore_numeric_type_changes=True the type change must be hidden.""" |
| 1424 |
result = DeepDiff( |
| 1425 |
[{"a": 1}], [{"a": 1.0}], |
| 1426 |
ignore_order=True, |
| 1427 |
ignore_numeric_type_changes=True, |
| 1428 |
) |
| 1429 |
assert result == {}, ( |
| 1430 |
"With ignore_numeric_type_changes=True there should be no diff, got: %s" % result |
| 1431 |
) |
| 1432 |
|
| 1433 |
def test_value_change_still_detected(self): |
| 1434 |
"""Ordinary value differences must still be detected.""" |
| 1435 |
result = DeepDiff([1], [2], ignore_order=True) |
| 1436 |
assert result != {}, "Expected a diff between [1] and [2]" |
| 1437 |
|
| 1438 |
def test_reorder_no_false_positive(self): |
| 1439 |
"""A simple reorder of identical values must not trigger type_changes.""" |
| 1440 |
result = DeepDiff([1, 2, 3], [3, 2, 1], ignore_order=True) |
| 1441 |
assert result == {}, "Reordering identical ints must not produce a diff" |
| 1442 |
|
| 1443 |
def test_mixed_list_one_type_change(self): |
| 1444 |
"""Only the item with a type change should appear in the diff.""" |
| 1445 |
result = DeepDiff( |
| 1446 |
[{"a": 1}, {"b": 2}], |
| 1447 |
[{"b": 2}, {"a": 1.0}], |
| 1448 |
ignore_order=True, |
| 1449 |
) |
| 1450 |
assert "type_changes" in result |
| 1451 |
assert "root[0]['a']" in result["type_changes"] |
| 1452 |
|
| 1453 |
def test_ignore_order_false_unchanged(self): |
| 1454 |
"""The ignore_order=False path must continue to work as before.""" |
| 1455 |
result = DeepDiff([{"a": 1}], [{"a": 1.0}], ignore_order=False) |
| 1456 |
assert "type_changes" in result |