| 1 |
import copy |
| 2 |
import datetime |
| 3 |
from typing import NamedTuple |
| 4 |
import pytest |
| 5 |
import os |
| 6 |
import io |
| 7 |
import json |
| 8 |
import sys |
| 9 |
from decimal import Decimal |
| 10 |
from unittest import mock |
| 11 |
from deepdiff import Delta, DeepDiff |
| 12 |
from deepdiff.helper import np, number_to_string, TEXT_VIEW, DELTA_VIEW, CannotCompare, FlatDeltaRow, FlatDataAction, SetOrdered |
| 13 |
from deepdiff.path import GETATTR, GET |
| 14 |
from deepdiff.delta import ( |
| 15 |
ELEM_NOT_FOUND_TO_ADD_MSG, |
| 16 |
VERIFICATION_MSG, VERIFY_BIDIRECTIONAL_MSG, not_found, DeltaNumpyOperatorOverrideError, |
| 17 |
BINIARY_MODE_NEEDED_MSG, DELTA_AT_LEAST_ONE_ARG_NEEDED, DeltaError, |
| 18 |
INVALID_ACTION_WHEN_CALLING_GET_ELEM, INVALID_ACTION_WHEN_CALLING_SIMPLE_SET_ELEM, |
| 19 |
INVALID_ACTION_WHEN_CALLING_SIMPLE_DELETE_ELEM, INDEXES_NOT_FOUND_WHEN_IGNORE_ORDER, |
| 20 |
FAIL_TO_REMOVE_ITEM_IGNORE_ORDER_MSG, UNABLE_TO_GET_PATH_MSG, NOT_VALID_NUMPY_TYPE) |
| 21 |
from deepdiff.serialization import ( |
| 22 |
DELTA_IGNORE_ORDER_NEEDS_REPETITION_REPORT, DELTA_ERROR_WHEN_GROUP_BY, |
| 23 |
json_dumps, json_loads, |
| 24 |
) |
| 25 |
|
| 26 |
from tests import PicklableClass, parameterize_cases, CustomClass, CustomClass2 |
| 27 |
|
| 28 |
|
| 29 |
class TestBasicsOfDelta: |
| 30 |
|
| 31 |
def test_from_null_delta_json(self): |
| 32 |
t1 = None |
| 33 |
t2 = [1, 2, 3, 5] |
| 34 |
diff = DeepDiff(t1, t2) |
| 35 |
delta = Delta(diff, serializer=json_dumps) |
| 36 |
dump = delta.dumps() |
| 37 |
delta2 = Delta(dump, deserializer=json_loads) |
| 38 |
assert delta2 + t1 == t2 |
| 39 |
assert t1 + delta2 == t2 |
| 40 |
with pytest.raises(ValueError) as exc_info: |
| 41 |
t2 - delta |
| 42 |
assert 'Please recreate the delta with bidirectional=True' == str(exc_info.value) |
| 43 |
delta = Delta(diff, serializer=json_dumps, bidirectional=True) |
| 44 |
assert t2 - delta == t1 |
| 45 |
|
| 46 |
def test_to_null_delta1_json(self): |
| 47 |
t1 = 1 |
| 48 |
t2 = None |
| 49 |
diff = DeepDiff(t1, t2) |
| 50 |
delta = Delta(diff, serializer=json_dumps) |
| 51 |
dump = delta.dumps() |
| 52 |
delta2 = Delta(dump, deserializer=json_loads) |
| 53 |
assert delta2 + t1 == t2 |
| 54 |
assert t1 + delta2 == t2 |
| 55 |
|
| 56 |
def test_to_null_delta2_json(self): |
| 57 |
t1 = [1, 2, 3, 5] |
| 58 |
t2 = None |
| 59 |
diff = DeepDiff(t1, t2) |
| 60 |
delta = Delta(diff) |
| 61 |
|
| 62 |
assert delta + t1 == t2 |
| 63 |
assert t1 + delta == t2 |
| 64 |
|
| 65 |
def test_list_difference_add_delta(self): |
| 66 |
t1 = [1, 2] |
| 67 |
t2 = [1, 2, 3, 5] |
| 68 |
diff = {'iterable_item_added': {'root[3]': 5, 'root[2]': 3}} |
| 69 |
delta = Delta(diff) |
| 70 |
|
| 71 |
assert delta + t1 == t2 |
| 72 |
assert t1 + delta == t2 |
| 73 |
|
| 74 |
flat_result1 = delta.to_flat_rows() |
| 75 |
flat_expected1 = [ |
| 76 |
FlatDeltaRow(path=[3], value=5, action='iterable_item_added', type=int), |
| 77 |
FlatDeltaRow(path=[2], value=3, action='iterable_item_added', type=int), |
| 78 |
] |
| 79 |
|
| 80 |
assert flat_expected1 == flat_result1 |
| 81 |
delta2 = Delta(diff=diff, bidirectional=True) |
| 82 |
assert t1 == t2 - delta2 |
| 83 |
|
| 84 |
def test_list_difference_dump_delta(self): |
| 85 |
t1 = [1, 2] |
| 86 |
t2 = [1, 2, 3, 5] |
| 87 |
diff = DeepDiff(t1, t2) |
| 88 |
dump = Delta(diff).dumps() |
| 89 |
delta = Delta(dump) |
| 90 |
|
| 91 |
assert delta + t1 == t2 |
| 92 |
|
| 93 |
def test_multiple_delta(self): |
| 94 |
t1 = [1, 2] |
| 95 |
t2 = [1, 2, 3, 5] |
| 96 |
t3 = [{1}, 3, 5] |
| 97 |
dump1 = Delta(DeepDiff(t1, t2)).dumps() |
| 98 |
dump2 = Delta(DeepDiff(t2, t3)).dumps() |
| 99 |
|
| 100 |
delta1 = Delta(dump1) |
| 101 |
delta2 = Delta(dump2) |
| 102 |
|
| 103 |
assert t1 + delta1 + delta2 == t3 |
| 104 |
|
| 105 |
def test_delta_dump_and_read1(self, tmp_path): |
| 106 |
t1 = [1, 2] |
| 107 |
t2 = [1, 2, 3, 5] |
| 108 |
diff = DeepDiff(t1, t2) |
| 109 |
path = os.path.join(tmp_path, 'delta_test.delta') |
| 110 |
with open(path, 'wb') as the_file: |
| 111 |
Delta(diff).dump(the_file) |
| 112 |
delta = Delta(delta_path=path) |
| 113 |
os.remove(path) |
| 114 |
assert delta + t1 == t2 |
| 115 |
|
| 116 |
def test_delta_dump_and_read2(self, tmp_path): |
| 117 |
t1 = [1, 2] |
| 118 |
t2 = [1, 2, 3, 5] |
| 119 |
diff = DeepDiff(t1, t2) |
| 120 |
delta_content = Delta(diff).dumps() |
| 121 |
path = os.path.join(tmp_path, 'delta_test2.delta') |
| 122 |
with open(path, 'wb') as the_file: |
| 123 |
the_file.write(delta_content) |
| 124 |
delta = Delta(delta_path=path) |
| 125 |
os.remove(path) |
| 126 |
assert delta + t1 == t2 |
| 127 |
|
| 128 |
def test_delta_dump_and_read3(self, tmp_path): |
| 129 |
t1 = [1, 2] |
| 130 |
t2 = [1, 2, 3, 5] |
| 131 |
diff = DeepDiff(t1, t2) |
| 132 |
delta_content = Delta(diff).dumps() |
| 133 |
path = os.path.join(tmp_path, 'delta_test2.delta') |
| 134 |
with open(path, 'wb') as the_file: |
| 135 |
the_file.write(delta_content) |
| 136 |
with pytest.raises(ValueError) as excinfo: |
| 137 |
with open(path, 'r') as the_file: |
| 138 |
delta = Delta(delta_file=the_file) |
| 139 |
assert BINIARY_MODE_NEEDED_MSG[:20] == str(excinfo.value)[:20] |
| 140 |
with open(path, 'rb') as the_file: |
| 141 |
delta = Delta(delta_file=the_file) |
| 142 |
os.remove(path) |
| 143 |
assert delta + t1 == t2 |
| 144 |
|
| 145 |
def test_delta_when_no_arg_passed(self): |
| 146 |
with pytest.raises(ValueError) as excinfo: |
| 147 |
Delta() |
| 148 |
assert DELTA_AT_LEAST_ONE_ARG_NEEDED == str(excinfo.value) |
| 149 |
|
| 150 |
def test_delta_when_group_by(self): |
| 151 |
|
| 152 |
t1 = [ |
| 153 |
{'id': 'AA', 'name': 'Joe', 'last_name': 'Nobody'}, |
| 154 |
{'id': 'BB', 'name': 'James', 'last_name': 'Blue'}, |
| 155 |
] |
| 156 |
|
| 157 |
t2 = [ |
| 158 |
{'id': 'AA', 'name': 'Joe', 'last_name': 'Nobody'}, |
| 159 |
{'id': 'BB', 'name': 'James', 'last_name': 'Brown'}, |
| 160 |
] |
| 161 |
|
| 162 |
diff = DeepDiff(t1, t2, group_by='id') |
| 163 |
|
| 164 |
with pytest.raises(ValueError) as excinfo: |
| 165 |
Delta(diff) |
| 166 |
assert DELTA_ERROR_WHEN_GROUP_BY == str(excinfo.value) |
| 167 |
|
| 168 |
def test_delta_repr(self): |
| 169 |
t1 = [1, 2] |
| 170 |
t2 = [1, 2, 3, 5] |
| 171 |
diff = DeepDiff(t1, t2) |
| 172 |
delta = Delta(diff) |
| 173 |
options = { |
| 174 |
'<Delta: {"iterable_item_added":{"root[2]":3,"root[3]":5}}>', |
| 175 |
'<Delta: {"iterable_item_added":{"root[3]":5,"root[2]":3}}>', |
| 176 |
} |
| 177 |
assert repr(delta) in options |
| 178 |
|
| 179 |
def test_get_elem_and_compare_to_old_value(self): |
| 180 |
delta = Delta({}) |
| 181 |
|
| 182 |
with pytest.raises(DeltaError) as excinfo: |
| 183 |
delta._get_elem_and_compare_to_old_value( |
| 184 |
obj=None, path_for_err_reporting=None, expected_old_value=None, action='ketchup on steak') |
| 185 |
assert INVALID_ACTION_WHEN_CALLING_GET_ELEM.format('ketchup on steak') == str(excinfo.value) |
| 186 |
|
| 187 |
def test_simple_set_elem_value(self): |
| 188 |
delta = Delta({}, raise_errors=True) |
| 189 |
|
| 190 |
with pytest.raises(DeltaError) as excinfo: |
| 191 |
delta._simple_set_elem_value( |
| 192 |
obj=None, elem=None, value=None, action='mayo on salad', path_for_err_reporting=None) |
| 193 |
assert INVALID_ACTION_WHEN_CALLING_SIMPLE_SET_ELEM.format('mayo on salad') == str(excinfo.value) |
| 194 |
|
| 195 |
with pytest.raises(DeltaError) as excinfo: |
| 196 |
delta._simple_set_elem_value( |
| 197 |
obj={}, elem={1}, value=None, action=GET, path_for_err_reporting='mypath') |
| 198 |
assert str(excinfo.value) in {"Failed to set mypath due to unhashable type: 'set'", |
| 199 |
"Failed to set mypath due to 'set' objects are unhashable", |
| 200 |
"Failed to set mypath due to cannot use 'set' as a dict key (unhashable type: 'set')"} |
| 201 |
|
| 202 |
def test_simple_delete_elem(self): |
| 203 |
delta = Delta({}, raise_errors=True) |
| 204 |
|
| 205 |
with pytest.raises(DeltaError) as excinfo: |
| 206 |
delta._simple_delete_elem( |
| 207 |
obj=None, elem=None, action='burnt oil', path_for_err_reporting=None) |
| 208 |
assert INVALID_ACTION_WHEN_CALLING_SIMPLE_DELETE_ELEM.format('burnt oil') == str(excinfo.value) |
| 209 |
|
| 210 |
with pytest.raises(DeltaError) as excinfo: |
| 211 |
delta._simple_delete_elem( |
| 212 |
obj={}, elem=1, action=GET, path_for_err_reporting='mypath') |
| 213 |
assert "Failed to set mypath due to 1" == str(excinfo.value) |
| 214 |
|
| 215 |
def test_raise_error(self): |
| 216 |
t1 = [1, 2, [3, 5, 6]] |
| 217 |
t2 = [2, 3, [3, 6, 8]] |
| 218 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 219 |
delta = Delta(diff, raise_errors=False) |
| 220 |
t3 = [1, 2, 3, 5] |
| 221 |
t4 = t3 + delta |
| 222 |
assert [3, 2, 3, 5] == t4 |
| 223 |
|
| 224 |
delta2 = Delta(diff, raise_errors=True) |
| 225 |
|
| 226 |
with pytest.raises(DeltaError) as excinfo: |
| 227 |
t3 + delta2 |
| 228 |
assert "Unable to get the item at root[2][1]" == str(excinfo.value) |
| 229 |
|
| 230 |
def test_identical_delta(self): |
| 231 |
delta = Delta({}) |
| 232 |
|
| 233 |
t1 = [1, 3] |
| 234 |
assert t1 + delta == t1 |
| 235 |
|
| 236 |
flat_result1 = delta.to_flat_rows() |
| 237 |
flat_expected1 = [] |
| 238 |
|
| 239 |
assert flat_expected1 == flat_result1 |
| 240 |
|
| 241 |
def test_delta_mutate(self): |
| 242 |
t1 = [1, 2] |
| 243 |
t2 = [1, 2, 3, 5] |
| 244 |
diff = DeepDiff(t1, t2) |
| 245 |
delta = Delta(diff, mutate=True) |
| 246 |
t1 + delta |
| 247 |
assert t1 == t2 |
| 248 |
|
| 249 |
@mock.patch('deepdiff.delta.logger.error') |
| 250 |
def test_list_difference_add_delta_when_index_not_valid(self, mock_logger): |
| 251 |
t1 = [1, 2] |
| 252 |
diff = {'iterable_item_added': {'root[20]': 3, 'root[3]': 5}} |
| 253 |
delta = Delta(diff, log_errors=False) |
| 254 |
assert delta + t1 == t1 |
| 255 |
|
| 256 |
# since we sort the keys by the path elements, root[3] is gonna be processed before root[20] |
| 257 |
expected_msg = ELEM_NOT_FOUND_TO_ADD_MSG.format(3, 'root[3]') |
| 258 |
|
| 259 |
delta2 = Delta(diff, bidirectional=True, raise_errors=True, log_errors=False) |
| 260 |
with pytest.raises(ValueError) as excinfo: |
| 261 |
delta2 + t1 |
| 262 |
assert expected_msg == str(excinfo.value) |
| 263 |
assert not mock_logger.called |
| 264 |
|
| 265 |
delta3 = Delta(diff, bidirectional=True, raise_errors=True, log_errors=True) |
| 266 |
with pytest.raises(ValueError) as excinfo: |
| 267 |
delta3 + t1 |
| 268 |
assert expected_msg == str(excinfo.value) |
| 269 |
mock_logger.assert_called_once_with(expected_msg) |
| 270 |
|
| 271 |
def test_list_difference3_delta(self): |
| 272 |
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 5]}} |
| 273 |
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 3, 2, 5]}} |
| 274 |
diff = { |
| 275 |
'values_changed': { |
| 276 |
"root[4]['b'][2]": { |
| 277 |
'new_value': 2, |
| 278 |
'old_value': 5 |
| 279 |
}, |
| 280 |
"root[4]['b'][1]": { |
| 281 |
'new_value': 3, |
| 282 |
'old_value': 2 |
| 283 |
} |
| 284 |
}, |
| 285 |
'iterable_item_added': { |
| 286 |
"root[4]['b'][3]": 5 |
| 287 |
} |
| 288 |
} |
| 289 |
delta = Delta(diff) |
| 290 |
|
| 291 |
assert delta + t1 == t2 |
| 292 |
assert t1 + delta == t2 |
| 293 |
|
| 294 |
flat_result1 = delta.to_flat_rows() |
| 295 |
flat_expected1 = [ |
| 296 |
FlatDeltaRow(path=[4, 'b', 2], action='values_changed', value=2, old_value=5, type=int, old_type=int), |
| 297 |
FlatDeltaRow(path=[4, 'b', 1], action='values_changed', value=3, old_value=2, type=int, old_type=int), |
| 298 |
FlatDeltaRow(path=[4, 'b', 3], value=5, action='iterable_item_added', type=int), |
| 299 |
] |
| 300 |
|
| 301 |
assert flat_expected1 == flat_result1 |
| 302 |
|
| 303 |
delta2 = Delta(diff=diff, bidirectional=True) |
| 304 |
assert t1 == t2 - delta2 |
| 305 |
|
| 306 |
def test_list_difference_delta_raises_error_if_prev_value_does_not_match(self): |
| 307 |
t1 = [1, 2, 6] |
| 308 |
t2 = [1, 3, 2, 5] |
| 309 |
diff = { |
| 310 |
'values_changed': { |
| 311 |
"root[2]": { |
| 312 |
'new_value': 2, |
| 313 |
'old_value': 5 |
| 314 |
}, |
| 315 |
"root[1]": { |
| 316 |
'new_value': 3, |
| 317 |
'old_value': 2 |
| 318 |
} |
| 319 |
}, |
| 320 |
'iterable_item_added': { |
| 321 |
"root[3]": 5 |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
expected_msg = VERIFICATION_MSG.format('root[2]', 5, 6, VERIFY_BIDIRECTIONAL_MSG) |
| 326 |
|
| 327 |
delta = Delta(diff, bidirectional=True, raise_errors=True) |
| 328 |
with pytest.raises(ValueError) as excinfo: |
| 329 |
delta + t1 |
| 330 |
assert expected_msg == str(excinfo.value) |
| 331 |
|
| 332 |
delta2 = Delta(diff, bidirectional=False) |
| 333 |
assert delta2 + t1 == t2 |
| 334 |
|
| 335 |
flat_result2 = delta2.to_flat_rows() |
| 336 |
flat_expected2 = [ |
| 337 |
FlatDeltaRow(path=[2], action='values_changed', value=2, old_value=5, type=int, old_type=int), |
| 338 |
FlatDeltaRow(path=[1], action='values_changed', value=3, old_value=2, type=int, old_type=int), |
| 339 |
FlatDeltaRow(path=[3], value=5, action='iterable_item_added', type=int), |
| 340 |
] |
| 341 |
|
| 342 |
assert flat_expected2 == flat_result2 |
| 343 |
|
| 344 |
def test_list_difference_delta1(self): |
| 345 |
t1 = { |
| 346 |
1: 1, |
| 347 |
2: 2, |
| 348 |
3: 3, |
| 349 |
4: { |
| 350 |
"a": "hello", |
| 351 |
"b": [1, 2, 'to_be_removed', 'to_be_removed2'] |
| 352 |
} |
| 353 |
} |
| 354 |
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2]}} |
| 355 |
|
| 356 |
diff = { |
| 357 |
'iterable_item_removed': { |
| 358 |
"root[4]['b'][2]": "to_be_removed", |
| 359 |
"root[4]['b'][3]": 'to_be_removed2' |
| 360 |
} |
| 361 |
} |
| 362 |
delta = Delta(diff) |
| 363 |
|
| 364 |
assert delta + t1 == t2 |
| 365 |
|
| 366 |
flat_result = delta.to_flat_rows() |
| 367 |
flat_expected = [ |
| 368 |
FlatDeltaRow(path=[4, 'b', 2], value='to_be_removed', action='iterable_item_removed', type=str), |
| 369 |
FlatDeltaRow(path=[4, 'b', 3], value='to_be_removed2', action='iterable_item_removed', type=str), |
| 370 |
] |
| 371 |
|
| 372 |
assert flat_expected == flat_result |
| 373 |
|
| 374 |
delta2 = Delta(diff=diff, bidirectional=True) |
| 375 |
assert t1 == t2 - delta2 |
| 376 |
|
| 377 |
@mock.patch('deepdiff.delta.logger.error') |
| 378 |
def test_list_difference_delta_if_item_is_already_removed(self, mock_logger): |
| 379 |
t1 = [1, 2, 'to_be_removed'] |
| 380 |
t2 = [1, 2] |
| 381 |
|
| 382 |
diff = { |
| 383 |
'iterable_item_removed': { |
| 384 |
"root[2]": "to_be_removed", |
| 385 |
"root[3]": 'to_be_removed2' |
| 386 |
} |
| 387 |
} |
| 388 |
delta = Delta(diff, bidirectional=True, raise_errors=True) |
| 389 |
assert delta + t1 == t2, ( |
| 390 |
"We used to throw errors when the item to be removed was not found. " |
| 391 |
"Instead, we try to look for the item to be removed even when the " |
| 392 |
"index of it in delta is different than the index of it in the object." |
| 393 |
) |
| 394 |
|
| 395 |
delta2 = Delta(diff, bidirectional=False, raise_errors=False) |
| 396 |
assert t1 + delta2 == t2 |
| 397 |
expected_msg = UNABLE_TO_GET_PATH_MSG.format('root[3]') |
| 398 |
assert 0 == mock_logger.call_count |
| 399 |
|
| 400 |
def test_list_difference_delta_does_not_raise_error_if_prev_value_changed(self): |
| 401 |
t1 = { |
| 402 |
1: 1, |
| 403 |
2: 2, |
| 404 |
3: 3, |
| 405 |
4: { |
| 406 |
"a": "hello", |
| 407 |
"b": [1, 2, 'wrong', 'to_be_removed2'] |
| 408 |
} |
| 409 |
} |
| 410 |
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2]}} |
| 411 |
|
| 412 |
diff = { |
| 413 |
'iterable_item_removed': { |
| 414 |
"root[4]['b'][2]": "to_be_removed", |
| 415 |
"root[4]['b'][3]": 'to_be_removed2' |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
# The previous behavior was to throw an error here because the original value for "root[4]['b'][2]" was not 'wrong' anymore. |
| 420 |
# However, I decided to change that behavior to what makes more sense and is consistent with the bidirectional flag. |
| 421 |
# No more verify_symmetry flag. |
| 422 |
delta = Delta(diff, bidirectional=True, raise_errors=True) |
| 423 |
assert delta + t1 != t2 |
| 424 |
expected = {1: 1, 2: 2, 3: 3, 4: {'a': 'hello', 'b': [1, 2, 'wrong']}} |
| 425 |
assert expected == delta + t1 |
| 426 |
|
| 427 |
delta2 = Delta(diff, bidirectional=False, raise_errors=True) |
| 428 |
assert expected == t1 + delta2 |
| 429 |
|
| 430 |
def test_delta_dict_items_added_retain_order(self): |
| 431 |
t1 = { |
| 432 |
6: 6 |
| 433 |
} |
| 434 |
|
| 435 |
t2 = { |
| 436 |
6: 6, |
| 437 |
7: 7, |
| 438 |
3: 3, |
| 439 |
5: 5, |
| 440 |
2: 2, |
| 441 |
4: 4 |
| 442 |
} |
| 443 |
|
| 444 |
expected_delta_dict = { |
| 445 |
'dictionary_item_added': { |
| 446 |
'root[7]': 7, |
| 447 |
'root[3]': 3, |
| 448 |
'root[5]': 5, |
| 449 |
'root[2]': 2, |
| 450 |
'root[4]': 4 |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
diff = DeepDiff(t1, t2, threshold_to_diff_deeper=0) |
| 455 |
delta_dict = diff._to_delta_dict() |
| 456 |
assert expected_delta_dict == delta_dict |
| 457 |
delta = Delta(diff, bidirectional=False, raise_errors=True) |
| 458 |
|
| 459 |
result = t1 + delta |
| 460 |
assert result == t2 |
| 461 |
|
| 462 |
assert set(result.keys()) == {6, 7, 3, 5, 2, 4} |
| 463 |
assert set(result.keys()) == set(t2.keys()) |
| 464 |
|
| 465 |
delta2 = Delta(diff=diff, bidirectional=True) |
| 466 |
assert t1 == t2 - delta2 |
| 467 |
|
| 468 |
delta3 = Delta(diff, always_include_values=True, bidirectional=True, raise_errors=True) |
| 469 |
flat_rows_list = delta3.to_flat_rows() |
| 470 |
delta4 = Delta(flat_rows_list=flat_rows_list, |
| 471 |
always_include_values=True, bidirectional=True, raise_errors=True) |
| 472 |
assert t1 == t2 - delta4 |
| 473 |
assert t1 + delta4 == t2 |
| 474 |
|
| 475 |
|
| 476 |
def test_delta_constr_flat_dict_list_param_preserve(self): |
| 477 |
""" |
| 478 |
Issue: https://github.com/seperman/deepdiff/issues/457 |
| 479 |
|
| 480 |
Scenario: |
| 481 |
We found that when a flat_rows_list was provided as a constructor |
| 482 |
parameter for instantiating a new delta, the provided flat_rows_list |
| 483 |
is unexpectedly being mutated/changed, which can be troublesome for the |
| 484 |
caller if they were expecting the flat_rows_list to be used BY COPY |
| 485 |
rather than BY REFERENCE. |
| 486 |
|
| 487 |
Intent: |
| 488 |
Preserve the original value of the flat_rows_list variable within the |
| 489 |
calling module/function after instantiating the new delta. |
| 490 |
""" |
| 491 |
|
| 492 |
t1 = { |
| 493 |
"individualNames": [ |
| 494 |
{ |
| 495 |
"firstName": "Johnathan", |
| 496 |
"lastName": "Doe", |
| 497 |
"prefix": "COLONEL", |
| 498 |
"middleName": "A", |
| 499 |
"primaryIndicator": True, |
| 500 |
"professionalDesignation": "PHD", |
| 501 |
"suffix": "SR", |
| 502 |
"nameIdentifier": "00001" |
| 503 |
}, |
| 504 |
{ |
| 505 |
"firstName": "John", |
| 506 |
"lastName": "Doe", |
| 507 |
"prefix": "", |
| 508 |
"middleName": "", |
| 509 |
"primaryIndicator": False, |
| 510 |
"professionalDesignation": "", |
| 511 |
"suffix": "SR", |
| 512 |
"nameIdentifier": "00002" |
| 513 |
} |
| 514 |
] |
| 515 |
} |
| 516 |
|
| 517 |
t2 = { |
| 518 |
"individualNames": [ |
| 519 |
{ |
| 520 |
"firstName": "Johnathan", |
| 521 |
"lastName": "Doe", |
| 522 |
"prefix": "COLONEL", |
| 523 |
"middleName": "A", |
| 524 |
"primaryIndicator": True, |
| 525 |
"professionalDesignation": "PHD", |
| 526 |
"suffix": "SR", |
| 527 |
"nameIdentifier": "00001" |
| 528 |
}, |
| 529 |
{ |
| 530 |
"firstName": "Johnny", |
| 531 |
"lastName": "Doe", |
| 532 |
"prefix": "", |
| 533 |
"middleName": "A", |
| 534 |
"primaryIndicator": False, |
| 535 |
"professionalDesignation": "", |
| 536 |
"suffix": "SR", |
| 537 |
"nameIdentifier": "00003" |
| 538 |
} |
| 539 |
] |
| 540 |
} |
| 541 |
|
| 542 |
def compare_func(item1, item2, level=None): |
| 543 |
print("*** inside compare ***") |
| 544 |
it1_keys = item1.keys() |
| 545 |
|
| 546 |
try: |
| 547 |
|
| 548 |
# --- individualNames --- |
| 549 |
if 'nameIdentifier' in it1_keys and 'lastName' in it1_keys: |
| 550 |
match_result = item1['nameIdentifier'] == item2['nameIdentifier'] |
| 551 |
print("individualNames - matching result:", match_result) |
| 552 |
return match_result |
| 553 |
else: |
| 554 |
print("Unknown list item...", "matching result:", item1 == item2) |
| 555 |
return item1 == item2 |
| 556 |
except Exception: |
| 557 |
raise CannotCompare() from None |
| 558 |
# ---------------------------- End of nested function |
| 559 |
|
| 560 |
# This diff should show: |
| 561 |
# 1 - list item (with an index on the path) being added |
| 562 |
# 1 - list item (with an index on the path) being removed |
| 563 |
diff = DeepDiff(t1, t2, report_repetition=True, |
| 564 |
ignore_order=True, iterable_compare_func=compare_func, cutoff_intersection_for_pairs=1) |
| 565 |
|
| 566 |
# Now create a flat_rows_list from a delta instantiated from the diff... |
| 567 |
temp_delta = Delta(diff, always_include_values=True, bidirectional=True, raise_errors=True) |
| 568 |
flat_rows_list = temp_delta.to_flat_rows() |
| 569 |
|
| 570 |
# Note: the list index is provided on the path value... |
| 571 |
assert flat_rows_list == [FlatDeltaRow(path=['individualNames', 1], |
| 572 |
value={'firstName': 'Johnny', |
| 573 |
'lastName': 'Doe', |
| 574 |
'prefix': '', |
| 575 |
'middleName': 'A', |
| 576 |
'primaryIndicator': False, |
| 577 |
'professionalDesignation': '', |
| 578 |
'suffix': 'SR', |
| 579 |
'nameIdentifier': '00003'}, |
| 580 |
action='unordered_iterable_item_added', |
| 581 |
type=dict), |
| 582 |
FlatDeltaRow(path=['individualNames', 1], |
| 583 |
value={'firstName': 'John', |
| 584 |
'lastName': 'Doe', |
| 585 |
'prefix': '', |
| 586 |
'middleName': '', |
| 587 |
'primaryIndicator': False, |
| 588 |
'professionalDesignation': '', |
| 589 |
'suffix': 'SR', |
| 590 |
'nameIdentifier': '00002'}, |
| 591 |
action='unordered_iterable_item_removed', |
| 592 |
type=dict), |
| 593 |
] |
| 594 |
|
| 595 |
preserved_flat_dict_list = copy.deepcopy(flat_rows_list) # Use this later for assert comparison |
| 596 |
|
| 597 |
# Now use the flat_rows_list to instantiate a new delta... |
| 598 |
delta = Delta(flat_rows_list=flat_rows_list, |
| 599 |
always_include_values=True, bidirectional=True, raise_errors=True) |
| 600 |
|
| 601 |
flat_rows_list_again = delta.to_flat_rows() |
| 602 |
# if the flat_rows_list is (unexpectedly) mutated, it will be missing the list index number on the path value. |
| 603 |
old_mutated_list_missing_indexes_on_path = [FlatDeltaRow(path=['individualNames'], |
| 604 |
value={'firstName': 'Johnny', |
| 605 |
'lastName': 'Doe', |
| 606 |
'prefix': '', |
| 607 |
'middleName': 'A', |
| 608 |
'primaryIndicator': False, |
| 609 |
'professionalDesignation': '', |
| 610 |
'suffix': 'SR', |
| 611 |
'nameIdentifier': '00003'}, |
| 612 |
action='unordered_iterable_item_added'), |
| 613 |
FlatDeltaRow(path=['individualNames'], |
| 614 |
value={'firstName': 'John', |
| 615 |
'lastName': 'Doe', |
| 616 |
'prefix': '', |
| 617 |
'middleName': '', |
| 618 |
'primaryIndicator': False, |
| 619 |
'professionalDesignation': '', |
| 620 |
'suffix': 'SR', |
| 621 |
'nameIdentifier': '00002'}, |
| 622 |
action='unordered_iterable_item_removed')] |
| 623 |
|
| 624 |
# Verify that our fix in the delta constructor worked... |
| 625 |
assert flat_rows_list != old_mutated_list_missing_indexes_on_path |
| 626 |
assert flat_rows_list == preserved_flat_dict_list |
| 627 |
assert flat_rows_list == flat_rows_list_again |
| 628 |
|
| 629 |
def test_namedtuple_add_delta(self): |
| 630 |
class Point(NamedTuple): |
| 631 |
x: int |
| 632 |
y: int |
| 633 |
|
| 634 |
p1 = Point(1, 1) |
| 635 |
p2 = Point(1, 2) |
| 636 |
diff = DeepDiff(p1, p2) |
| 637 |
delta = Delta(diff) |
| 638 |
assert p2 == p1 + delta |
| 639 |
|
| 640 |
def test_namedtuple_frozenset_add_delta(self): |
| 641 |
class Article(NamedTuple): |
| 642 |
tags: frozenset |
| 643 |
a1 = Article(frozenset(["a" ])) |
| 644 |
a2 = Article(frozenset(["a", "b"])) |
| 645 |
diff = DeepDiff(a1, a2) |
| 646 |
delta = Delta(diff) |
| 647 |
assert a2 == a1 + delta |
| 648 |
|
| 649 |
picklalbe_obj_without_item = PicklableClass(11) |
| 650 |
del picklalbe_obj_without_item.item |
| 651 |
|
| 652 |
|
| 653 |
DELTA_CASES = { |
| 654 |
'delta_case0': { |
| 655 |
't1': frozenset([1, 2, 'B']), |
| 656 |
't2': frozenset([1, 2, 'B']), |
| 657 |
'deepdiff_kwargs': {}, |
| 658 |
'to_delta_kwargs': {}, |
| 659 |
'expected_delta_dict': {}, |
| 660 |
}, |
| 661 |
'delta_case1': { |
| 662 |
't1': frozenset([1, 2, 'B']), |
| 663 |
't2': frozenset([1, 2, 3, 5]), |
| 664 |
'deepdiff_kwargs': {}, |
| 665 |
'to_delta_kwargs': {}, |
| 666 |
'expected_delta_dict': {'set_item_removed': {'root': {'B'}}, 'set_item_added': {'root': {3, 5}}}, |
| 667 |
}, |
| 668 |
'delta_case2': { |
| 669 |
't1': [1, 2, 'B'], |
| 670 |
't2': [1, 2, 3, 5], |
| 671 |
'deepdiff_kwargs': {}, |
| 672 |
'to_delta_kwargs': {}, |
| 673 |
'expected_delta_dict': { |
| 674 |
'type_changes': { |
| 675 |
'root[2]': { |
| 676 |
'old_type': str, |
| 677 |
'new_type': int, |
| 678 |
'new_value': 3 |
| 679 |
} |
| 680 |
}, |
| 681 |
'iterable_item_added': { |
| 682 |
'root[3]': 5 |
| 683 |
} |
| 684 |
}, |
| 685 |
}, |
| 686 |
'delta_case3': { |
| 687 |
't1': [1, 2, '3'], |
| 688 |
't2': [1, 2, 3], |
| 689 |
'deepdiff_kwargs': {}, |
| 690 |
'to_delta_kwargs': {}, |
| 691 |
'expected_delta_dict': { |
| 692 |
'type_changes': { |
| 693 |
'root[2]': { |
| 694 |
'old_type': str, |
| 695 |
'new_type': int, |
| 696 |
} |
| 697 |
} |
| 698 |
}, |
| 699 |
}, |
| 700 |
'delta_case4': { |
| 701 |
't1': 3, |
| 702 |
't2': '3', |
| 703 |
'deepdiff_kwargs': {}, |
| 704 |
'to_delta_kwargs': {}, |
| 705 |
'expected_delta_dict': { |
| 706 |
'type_changes': { |
| 707 |
'root': { |
| 708 |
'old_type': int, |
| 709 |
'new_type': str, |
| 710 |
} |
| 711 |
} |
| 712 |
}, |
| 713 |
}, |
| 714 |
'delta_case5': { |
| 715 |
't1': 3.2, |
| 716 |
't2': Decimal('3.2'), |
| 717 |
'deepdiff_kwargs': {}, |
| 718 |
'to_delta_kwargs': {}, |
| 719 |
'expected_delta_dict': { |
| 720 |
'type_changes': { |
| 721 |
'root': { |
| 722 |
'old_type': float, |
| 723 |
'new_type': Decimal, |
| 724 |
'new_value': Decimal('3.2') |
| 725 |
} |
| 726 |
} |
| 727 |
}, |
| 728 |
}, |
| 729 |
'delta_case6': { |
| 730 |
't1': (1, 2), |
| 731 |
't2': (1, 3), |
| 732 |
'deepdiff_kwargs': {}, |
| 733 |
'to_delta_kwargs': {}, |
| 734 |
'expected_delta_dict': { |
| 735 |
'values_changed': { |
| 736 |
'root[1]': { |
| 737 |
'new_value': 3 |
| 738 |
} |
| 739 |
} |
| 740 |
}, |
| 741 |
}, |
| 742 |
'delta_case7': { |
| 743 |
't1': (1, 2, 5), |
| 744 |
't2': (1, ), |
| 745 |
'deepdiff_kwargs': {}, |
| 746 |
'to_delta_kwargs': {}, |
| 747 |
'expected_delta_dict': { |
| 748 |
'iterable_item_removed': { |
| 749 |
'root[1]': 2, |
| 750 |
'root[2]': 5 |
| 751 |
} |
| 752 |
}, |
| 753 |
}, |
| 754 |
'delta_case8': { |
| 755 |
't1': (1, 2, 5), |
| 756 |
't2': (1, 3), |
| 757 |
'deepdiff_kwargs': {}, |
| 758 |
'to_delta_kwargs': {}, |
| 759 |
'expected_delta_dict': { |
| 760 |
'iterable_item_removed': { |
| 761 |
'root[2]': 5 |
| 762 |
}, |
| 763 |
'values_changed': { |
| 764 |
'root[1]': { |
| 765 |
'new_value': 3 |
| 766 |
} |
| 767 |
} |
| 768 |
}, |
| 769 |
}, |
| 770 |
'delta_case9': { |
| 771 |
't1': (1, ), |
| 772 |
't2': (1, 3), |
| 773 |
'deepdiff_kwargs': {}, |
| 774 |
'to_delta_kwargs': {}, |
| 775 |
'expected_delta_dict': { |
| 776 |
'iterable_item_added': { |
| 777 |
'root[1]': 3 |
| 778 |
}, |
| 779 |
}, |
| 780 |
}, |
| 781 |
'delta_case10': { |
| 782 |
't1': { |
| 783 |
2: 2 |
| 784 |
}, |
| 785 |
't2': { |
| 786 |
2: 2, |
| 787 |
3: 3 |
| 788 |
}, |
| 789 |
'deepdiff_kwargs': {}, |
| 790 |
'to_delta_kwargs': {}, |
| 791 |
'expected_delta_dict': { |
| 792 |
'dictionary_item_added': { |
| 793 |
'root[3]': 3 |
| 794 |
}, |
| 795 |
}, |
| 796 |
}, |
| 797 |
'delta_case11': { |
| 798 |
't1': { |
| 799 |
1: 1, |
| 800 |
2: 2 |
| 801 |
}, |
| 802 |
't2': { |
| 803 |
2: 2, |
| 804 |
3: 3 |
| 805 |
}, |
| 806 |
'deepdiff_kwargs': {}, |
| 807 |
'to_delta_kwargs': {}, |
| 808 |
'expected_delta_dict': { |
| 809 |
'dictionary_item_added': { |
| 810 |
'root[3]': 3 |
| 811 |
}, |
| 812 |
'dictionary_item_removed': { |
| 813 |
'root[1]': 1 |
| 814 |
}, |
| 815 |
}, |
| 816 |
}, |
| 817 |
'delta_case12': { |
| 818 |
't1': PicklableClass(10), |
| 819 |
't2': PicklableClass(11), |
| 820 |
'deepdiff_kwargs': {}, |
| 821 |
'to_delta_kwargs': {}, |
| 822 |
'expected_delta_dict': { |
| 823 |
'values_changed': { |
| 824 |
'root.item': { |
| 825 |
'new_value': 11 |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
}, |
| 830 |
'delta_case13': { |
| 831 |
't1': PicklableClass(10), |
| 832 |
't2': picklalbe_obj_without_item, |
| 833 |
'deepdiff_kwargs': {}, |
| 834 |
'to_delta_kwargs': {}, |
| 835 |
'expected_delta_dict': { |
| 836 |
'attribute_removed': { |
| 837 |
'root.item': 10 |
| 838 |
} |
| 839 |
} |
| 840 |
}, |
| 841 |
'delta_case14': { |
| 842 |
't1': picklalbe_obj_without_item, |
| 843 |
't2': PicklableClass(10), |
| 844 |
'deepdiff_kwargs': {}, |
| 845 |
'to_delta_kwargs': {}, |
| 846 |
'expected_delta_dict': { |
| 847 |
'attribute_added': { |
| 848 |
'root.item': 10 |
| 849 |
} |
| 850 |
} |
| 851 |
}, |
| 852 |
'delta_case14b_threshold_to_diff_deeper': { |
| 853 |
't1': picklalbe_obj_without_item, |
| 854 |
't2': PicklableClass(11), |
| 855 |
'deepdiff_kwargs': {'threshold_to_diff_deeper': 0.5}, |
| 856 |
'to_delta_kwargs': {}, |
| 857 |
'expected_delta_dict': {'attribute_added': {'root.item': 11}} |
| 858 |
}, |
| 859 |
'delta_case15_diffing_simple_numbers': { |
| 860 |
't1': 1, |
| 861 |
't2': 2, |
| 862 |
'deepdiff_kwargs': {}, |
| 863 |
'to_delta_kwargs': {}, |
| 864 |
'expected_delta_dict': {'values_changed': {'root': {'new_value': 2}}} |
| 865 |
}, |
| 866 |
'delta_case16_diffmultiline_text': { |
| 867 |
't1': {1: 1, 2: 2, 3: 3, 4: {'a': 'hello', 'b': 'world\n1\n2\nEnd'}}, |
| 868 |
't2': {1: 1, 2: 2, 3: 3, 4: {'a': 'hello', 'b': 'world!\nGoodbye!\n1\n2\nEnd'}}, |
| 869 |
'deepdiff_kwargs': {}, |
| 870 |
'to_delta_kwargs': {}, |
| 871 |
'expected_delta_dict': {'values_changed': {"root[4]['b']": {'new_value': 'world!\nGoodbye!\n1\n2\nEnd'}}} |
| 872 |
}, |
| 873 |
'delta_case17_numbers_and_letters': { |
| 874 |
't1': [0, 1, 2, 3, 4, 5, 6, 7, 8], |
| 875 |
't2': [0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c'], |
| 876 |
'deepdiff_kwargs': {}, |
| 877 |
'to_delta_kwargs': {}, |
| 878 |
'expected_delta_dict': {'iterable_item_added': {'root[9]': 'a', 'root[10]': 'b', 'root[11]': 'c'}} |
| 879 |
}, |
| 880 |
'delta_case18_numbers_and_letters': { |
| 881 |
't1': [0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c'], |
| 882 |
't2': [0, 1, 2, 3, 4, 5, 6, 7, 8], |
| 883 |
'deepdiff_kwargs': {}, |
| 884 |
'to_delta_kwargs': {}, |
| 885 |
'expected_delta_dict': {'iterable_item_removed': {'root[9]': 'a', 'root[10]': 'b', 'root[11]': 'c'}} |
| 886 |
}, |
| 887 |
'delta_case19_value_removed_from_the_middle_of_list': { |
| 888 |
't1': [0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c'], |
| 889 |
't2': [0, 1, 2, 3, 5, 6, 7, 8, 'a', 'b', 'c'], |
| 890 |
'deepdiff_kwargs': {}, |
| 891 |
'to_delta_kwargs': {'directed': True}, |
| 892 |
'expected_delta_dict': {'iterable_item_removed': {'root[4]': 4}} |
| 893 |
}, |
| 894 |
'delta_case20_quotes_in_path': { |
| 895 |
't1': {"a']['b']['c": 1}, |
| 896 |
't2': {"a']['b']['c": 2}, |
| 897 |
'deepdiff_kwargs': {}, |
| 898 |
'to_delta_kwargs': {'directed': True}, |
| 899 |
'expected_delta_dict': {'values_changed': {'root["a\'][\'b\'][\'c"]': {'new_value': 2}}} |
| 900 |
}, |
| 901 |
'delta_case21_empty_list_add': { |
| 902 |
't1': {'car_model': [], 'car_model_version_id': 0}, |
| 903 |
't2': {'car_model': ['Super Duty F-250'], 'car_model_version_id': 1}, |
| 904 |
'deepdiff_kwargs': {}, |
| 905 |
'to_delta_kwargs': {'directed': True}, |
| 906 |
'expected_delta_dict': {'iterable_item_added': {"root['car_model'][0]": 'Super Duty F-250'}, 'values_changed': {"root['car_model_version_id']": {'new_value': 1}}}, |
| 907 |
}, |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
DELTA_CASES_PARAMS = parameterize_cases('test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict', DELTA_CASES) |
| 912 |
|
| 913 |
|
| 914 |
class TestDelta: |
| 915 |
|
| 916 |
@pytest.mark.parametrize(**DELTA_CASES_PARAMS) |
| 917 |
def test_delta_cases(self, test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict): |
| 918 |
diff = DeepDiff(t1, t2, **deepdiff_kwargs) |
| 919 |
delta_dict = diff._to_delta_dict(**to_delta_kwargs) |
| 920 |
assert expected_delta_dict == delta_dict, f"test_delta_cases {test_name} failed." |
| 921 |
delta = Delta(diff, bidirectional=False, raise_errors=True) |
| 922 |
assert t1 + delta == t2, f"test_delta_cases {test_name} failed." |
| 923 |
|
| 924 |
delta2 = Delta(diff, bidirectional=True, raise_errors=True) |
| 925 |
assert t2 - delta2 == t1, f"test_delta_cases {test_name} failed." |
| 926 |
|
| 927 |
|
| 928 |
DELTA_IGNORE_ORDER_CASES = { |
| 929 |
'delta_ignore_order_case1': { |
| 930 |
't1': [1, 2, 'B', 3], |
| 931 |
't2': [1, 2, 3, 5], |
| 932 |
'deepdiff_kwargs': { |
| 933 |
'ignore_order': True, |
| 934 |
'report_repetition': True |
| 935 |
}, |
| 936 |
'to_delta_kwargs': {}, |
| 937 |
'expected_delta_dict': { |
| 938 |
'iterable_items_added_at_indexes': { |
| 939 |
'root': { |
| 940 |
3: 5 |
| 941 |
} |
| 942 |
}, |
| 943 |
'iterable_items_removed_at_indexes': { |
| 944 |
'root': { |
| 945 |
2: 'B' |
| 946 |
} |
| 947 |
} |
| 948 |
}, |
| 949 |
'expected_t1_plus_delta': 't2', |
| 950 |
}, |
| 951 |
'delta_ignore_order_case2': { |
| 952 |
't1': [1, 2, 'B', 3, 'B', 'B', 4], |
| 953 |
't2': [1, 2, 3, 5], |
| 954 |
'deepdiff_kwargs': { |
| 955 |
'ignore_order': True, |
| 956 |
'report_repetition': True |
| 957 |
}, |
| 958 |
'to_delta_kwargs': {}, |
| 959 |
'expected_delta_dict': { |
| 960 |
'values_changed': { |
| 961 |
'root[6]': { |
| 962 |
'new_value': 5, |
| 963 |
'new_path': 'root[3]', |
| 964 |
}, |
| 965 |
}, |
| 966 |
'iterable_items_removed_at_indexes': { |
| 967 |
'root': { |
| 968 |
2: 'B', |
| 969 |
4: 'B', |
| 970 |
5: 'B' |
| 971 |
} |
| 972 |
} |
| 973 |
}, |
| 974 |
'expected_t1_plus_delta': 't2', |
| 975 |
}, |
| 976 |
'delta_ignore_order_case_reverse2': { |
| 977 |
't1': [1, 2, 3, 5], |
| 978 |
't2': [1, 2, 'B', 3, 'B', 'B', 4], |
| 979 |
'deepdiff_kwargs': { |
| 980 |
'ignore_order': True, |
| 981 |
'report_repetition': True |
| 982 |
}, |
| 983 |
'to_delta_kwargs': {}, |
| 984 |
'expected_delta_dict': { |
| 985 |
'values_changed': { |
| 986 |
'root[3]': { |
| 987 |
'new_value': 4, |
| 988 |
'new_path': 'root[6]', |
| 989 |
}, |
| 990 |
}, |
| 991 |
'iterable_items_added_at_indexes': { |
| 992 |
'root': { |
| 993 |
2: 'B', |
| 994 |
4: 'B', |
| 995 |
5: 'B' |
| 996 |
} |
| 997 |
} |
| 998 |
}, |
| 999 |
'expected_t1_plus_delta': 't2', |
| 1000 |
}, |
| 1001 |
'delta_ignore_order_case3': { |
| 1002 |
't1': [5, 1, 1, 1, 6], |
| 1003 |
't2': [7, 1, 1, 1, 8], |
| 1004 |
'deepdiff_kwargs': { |
| 1005 |
'ignore_order': True, |
| 1006 |
'report_repetition': True |
| 1007 |
}, |
| 1008 |
'to_delta_kwargs': {}, |
| 1009 |
'expected_delta_dict': { |
| 1010 |
'values_changed': { |
| 1011 |
'root[4]': { |
| 1012 |
'new_value': 7, |
| 1013 |
'new_path': 'root[0]' |
| 1014 |
}, |
| 1015 |
'root[0]': { |
| 1016 |
'new_value': 8, |
| 1017 |
'new_path': 'root[4]' |
| 1018 |
} |
| 1019 |
} |
| 1020 |
}, |
| 1021 |
'expected_t1_plus_delta': [8, 1, 1, 1, 7], |
| 1022 |
}, |
| 1023 |
'delta_ignore_order_case4': { |
| 1024 |
't1': [5, 1, 3, 1, 4, 4, 6], |
| 1025 |
't2': [7, 4, 4, 1, 3, 4, 8], |
| 1026 |
'deepdiff_kwargs': { |
| 1027 |
'ignore_order': True, |
| 1028 |
'report_repetition': True |
| 1029 |
}, |
| 1030 |
'to_delta_kwargs': {}, |
| 1031 |
'expected_delta_dict': { |
| 1032 |
'values_changed': { |
| 1033 |
'root[6]': { |
| 1034 |
'new_value': 7, |
| 1035 |
'new_path': 'root[0]' |
| 1036 |
}, |
| 1037 |
'root[0]': { |
| 1038 |
'new_value': 8, |
| 1039 |
'new_path': 'root[6]' |
| 1040 |
} |
| 1041 |
}, |
| 1042 |
'iterable_items_added_at_indexes': { |
| 1043 |
'root': { |
| 1044 |
1: 4, |
| 1045 |
2: 4, |
| 1046 |
5: 4, |
| 1047 |
3: 1, |
| 1048 |
} |
| 1049 |
} |
| 1050 |
}, |
| 1051 |
'expected_t1_plus_delta': [8, 4, 4, 1, 3, 4, 7], |
| 1052 |
}, |
| 1053 |
'delta_ignore_order_case5': { |
| 1054 |
't1': (5, 1, 3, 1, 4, 4, 6), |
| 1055 |
't2': (7, 4, 4, 1, 3, 4, 8, 1), |
| 1056 |
'deepdiff_kwargs': { |
| 1057 |
'ignore_order': True, |
| 1058 |
'report_repetition': True |
| 1059 |
}, |
| 1060 |
'to_delta_kwargs': {}, |
| 1061 |
'expected_delta_dict': { |
| 1062 |
'iterable_items_added_at_indexes': { |
| 1063 |
'root': { |
| 1064 |
1: 4, |
| 1065 |
2: 4, |
| 1066 |
5: 4 |
| 1067 |
} |
| 1068 |
}, |
| 1069 |
'values_changed': { |
| 1070 |
'root[6]': { |
| 1071 |
'new_value': 7, |
| 1072 |
'new_path': 'root[0]', |
| 1073 |
}, |
| 1074 |
'root[0]': { |
| 1075 |
'new_value': 8, |
| 1076 |
'new_path': 'root[6]', |
| 1077 |
} |
| 1078 |
} |
| 1079 |
}, |
| 1080 |
'expected_t1_plus_delta': (8, 4, 4, 1, 3, 4, 1, 7), |
| 1081 |
}, |
| 1082 |
'delta_ignore_order_case6': { |
| 1083 |
't1': [{1, 2, 3}, {4, 5}], |
| 1084 |
't2': [{4, 5, 6}, {1, 2, 3}], |
| 1085 |
'deepdiff_kwargs': { |
| 1086 |
'ignore_order': True, |
| 1087 |
'report_repetition': True |
| 1088 |
}, |
| 1089 |
'to_delta_kwargs': {}, |
| 1090 |
'expected_delta_dict': {'set_item_added': {'root[1]': {6}}}, |
| 1091 |
'expected_t1_plus_delta': [{1, 2, 3}, {4, 5, 6}], |
| 1092 |
}, |
| 1093 |
'delta_ignore_order_case7': { |
| 1094 |
't1': [{1, 2, 3}, {4, 5, 'hello', 'right!'}, {4, 5, (2, 4, 7)}], |
| 1095 |
't2': [{4, 5, 6, (2, )}, {1, 2, 3}, {5, 'hello', 'right!'}], |
| 1096 |
'deepdiff_kwargs': { |
| 1097 |
'ignore_order': True, |
| 1098 |
'report_repetition': True |
| 1099 |
}, |
| 1100 |
'to_delta_kwargs': {}, |
| 1101 |
'expected_delta_dict': { |
| 1102 |
'set_item_removed': { |
| 1103 |
'root[1]': {4} |
| 1104 |
}, |
| 1105 |
'iterable_items_added_at_indexes': { |
| 1106 |
'root': { |
| 1107 |
0: {(2, ), 4, 5, 6} |
| 1108 |
} |
| 1109 |
}, |
| 1110 |
'iterable_items_removed_at_indexes': { |
| 1111 |
'root': { |
| 1112 |
2: {4, 5, (2, 4, 7)} |
| 1113 |
} |
| 1114 |
} |
| 1115 |
}, |
| 1116 |
'expected_t1_plus_delta': 't2', |
| 1117 |
}, |
| 1118 |
'delta_ignore_order_case8_multi_dimensional_list': { |
| 1119 |
't1': [[1, 2, 3, 4], [4, 2, 2, 1]], |
| 1120 |
't2': [[4, 1, 1, 1], [1, 3, 2, 4]], |
| 1121 |
'deepdiff_kwargs': { |
| 1122 |
'ignore_order': True, |
| 1123 |
'report_repetition': True |
| 1124 |
}, |
| 1125 |
'to_delta_kwargs': {}, |
| 1126 |
'expected_delta_dict': { |
| 1127 |
'iterable_items_added_at_indexes': { |
| 1128 |
'root[1]': { |
| 1129 |
1: 1, |
| 1130 |
2: 1, |
| 1131 |
3: 1 |
| 1132 |
} |
| 1133 |
}, |
| 1134 |
'iterable_items_removed_at_indexes': { |
| 1135 |
'root[1]': { |
| 1136 |
1: 2, |
| 1137 |
2: 2 |
| 1138 |
} |
| 1139 |
} |
| 1140 |
}, |
| 1141 |
'expected_t1_plus_delta': [[1, 2, 3, 4], [4, 1, 1, 1]], |
| 1142 |
}, |
| 1143 |
'delta_ignore_order_case9': { |
| 1144 |
't1': [{ |
| 1145 |
"path": ["interface1", "ipv1"] |
| 1146 |
}, { |
| 1147 |
"path": ["interface2", "ipv2"] |
| 1148 |
}, { |
| 1149 |
"path": ["interface3", "ipv3"] |
| 1150 |
}, { |
| 1151 |
"path": [{ |
| 1152 |
"test0": "interface4.0", |
| 1153 |
"test0.0": "ipv4.0" |
| 1154 |
}, { |
| 1155 |
"test1": "interface4.1", |
| 1156 |
"test1.1": "ipv4.1" |
| 1157 |
}] |
| 1158 |
}, { |
| 1159 |
"path": ["interface5", "ipv5"] |
| 1160 |
}], |
| 1161 |
't2': [{ |
| 1162 |
"path": ["interface1", "ipv1"] |
| 1163 |
}, { |
| 1164 |
"path": ["interface3", "ipv3"] |
| 1165 |
}, { |
| 1166 |
"path": [{ |
| 1167 |
"test0": "interface4.0", |
| 1168 |
"test0.0": "ipv4.0" |
| 1169 |
}, { |
| 1170 |
"test2": "interface4.2", |
| 1171 |
"test2.2": "ipv4.0" |
| 1172 |
}, { |
| 1173 |
"test1": "interface4.1", |
| 1174 |
"test1.1": "ipv4.1" |
| 1175 |
}] |
| 1176 |
}, { |
| 1177 |
"path": ["interface6", "ipv6"] |
| 1178 |
}, { |
| 1179 |
"path": ["interface5", "ipv5"] |
| 1180 |
}], |
| 1181 |
'deepdiff_kwargs': { |
| 1182 |
'ignore_order': True, |
| 1183 |
'report_repetition': True |
| 1184 |
}, |
| 1185 |
'to_delta_kwargs': {}, |
| 1186 |
'expected_delta_dict': { |
| 1187 |
'iterable_items_added_at_indexes': { |
| 1188 |
"root[3]['path']": { |
| 1189 |
1: { |
| 1190 |
'test2': 'interface4.2', |
| 1191 |
'test2.2': 'ipv4.0' |
| 1192 |
} |
| 1193 |
}, |
| 1194 |
'root': { |
| 1195 |
3: { |
| 1196 |
'path': [ |
| 1197 |
'interface6', 'ipv6' |
| 1198 |
] |
| 1199 |
} |
| 1200 |
} |
| 1201 |
}, |
| 1202 |
'iterable_items_removed_at_indexes': { |
| 1203 |
'root': { |
| 1204 |
1: { |
| 1205 |
'path': ['interface2', 'ipv2'] |
| 1206 |
} |
| 1207 |
} |
| 1208 |
} |
| 1209 |
}, |
| 1210 |
'expected_t1_plus_delta': |
| 1211 |
't2', |
| 1212 |
}, |
| 1213 |
} |
| 1214 |
|
| 1215 |
DELTA_IGNORE_ORDER_CASES_PARAMS = parameterize_cases( |
| 1216 |
'test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict, expected_t1_plus_delta', DELTA_IGNORE_ORDER_CASES) |
| 1217 |
|
| 1218 |
|
| 1219 |
class TestIgnoreOrderDelta: |
| 1220 |
|
| 1221 |
@pytest.mark.parametrize(**DELTA_IGNORE_ORDER_CASES_PARAMS) |
| 1222 |
def test_ignore_order_delta_cases( |
| 1223 |
self, test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict, expected_t1_plus_delta, request): |
| 1224 |
# test_name = request.node.callspec.id |
| 1225 |
diff = DeepDiff(t1, t2, **deepdiff_kwargs) |
| 1226 |
delta_dict = diff._to_delta_dict(**to_delta_kwargs) |
| 1227 |
assert expected_delta_dict == delta_dict, f"test_ignore_order_delta_cases {test_name} failed" |
| 1228 |
delta = Delta(diff, bidirectional=False, raise_errors=True) |
| 1229 |
expected_t1_plus_delta = t2 if expected_t1_plus_delta == 't2' else expected_t1_plus_delta |
| 1230 |
t1_plus_delta = t1 + delta |
| 1231 |
assert t1 + delta == t1_plus_delta, f"test_ignore_order_delta_cases {test_name} 'asserting that delta is not mutated once it is applied' failed" |
| 1232 |
# assert not DeepDiff(t1_plus_delta, expected_t1_plus_delta, ignore_order=True), f"test_ignore_order_delta_cases {test_name} failed: diff = {DeepDiff(t1_plus_delta, expected_t1_plus_delta, ignore_order=True)}" |
| 1233 |
|
| 1234 |
|
| 1235 |
DELTA_NUMPY_TEST_CASES = { |
| 1236 |
'delta_case15_similar_to_delta_numpy': { |
| 1237 |
't1': [1, 2, 3], |
| 1238 |
't2': [1, 2, 5], |
| 1239 |
'deepdiff_kwargs': {}, |
| 1240 |
'to_delta_kwargs': {}, |
| 1241 |
'expected_delta_dict': {'values_changed': {'root[2]': {'new_value': 5}}}, |
| 1242 |
'expected_result': 't2' |
| 1243 |
}, |
| 1244 |
'delta_numpy1_operator_override': { |
| 1245 |
't1': np.array([1, 2, 3], np.int8), |
| 1246 |
't2': np.array([1, 2, 5], np.int8), |
| 1247 |
'deepdiff_kwargs': {}, |
| 1248 |
'to_delta_kwargs': {}, |
| 1249 |
'expected_delta_dict': {'values_changed': {'root[2]': {'new_value': 5}}, '_numpy_paths': {'root': 'int8'}}, |
| 1250 |
'expected_result': DeltaNumpyOperatorOverrideError |
| 1251 |
}, |
| 1252 |
'delta_numpy2': { |
| 1253 |
't1': np.array([1, 2, 3], np.int8), |
| 1254 |
't2': np.array([1, 2, 5], np.int8), |
| 1255 |
'deepdiff_kwargs': {}, |
| 1256 |
'to_delta_kwargs': {}, |
| 1257 |
'expected_delta_dict': {'values_changed': {'root[2]': {'new_value': 5}}, '_numpy_paths': {'root': 'int8'}}, |
| 1258 |
'expected_result': 't2' |
| 1259 |
}, |
| 1260 |
'delta_numpy3_type_change_but_no_value_change': { |
| 1261 |
't1': np.array([1, 2, 3], np.int8), |
| 1262 |
't2': np.array([1, 2, 3], np.int16), |
| 1263 |
'deepdiff_kwargs': {}, |
| 1264 |
'to_delta_kwargs': {}, |
| 1265 |
'expected_delta_dict': {'type_changes': {'root': {'old_type': np.int8, 'new_type': np.int16}}}, |
| 1266 |
'expected_result': 't2' |
| 1267 |
}, |
| 1268 |
'delta_numpy4_type_change_plus_value_change': { |
| 1269 |
't1': np.array([1, 2, 3], np.int8), |
| 1270 |
't2': np.array([1, 2, 5], np.int16), |
| 1271 |
'deepdiff_kwargs': {}, |
| 1272 |
'to_delta_kwargs': {}, |
| 1273 |
'expected_delta_dict': None, # Not easy to compare since it throws: |
| 1274 |
# ValueError: The truth value of an array with more than one element is ambiguous. |
| 1275 |
# And we don't want to use DeepDiff for testing the equality inside deepdiff tests themselves! |
| 1276 |
'expected_result': 't2' |
| 1277 |
}, |
| 1278 |
'delta_numpy4_type_change_ignore_numeric_type_changes': { |
| 1279 |
't1': np.array([1, 2, 3], np.int8), |
| 1280 |
't2': np.array([1, 2, 5], np.int16), |
| 1281 |
'deepdiff_kwargs': { |
| 1282 |
'ignore_numeric_type_changes': True |
| 1283 |
}, |
| 1284 |
'to_delta_kwargs': {}, |
| 1285 |
'expected_delta_dict': { |
| 1286 |
'values_changed': { |
| 1287 |
'root[2]': { |
| 1288 |
'new_value': 5 |
| 1289 |
} |
| 1290 |
}, |
| 1291 |
'_numpy_paths': { |
| 1292 |
'root': 'int16' |
| 1293 |
} |
| 1294 |
}, |
| 1295 |
'expected_result': 't2' |
| 1296 |
}, |
| 1297 |
'delta_numpy5_multi_dimensional': { |
| 1298 |
't1': np.array([[1, 2, 3], [4, 2, 2]], np.int8), |
| 1299 |
't2': np.array([[1, 2, 5], [4, 1, 2]], np.int8), |
| 1300 |
'deepdiff_kwargs': {}, |
| 1301 |
'to_delta_kwargs': {}, |
| 1302 |
'expected_delta_dict': { |
| 1303 |
'values_changed': { |
| 1304 |
'root[0][2]': { |
| 1305 |
'new_value': 5 |
| 1306 |
}, |
| 1307 |
'root[1][1]': { |
| 1308 |
'new_value': 1 |
| 1309 |
} |
| 1310 |
}, |
| 1311 |
'_numpy_paths': { |
| 1312 |
'root': 'int8' |
| 1313 |
} |
| 1314 |
}, |
| 1315 |
'expected_result': 't2' |
| 1316 |
}, |
| 1317 |
'delta_numpy6_multi_dimensional_ignore_order': { |
| 1318 |
't1': np.array([[1, 2, 3, 4], [4, 2, 2, 1]], np.int8), |
| 1319 |
't2': np.array([[4, 1, 1, 1], [1, 3, 2, 4]], np.int8), |
| 1320 |
'deepdiff_kwargs': { |
| 1321 |
'ignore_order': True, |
| 1322 |
'report_repetition': True |
| 1323 |
}, |
| 1324 |
'to_delta_kwargs': {}, |
| 1325 |
'expected_delta_dict': { |
| 1326 |
'iterable_items_added_at_indexes': { |
| 1327 |
'root[1]': { |
| 1328 |
1: 1, |
| 1329 |
2: 1, |
| 1330 |
3: 1 |
| 1331 |
} |
| 1332 |
}, |
| 1333 |
'iterable_items_removed_at_indexes': { |
| 1334 |
'root[1]': { |
| 1335 |
1: 2, |
| 1336 |
2: 2 |
| 1337 |
} |
| 1338 |
}, |
| 1339 |
'_numpy_paths': { |
| 1340 |
'root': 'int8' |
| 1341 |
} |
| 1342 |
}, |
| 1343 |
'expected_result': 't2_via_deepdiff' |
| 1344 |
}, |
| 1345 |
'delta_numpy7_arrays_of_different_sizes': { |
| 1346 |
't1': np.array([1, 2, 3, 4]), |
| 1347 |
't2': np.array([5, 6, 7, 8, 9, 10]), |
| 1348 |
'deepdiff_kwargs': {}, |
| 1349 |
'to_delta_kwargs': {}, |
| 1350 |
'expected_delta_dict': { |
| 1351 |
'values_changed': { |
| 1352 |
'root[0]': { |
| 1353 |
'new_value': 5 |
| 1354 |
}, |
| 1355 |
'root[1]': { |
| 1356 |
'new_value': 6 |
| 1357 |
}, |
| 1358 |
'root[2]': { |
| 1359 |
'new_value': 7 |
| 1360 |
}, |
| 1361 |
'root[3]': { |
| 1362 |
'new_value': 8 |
| 1363 |
} |
| 1364 |
}, |
| 1365 |
'iterable_item_added': { |
| 1366 |
'root[4]': 9, |
| 1367 |
'root[5]': 10 |
| 1368 |
}, |
| 1369 |
'_numpy_paths': { |
| 1370 |
'root': np.where((sys.maxsize > 2**32), 'int64', 'int32') |
| 1371 |
} |
| 1372 |
}, |
| 1373 |
'expected_result': 't2' |
| 1374 |
}, |
| 1375 |
'delta_with_null_as_key': { |
| 1376 |
't1': { None: [1, 2], 'foo': [1, 2] }, |
| 1377 |
't2': { None: [1], 'foo': [1] }, |
| 1378 |
'deepdiff_kwargs': {}, |
| 1379 |
'to_delta_kwargs': {}, |
| 1380 |
'expected_delta_dict': { |
| 1381 |
}, |
| 1382 |
'expected_result': 't2' |
| 1383 |
}, |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
DELTA_NUMPY_TEST_PARAMS = parameterize_cases( |
| 1388 |
'test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict, expected_result', DELTA_NUMPY_TEST_CASES) |
| 1389 |
|
| 1390 |
|
| 1391 |
class TestNumpyDelta: |
| 1392 |
|
| 1393 |
@pytest.mark.parametrize(**DELTA_NUMPY_TEST_PARAMS) |
| 1394 |
def test_numpy_delta_cases(self, test_name, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_delta_dict, expected_result): |
| 1395 |
diff = DeepDiff(t1, t2, **deepdiff_kwargs) |
| 1396 |
delta_dict = diff._to_delta_dict(**to_delta_kwargs) |
| 1397 |
if expected_delta_dict: |
| 1398 |
assert expected_delta_dict == delta_dict, f"test_numpy_delta_cases {test_name} failed." |
| 1399 |
delta = Delta(diff, bidirectional=False, raise_errors=True) |
| 1400 |
if expected_result == 't2': |
| 1401 |
result = delta + t1 |
| 1402 |
assert np.array_equal(result, t2), f"test_numpy_delta_cases {test_name} failed." |
| 1403 |
elif expected_result == 't2_via_deepdiff': |
| 1404 |
result = delta + t1 |
| 1405 |
diff = DeepDiff(result, t2, ignore_order=True, report_repetition=True) |
| 1406 |
assert not diff, f"test_numpy_delta_cases {test_name} failed." |
| 1407 |
elif expected_result is DeltaNumpyOperatorOverrideError: |
| 1408 |
with pytest.raises(DeltaNumpyOperatorOverrideError): |
| 1409 |
t1 + delta |
| 1410 |
else: |
| 1411 |
result = delta + t1 |
| 1412 |
assert np.array_equal(result, expected_result), f"test_numpy_delta_cases {test_name} failed." |
| 1413 |
|
| 1414 |
def test_invalid_numpy_type(self): |
| 1415 |
|
| 1416 |
t1 = np.array([1, 2, 3], np.int8) |
| 1417 |
delta_dict = {'iterable_item_added': {'root[2]': 5}, '_numpy_paths': {'root': 'int11'}} |
| 1418 |
|
| 1419 |
with pytest.raises(DeltaError) as excinfo: |
| 1420 |
Delta(delta_dict, raise_errors=True) + t1 |
| 1421 |
|
| 1422 |
expected_msg = NOT_VALID_NUMPY_TYPE.format("'int11'") |
| 1423 |
assert expected_msg == str(excinfo.value) |
| 1424 |
|
| 1425 |
|
| 1426 |
class TestDeltaOther: |
| 1427 |
|
| 1428 |
def test_list_ignore_order_various_deltas1(self): |
| 1429 |
t1 = [5, 1, 3, 1, 4, 4, 6] |
| 1430 |
t2 = [7, 4, 4, 1, 3, 4, 8] |
| 1431 |
|
| 1432 |
delta_dict1 = {'iterable_items_added_at_indexes': {'root': {0: 7, 6: 8, 1: 4, 2: 4, 5: 4, 3: 1}}, 'iterable_items_removed_at_indexes': {'root': {0: 5, 6: 6}}} |
| 1433 |
delta_dict2 = {'iterable_items_added_at_indexes': {'root': {1: 4, 2: 4, 5: 4, 3: 1}}, 'values_changed': {'root[6]': {'new_value': 7}, 'root[0]': {'new_value': 8}}} |
| 1434 |
delta1 = Delta(delta_dict1) |
| 1435 |
t1_plus_delta1 = t1 + delta1 |
| 1436 |
assert t1_plus_delta1 == t2 |
| 1437 |
delta2 = Delta(delta_dict2) |
| 1438 |
t1_plus_delta2 = t1 + delta2 |
| 1439 |
assert t1_plus_delta2 == [8, 4, 4, 1, 3, 4, 7] |
| 1440 |
|
| 1441 |
def test_list_ignore_order_various_deltas2(self): |
| 1442 |
t1 = (5, 1, 3, 1, 4, 4, 6) |
| 1443 |
t2 = (7, 4, 4, 1, 3, 4, 8, 1) |
| 1444 |
|
| 1445 |
delta_dict1 = {'iterable_items_added_at_indexes': {'root': {0: 7, 6: 8, 1: 4, 2: 4, 5: 4}}, 'iterable_items_removed_at_indexes': {'root': {6: 6, 0: 5}}} |
| 1446 |
delta_dict2 = {'iterable_items_added_at_indexes': {'root': {1: 4, 2: 4, 5: 4}}, 'values_changed': {'root[6]': {'new_value': 7}, 'root[0]': {'new_value': 8}}} |
| 1447 |
delta1 = Delta(delta_dict1) |
| 1448 |
t1_plus_delta1 = t1 + delta1 |
| 1449 |
assert t1_plus_delta1 == t2 |
| 1450 |
delta2 = Delta(delta_dict2) |
| 1451 |
t1_plus_delta2 = t1 + delta2 |
| 1452 |
assert t1_plus_delta2 == (8, 4, 4, 1, 3, 4, 1, 7) |
| 1453 |
|
| 1454 |
flat_result1 = delta1.to_flat_rows() |
| 1455 |
flat_expected1 = [ |
| 1456 |
{'path': [0], 'value': 7, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1457 |
{'path': [6], 'value': 8, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1458 |
{'path': [1], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1459 |
{'path': [2], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1460 |
{'path': [5], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1461 |
{'path': [6], 'value': 6, 'action': 'unordered_iterable_item_removed', 'type': int}, |
| 1462 |
{'path': [0], 'value': 5, 'action': 'unordered_iterable_item_removed', 'type': int}, |
| 1463 |
] |
| 1464 |
flat_expected1 = [FlatDeltaRow(**i) for i in flat_expected1] |
| 1465 |
assert flat_expected1 == flat_result1 |
| 1466 |
|
| 1467 |
delta1_again = Delta(flat_rows_list=flat_expected1) |
| 1468 |
assert t1_plus_delta1 == t1 + delta1_again |
| 1469 |
assert delta1.diff == delta1_again.diff |
| 1470 |
|
| 1471 |
flat_result2 = delta2.to_flat_rows() |
| 1472 |
flat_expected2 = [ |
| 1473 |
{'path': [1], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1474 |
{'path': [2], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1475 |
{'path': [5], 'value': 4, 'action': 'unordered_iterable_item_added', 'type': int}, |
| 1476 |
{'path': [6], 'action': 'values_changed', 'value': 7, 'type': int}, |
| 1477 |
{'path': [0], 'action': 'values_changed', 'value': 8, 'type': int}, |
| 1478 |
] |
| 1479 |
flat_expected2 = [FlatDeltaRow(**i) for i in flat_expected2] |
| 1480 |
assert flat_expected2 == flat_result2 |
| 1481 |
|
| 1482 |
delta2_again = Delta(flat_rows_list=flat_expected2) |
| 1483 |
assert delta2.diff == delta2_again.diff |
| 1484 |
|
| 1485 |
def test_delta_view_and_to_delta_dict_are_equal_when_parameteres_passed(self): |
| 1486 |
""" |
| 1487 |
This is a test that passes parameters in a dictionary instead of kwargs. |
| 1488 |
Note that when parameters are passed as a dictionary, all of them even the ones that |
| 1489 |
have default values need to be passed. |
| 1490 |
""" |
| 1491 |
t1 = [4, 2, 2, 1] |
| 1492 |
t2 = [4, 1, 1, 1] |
| 1493 |
_parameters = { |
| 1494 |
'ignore_order': True, |
| 1495 |
'ignore_numeric_type_changes': False, |
| 1496 |
'ignore_string_type_changes': False, |
| 1497 |
'ignore_type_in_groups': [], |
| 1498 |
'report_repetition': True, |
| 1499 |
'use_enum_value': False, |
| 1500 |
'exclude_paths': None, |
| 1501 |
'include_paths': None, |
| 1502 |
'exclude_regex_paths': None, |
| 1503 |
'exclude_types': None, |
| 1504 |
'exclude_types_tuple': None, |
| 1505 |
'ignore_type_subclasses': False, |
| 1506 |
'ignore_string_case': False, |
| 1507 |
'include_obj_callback': None, |
| 1508 |
'include_obj_callback_strict': None, |
| 1509 |
'exclude_obj_callback': None, |
| 1510 |
'exclude_obj_callback_strict': None, |
| 1511 |
'ignore_uuid_types': False, |
| 1512 |
'ignore_private_variables': True, |
| 1513 |
'ignore_nan_inequality': False, |
| 1514 |
'hasher': None, |
| 1515 |
'significant_digits': None, |
| 1516 |
'number_format_notation': 'f', |
| 1517 |
'verbose_level': 1, |
| 1518 |
'view': DELTA_VIEW, |
| 1519 |
'max_passes': 10000000, |
| 1520 |
'max_diffs': None, |
| 1521 |
'number_to_string': number_to_string, |
| 1522 |
'cache_tuning_sample_size': 500, |
| 1523 |
'cache_size': 500, |
| 1524 |
'cutoff_intersection_for_pairs': 0.6, |
| 1525 |
'group_by': None, |
| 1526 |
'ignore_order_func': lambda *args, **kwargs: True, |
| 1527 |
'custom_operators': [], |
| 1528 |
'encodings': None, |
| 1529 |
'ignore_encoding_errors': False, |
| 1530 |
'iterable_compare_func': None, |
| 1531 |
'default_timezone': datetime.timezone.utc, |
| 1532 |
} |
| 1533 |
|
| 1534 |
expected = {'iterable_items_added_at_indexes': {'root': {1: 1, 2: 1, 3: 1}}, 'iterable_items_removed_at_indexes': {'root': {1: 2, 2: 2}}} |
| 1535 |
diff1 = DeepDiff(t1, t2, _parameters=_parameters) |
| 1536 |
assert expected == diff1 |
| 1537 |
|
| 1538 |
_parameters['view'] = TEXT_VIEW |
| 1539 |
diff2 = DeepDiff(t1, t2, _parameters=_parameters) |
| 1540 |
assert expected == diff2._to_delta_dict() |
| 1541 |
|
| 1542 |
def test_verify_symmetry_and_get_elem_and_compare_to_old_value(self): |
| 1543 |
""" |
| 1544 |
Test a specific case where path was a list of elements (in the form of tuples) |
| 1545 |
and the item could not be found. |
| 1546 |
""" |
| 1547 |
delta = Delta({}, bidirectional=True, raise_errors=True, log_errors=False) |
| 1548 |
with pytest.raises(DeltaError) as excinfo: |
| 1549 |
delta._get_elem_and_compare_to_old_value( |
| 1550 |
obj={}, |
| 1551 |
path_for_err_reporting=(('root', GETATTR),), |
| 1552 |
expected_old_value='Expected Value', |
| 1553 |
action=GET, |
| 1554 |
elem='key') |
| 1555 |
assert VERIFICATION_MSG.format('root', 'Expected Value', 'not found', "'key'") == str(excinfo.value) |
| 1556 |
|
| 1557 |
def test_apply_delta_to_incompatible_object1(self): |
| 1558 |
t1 = {1: {2: [4, 5]}} |
| 1559 |
t2 = {1: {2: [4]}, 0: 'new'} |
| 1560 |
|
| 1561 |
diff = DeepDiff(t1, t2) |
| 1562 |
delta = Delta(diff, raise_errors=True) |
| 1563 |
|
| 1564 |
t3 = [] |
| 1565 |
|
| 1566 |
with pytest.raises(DeltaError) as excinfo: |
| 1567 |
delta + t3 |
| 1568 |
assert "Unable to get the item at root[1][2][1]: list index out of range" == str(excinfo.value) |
| 1569 |
assert [] == t3 |
| 1570 |
|
| 1571 |
def test_apply_delta_to_incompatible_object3_errors_can_be_muted(self): |
| 1572 |
t1 = {1: {2: [4]}} |
| 1573 |
t2 = {1: {2: [4, 6]}} |
| 1574 |
t3 = [] |
| 1575 |
|
| 1576 |
diff = DeepDiff(t1, t2) |
| 1577 |
|
| 1578 |
delta2 = Delta(diff, raise_errors=False) |
| 1579 |
t4 = delta2 + t3 |
| 1580 |
assert [] == t4 |
| 1581 |
|
| 1582 |
def test_apply_delta_to_incompatible_object4_errors_can_be_muted(self): |
| 1583 |
t1 = {1: {2: [4, 5]}} |
| 1584 |
t2 = {1: {2: [4]}, 0: 'new'} |
| 1585 |
t3 = [] |
| 1586 |
|
| 1587 |
diff = DeepDiff(t1, t2) |
| 1588 |
|
| 1589 |
# The original delta was based on a diff between 2 dictionaries. |
| 1590 |
# if we turn raise_errors=False, we can try to see what portions of the delta |
| 1591 |
delta2 = Delta(diff, raise_errors=False) |
| 1592 |
t4 = delta2 + t3 |
| 1593 |
assert ['new'] == t4 |
| 1594 |
|
| 1595 |
def test_apply_delta_to_incompatible_object5_no_errors_detected(self): |
| 1596 |
t1 = {3: {2: [4]}} |
| 1597 |
t2 = {3: {2: [4]}, 0: 'new', 1: 'newer'} |
| 1598 |
diff = DeepDiff(t1, t2) |
| 1599 |
|
| 1600 |
t3 = [] |
| 1601 |
# The original delta was based on a diff between 2 dictionaries. |
| 1602 |
# if we turn raise_errors=True, and there are no errors, a delta can be applied fully to another object! |
| 1603 |
delta2 = Delta(diff, raise_errors=True) |
| 1604 |
t4 = delta2 + t3 |
| 1605 |
assert ['new', 'newer'] == t4 |
| 1606 |
|
| 1607 |
def test_apply_delta_to_incompatible_object6_value_change(self): |
| 1608 |
t1 = {1: {2: [4]}} |
| 1609 |
t2 = {1: {2: [5]}} |
| 1610 |
t3 = [] |
| 1611 |
|
| 1612 |
diff = DeepDiff(t1, t2) |
| 1613 |
|
| 1614 |
delta2 = Delta(diff, raise_errors=False) |
| 1615 |
t4 = delta2 + t3 |
| 1616 |
assert [] == t4 |
| 1617 |
|
| 1618 |
flat_result2 = delta2.to_flat_rows() |
| 1619 |
flat_expected2 = [{'path': [1, 2, 0], 'action': 'values_changed', 'value': 5, 'type': int}] |
| 1620 |
flat_expected2 = [FlatDeltaRow(**i) for i in flat_expected2] |
| 1621 |
|
| 1622 |
assert flat_expected2 == flat_result2 |
| 1623 |
|
| 1624 |
delta2_again = Delta(flat_rows_list=flat_expected2) |
| 1625 |
assert delta2.diff == delta2_again.diff |
| 1626 |
|
| 1627 |
delta3 = Delta(diff, raise_errors=False, bidirectional=True) |
| 1628 |
flat_result3 = delta3.to_flat_rows() |
| 1629 |
flat_expected3 = [{'path': [1, 2, 0], 'action': 'values_changed', 'value': 5, 'old_value': 4, 'type': int, 'old_type': int}] |
| 1630 |
flat_expected3 = [FlatDeltaRow(**i) for i in flat_expected3] |
| 1631 |
|
| 1632 |
assert flat_expected3 == flat_result3 |
| 1633 |
|
| 1634 |
delta3_again = Delta(flat_rows_list=flat_expected3) |
| 1635 |
assert delta3.diff == delta3_again.diff |
| 1636 |
|
| 1637 |
def test_apply_delta_to_incompatible_object7_type_change(self): |
| 1638 |
t1 = ['1'] |
| 1639 |
t2 = [1] |
| 1640 |
t3 = ['a'] |
| 1641 |
|
| 1642 |
diff = DeepDiff(t1, t2) |
| 1643 |
|
| 1644 |
delta2 = Delta(diff, raise_errors=False) |
| 1645 |
t4 = delta2 + t3 |
| 1646 |
assert ['a'] == t4 |
| 1647 |
|
| 1648 |
@mock.patch('deepdiff.delta.logger.error') |
| 1649 |
def test_apply_delta_to_incompatible_object7_verify_symmetry(self, mock_logger): |
| 1650 |
t1 = [1] |
| 1651 |
t2 = [2] |
| 1652 |
t3 = [3] |
| 1653 |
|
| 1654 |
diff = DeepDiff(t1, t2) |
| 1655 |
|
| 1656 |
delta2 = Delta(diff, raise_errors=False, bidirectional=True) |
| 1657 |
t4 = delta2 + t3 |
| 1658 |
|
| 1659 |
assert [2] == t4 |
| 1660 |
expected_msg = VERIFICATION_MSG.format('root[0]', 1, 3, VERIFY_BIDIRECTIONAL_MSG) |
| 1661 |
mock_logger.assert_called_once_with(expected_msg) |
| 1662 |
|
| 1663 |
@mock.patch('deepdiff.delta.logger.error') |
| 1664 |
def test_apply_delta_to_incompatible_object8_verify_symmetry_ignore_order(self, mock_logger): |
| 1665 |
t1 = [1, 2, 'B', 3] |
| 1666 |
t2 = [1, 2, 3, 5] |
| 1667 |
t3 = [] |
| 1668 |
|
| 1669 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 1670 |
|
| 1671 |
delta2 = Delta(diff, raise_errors=False, bidirectional=True) |
| 1672 |
t4 = delta2 + t3 |
| 1673 |
|
| 1674 |
assert [5] == t4 |
| 1675 |
expected_msg = INDEXES_NOT_FOUND_WHEN_IGNORE_ORDER.format({3: 5}) |
| 1676 |
mock_logger.assert_called_once_with(expected_msg) |
| 1677 |
|
| 1678 |
@mock.patch('deepdiff.delta.logger.error') |
| 1679 |
def test_apply_delta_to_incompatible_object9_ignore_order_and_verify_symmetry(self, mock_logger): |
| 1680 |
t1 = [1, 2, 'B'] |
| 1681 |
t2 = [1, 2] |
| 1682 |
t3 = [1, 2, 'C'] |
| 1683 |
|
| 1684 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 1685 |
|
| 1686 |
delta = Delta(diff, raise_errors=False, bidirectional=True) |
| 1687 |
t4 = delta + t3 |
| 1688 |
|
| 1689 |
assert [1, 2, 'C'] == t4 |
| 1690 |
expected_msg = FAIL_TO_REMOVE_ITEM_IGNORE_ORDER_MSG.format(2, 'root', 'B', 'C') |
| 1691 |
mock_logger.assert_called_once_with(expected_msg) |
| 1692 |
|
| 1693 |
@mock.patch('deepdiff.delta.logger.error') |
| 1694 |
def test_apply_delta_to_incompatible_object10_ignore_order(self, mock_logger): |
| 1695 |
t1 = [1, 2, 'B'] |
| 1696 |
t2 = [1, 2] |
| 1697 |
t3 = [1, 2, 'C'] |
| 1698 |
|
| 1699 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 1700 |
|
| 1701 |
# when bidirectional=False, we still won't remove the item that is different |
| 1702 |
# than what we expect specifically when ignore_order=True when generating the diff. |
| 1703 |
# The reason is that when ignore_order=True, we can' rely too much on the index |
| 1704 |
# of the item alone to delete it. We need to make sure we are deleting the correct value. |
| 1705 |
# The expected behavior is exactly the same as when bidirectional=True |
| 1706 |
# specifically for when ignore_order=True AND an item is removed. |
| 1707 |
delta = Delta(diff, raise_errors=False, bidirectional=False) |
| 1708 |
t4 = delta + t3 |
| 1709 |
|
| 1710 |
assert [1, 2, 'C'] == t4 |
| 1711 |
expected_msg = FAIL_TO_REMOVE_ITEM_IGNORE_ORDER_MSG.format(2, 'root', 'B', 'C') |
| 1712 |
mock_logger.assert_called_once_with(expected_msg) |
| 1713 |
|
| 1714 |
@mock.patch('deepdiff.delta.logger.error') |
| 1715 |
def test_apply_delta_to_incompatible_object11_ignore_order(self, mock_logger): |
| 1716 |
t1 = [[1, 2, 'B']] |
| 1717 |
t2 = [[1, 2]] |
| 1718 |
t3 = {} |
| 1719 |
|
| 1720 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 1721 |
delta = Delta(diff, raise_errors=False, bidirectional=False) |
| 1722 |
t4 = delta + t3 |
| 1723 |
|
| 1724 |
assert {} == t4 |
| 1725 |
expected_msg = UNABLE_TO_GET_PATH_MSG.format('root[0][0]') |
| 1726 |
mock_logger.assert_called_once_with(expected_msg) |
| 1727 |
|
| 1728 |
def test_delta_to_dict(self): |
| 1729 |
t1 = [1, 2, 'B'] |
| 1730 |
t2 = [1, 2] |
| 1731 |
diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 1732 |
delta = Delta(diff, raise_errors=False, bidirectional=False) |
| 1733 |
|
| 1734 |
result = delta.to_dict() |
| 1735 |
expected = {'iterable_items_removed_at_indexes': {'root': {2: 'B'}}} |
| 1736 |
assert expected == result |
| 1737 |
|
| 1738 |
flat_result = delta.to_flat_rows() |
| 1739 |
flat_expected = [{'action': 'unordered_iterable_item_removed', 'path': [2], 'value': 'B', 'type': str}] |
| 1740 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 1741 |
|
| 1742 |
assert flat_expected == flat_result |
| 1743 |
|
| 1744 |
delta_again = Delta(flat_rows_list=flat_expected) |
| 1745 |
assert delta.diff == delta_again.diff |
| 1746 |
|
| 1747 |
def test_class_type_change(self): |
| 1748 |
t1 = CustomClass |
| 1749 |
t2 = CustomClass2 |
| 1750 |
diff = DeepDiff(t1, t2, view=DELTA_VIEW) |
| 1751 |
expected = {'type_changes': {'root': {'new_type': CustomClass2, |
| 1752 |
'old_type': CustomClass}}} |
| 1753 |
|
| 1754 |
assert expected == diff |
| 1755 |
|
| 1756 |
def test_numpy_type_invalid(self): |
| 1757 |
t1 = np.array([[1, 2, 3], [4, 2, 2]], np.int8) |
| 1758 |
diff = { |
| 1759 |
'iterable_item_added': {'root[2]': [7, 8, 9]}, |
| 1760 |
'values_changed': { |
| 1761 |
'root[0][2]': { |
| 1762 |
'new_value': 5 |
| 1763 |
}, |
| 1764 |
'root[1][1]': { |
| 1765 |
'new_value': 1 |
| 1766 |
} |
| 1767 |
}, |
| 1768 |
'_numpy_paths': { |
| 1769 |
'root': 'int88' |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
delta = Delta(diff, raise_errors=True) |
| 1774 |
with pytest.raises(DeltaError) as excinfo: |
| 1775 |
delta + t1 |
| 1776 |
assert "'int88' is not a valid numpy type." == str(excinfo.value) |
| 1777 |
|
| 1778 |
def test_ignore_order_but_not_report_repetition(self): |
| 1779 |
t1 = [1, 2, 'B', 3] |
| 1780 |
t2 = [1, 2, 3, 5] |
| 1781 |
|
| 1782 |
with pytest.raises(ValueError) as excinfo: |
| 1783 |
Delta(DeepDiff(t1, t2, ignore_order=True)) |
| 1784 |
|
| 1785 |
assert DELTA_IGNORE_ORDER_NEEDS_REPETITION_REPORT == str(excinfo.value) |
| 1786 |
|
| 1787 |
def test_none_in_delta_object(self): |
| 1788 |
t1 = {"a": None} |
| 1789 |
t2 = {"a": 1} |
| 1790 |
|
| 1791 |
dump = Delta(DeepDiff(t1, t2)).dumps() |
| 1792 |
delta = Delta(dump) |
| 1793 |
assert t2 == delta + t1 |
| 1794 |
|
| 1795 |
flat_result = delta.to_flat_rows() |
| 1796 |
flat_expected = [{'path': ['a'], 'action': 'type_changes', 'value': 1, 'type': int, 'old_type': type(None)}] |
| 1797 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 1798 |
|
| 1799 |
assert flat_expected == flat_result |
| 1800 |
|
| 1801 |
delta_again = Delta(flat_rows_list=flat_expected) |
| 1802 |
assert delta.diff == delta_again.diff |
| 1803 |
|
| 1804 |
with pytest.raises(ValueError) as exc_info: |
| 1805 |
delta.to_flat_rows(report_type_changes=False) |
| 1806 |
assert str(exc_info.value).startswith("When converting to flat dictionaries, if report_type_changes=False and there are type") |
| 1807 |
delta2 = Delta(dump, always_include_values=True) |
| 1808 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 1809 |
flat_expected2 = [{'path': ['a'], 'action': 'values_changed', 'value': 1}] |
| 1810 |
flat_expected2 = [FlatDeltaRow(**i) for i in flat_expected2] |
| 1811 |
|
| 1812 |
assert flat_expected2 == flat_result2 |
| 1813 |
|
| 1814 |
def test_delta_set_in_objects(self): |
| 1815 |
t1 = [[1, SetOrdered(['A', 'B'])], {1}] |
| 1816 |
t2 = [[2, SetOrdered([10, 'C', 'B'])], {1}] |
| 1817 |
delta = Delta(DeepDiff(t1, t2)) |
| 1818 |
flat_result = delta.to_flat_rows() |
| 1819 |
flat_expected = [ |
| 1820 |
{'path': [0, 1], 'value': 10, 'action': 'set_item_added', 'type': int}, |
| 1821 |
{'path': [0, 0], 'action': 'values_changed', 'value': 2, 'type': int}, |
| 1822 |
{'path': [0, 1], 'value': 'A', 'action': 'set_item_removed', 'type': str}, |
| 1823 |
{'path': [0, 1], 'value': 'C', 'action': 'set_item_added', 'type': str}, |
| 1824 |
] |
| 1825 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 1826 |
|
| 1827 |
# Sorting because otherwise the order is not deterministic for sets, |
| 1828 |
# even though we are using SetOrdered here. It still is converted to set at some point and loses its order. |
| 1829 |
flat_result.sort(key=lambda x: str(x.value)) |
| 1830 |
assert flat_expected == flat_result |
| 1831 |
|
| 1832 |
delta_again = Delta(flat_rows_list=flat_expected) |
| 1833 |
assert delta.diff == delta_again.diff |
| 1834 |
|
| 1835 |
def test_delta_array_of_bytes(self): |
| 1836 |
t1 = [] |
| 1837 |
t2 = [b"hello"] |
| 1838 |
diff = DeepDiff(t1, t2) |
| 1839 |
expected_diff = {'iterable_item_added': {'root[0]': b'hello'}} |
| 1840 |
assert expected_diff == diff |
| 1841 |
delta = Delta(diff) |
| 1842 |
flat_result = delta.to_flat_rows() |
| 1843 |
flat_expected = [FlatDeltaRow(path=[0], action=FlatDataAction.iterable_item_added, value=b'hello', type=bytes)] |
| 1844 |
assert flat_expected == flat_result |
| 1845 |
|
| 1846 |
delta_again = Delta(flat_rows_list=flat_expected) |
| 1847 |
assert delta.diff == delta_again.diff |
| 1848 |
assert t1 + delta_again == t2 |
| 1849 |
|
| 1850 |
def test_delta_with_json_serializer(self): |
| 1851 |
t1 = {"a": 1} |
| 1852 |
t2 = {"a": 2} |
| 1853 |
|
| 1854 |
diff = DeepDiff(t1, t2) |
| 1855 |
delta = Delta(diff, serializer=json.dumps) |
| 1856 |
dump = delta.dumps() |
| 1857 |
delta_reloaded = Delta(dump, deserializer=json.loads) |
| 1858 |
assert t2 == delta_reloaded + t1 |
| 1859 |
|
| 1860 |
the_file = io.StringIO() |
| 1861 |
delta.dump(the_file) |
| 1862 |
the_file.seek(0) |
| 1863 |
|
| 1864 |
delta_reloaded_again = Delta(delta_file=the_file, deserializer=json.loads) |
| 1865 |
assert t2 == delta_reloaded_again + t1 |
| 1866 |
|
| 1867 |
def test_brackets_in_keys(self): |
| 1868 |
""" |
| 1869 |
Delta calculation not correct when bracket in Json key |
| 1870 |
https://github.com/seperman/deepdiff/issues/265 |
| 1871 |
""" |
| 1872 |
t1 = "{ \ |
| 1873 |
\"test\": \"test1\" \ |
| 1874 |
}" |
| 1875 |
|
| 1876 |
t2 = "{ \ |
| 1877 |
\"test\": \"test1\", \ |
| 1878 |
\"test2 [uuu]\": \"test2\" \ |
| 1879 |
}" |
| 1880 |
|
| 1881 |
json1 = json.loads(t1) |
| 1882 |
json2 = json.loads(t2) |
| 1883 |
|
| 1884 |
ddiff = DeepDiff(json1, json2) |
| 1885 |
delta = Delta(ddiff) |
| 1886 |
|
| 1887 |
original_json2 = delta + json1 |
| 1888 |
assert json2 == original_json2 |
| 1889 |
|
| 1890 |
|
| 1891 |
class TestDeltaCompareFunc: |
| 1892 |
|
| 1893 |
@staticmethod |
| 1894 |
def compare_func(x, y, level): |
| 1895 |
if (not isinstance(x, dict) or not isinstance(y, dict)): |
| 1896 |
raise CannotCompare |
| 1897 |
if(level.path() == "root['path2']"): |
| 1898 |
if (x["ID"] == y["ID"]): |
| 1899 |
return True |
| 1900 |
return False |
| 1901 |
|
| 1902 |
if("id" in x and "id" in y): |
| 1903 |
if (x["id"] == y["id"]): |
| 1904 |
return True |
| 1905 |
return False |
| 1906 |
|
| 1907 |
raise CannotCompare |
| 1908 |
|
| 1909 |
def test_compare_func1(self, compare_func_t1, compare_func_t2, compare_func_result1): |
| 1910 |
|
| 1911 |
ddiff = DeepDiff( |
| 1912 |
compare_func_t1, compare_func_t2, |
| 1913 |
iterable_compare_func=self.compare_func, verbose_level=1) |
| 1914 |
assert compare_func_result1 == ddiff |
| 1915 |
delta = Delta(ddiff) |
| 1916 |
recreated_t2 = compare_func_t1 + delta |
| 1917 |
assert compare_func_t2 == recreated_t2 |
| 1918 |
|
| 1919 |
def test_compare_func_with_duplicates_removed(self): |
| 1920 |
t1 = [ |
| 1921 |
{ |
| 1922 |
'id': 1, |
| 1923 |
'val': 1, |
| 1924 |
"nested": [ |
| 1925 |
{"id": 1, "val": 1}, |
| 1926 |
{"id": 2, "val": 2}, |
| 1927 |
] |
| 1928 |
}, |
| 1929 |
{ |
| 1930 |
'id': 2, |
| 1931 |
'val': 2 |
| 1932 |
}, |
| 1933 |
{ |
| 1934 |
'id': 1, |
| 1935 |
'val': 3 |
| 1936 |
}, |
| 1937 |
{ |
| 1938 |
'id': 3, |
| 1939 |
'val': 3 |
| 1940 |
} |
| 1941 |
] |
| 1942 |
t2 = [ |
| 1943 |
{ |
| 1944 |
'id': 3, |
| 1945 |
'val': 3 |
| 1946 |
}, |
| 1947 |
{ |
| 1948 |
'id': 2, |
| 1949 |
'val': 2 |
| 1950 |
}, |
| 1951 |
{ |
| 1952 |
'id': 1, |
| 1953 |
'val': 3, |
| 1954 |
"nested":[ |
| 1955 |
{ |
| 1956 |
"id": 2, |
| 1957 |
"val": 3 |
| 1958 |
}, |
| 1959 |
] |
| 1960 |
} |
| 1961 |
] |
| 1962 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 1963 |
expected = { |
| 1964 |
"iterable_item_removed": { |
| 1965 |
"root[2]": { |
| 1966 |
"id": 1, |
| 1967 |
"val": 3 |
| 1968 |
}, |
| 1969 |
|
| 1970 |
"root[2]['nested'][0]": { |
| 1971 |
"id": 1, |
| 1972 |
"val": 1 |
| 1973 |
} |
| 1974 |
}, |
| 1975 |
"iterable_item_moved": { |
| 1976 |
"root[0]": { |
| 1977 |
"new_path": "root[2]", |
| 1978 |
"value": { |
| 1979 |
"id": 1, |
| 1980 |
"val": 3, |
| 1981 |
"nested": [{"id": 2, "val": 3}, ] |
| 1982 |
}, |
| 1983 |
}, |
| 1984 |
"root[0]['nested'][1]": { |
| 1985 |
"new_path": "root[2]['nested'][0]", |
| 1986 |
"value": { |
| 1987 |
"id": 2, |
| 1988 |
"val": 3 |
| 1989 |
} |
| 1990 |
}, |
| 1991 |
"root[3]": { |
| 1992 |
"new_path": "root[0]", |
| 1993 |
"value": { |
| 1994 |
"id": 3, |
| 1995 |
"val": 3 |
| 1996 |
} |
| 1997 |
} |
| 1998 |
}, |
| 1999 |
'values_changed': { |
| 2000 |
"root[2]['nested'][0]['val']": { |
| 2001 |
'new_path': "root[0]['nested'][1]['val']", |
| 2002 |
'new_value': 3, |
| 2003 |
'old_value': 2 |
| 2004 |
}, |
| 2005 |
"root[2]['val']": { |
| 2006 |
'new_value': 3, |
| 2007 |
'old_value': 1, |
| 2008 |
'new_path': "root[0]['val']" |
| 2009 |
} |
| 2010 |
}, |
| 2011 |
} |
| 2012 |
|
| 2013 |
assert expected == ddiff |
| 2014 |
delta = Delta(ddiff) |
| 2015 |
recreated_t2 = t1 + delta |
| 2016 |
assert t2 == recreated_t2 |
| 2017 |
|
| 2018 |
flat_result = delta.to_flat_rows() |
| 2019 |
flat_expected = [ |
| 2020 |
{'path': [2, 'val'], 'value': 3, 'action': 'values_changed', 'type': int, 'new_path': [0, 'val']}, |
| 2021 |
{'path': [2, 'nested', 0, 'val'], 'value': 3, 'action': 'values_changed', 'type': int, 'new_path': [0, 'nested', 1, 'val']}, |
| 2022 |
{'path': [2, 'nested', 0], 'value': {'id': 1, 'val': 1}, 'action': 'iterable_item_removed', 'type': dict}, |
| 2023 |
{'path': [2], 'value': {'id': 1, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict}, |
| 2024 |
{'path': [0], 'value': {'id': 1, 'val': 3, 'nested': [{'id': 2, 'val': 3}]}, 'action': 'iterable_item_removed', 'type': dict}, |
| 2025 |
{'path': [0, 'nested', 1], 'value': {'id': 2, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict}, |
| 2026 |
{'path': [3], 'value': {'id': 3, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict}, |
| 2027 |
{'path': [0], 'action': 'iterable_item_moved', 'value': {'id': 1, 'val': 3, 'nested': [{'id': 2, 'val': 3}]}, 'new_path': [2], 'type': dict}, |
| 2028 |
{'path': [0, 'nested', 1], 'value': {'id': 2, 'val': 3}, 'action': 'iterable_item_moved', 'type': dict, 'new_path': [2, 'nested', 0]}, |
| 2029 |
{'path': [3], 'action': 'iterable_item_moved', 'value': {'id': 3, 'val': 3}, 'new_path': [0], 'type': dict}, |
| 2030 |
] |
| 2031 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 2032 |
|
| 2033 |
assert flat_expected == flat_result |
| 2034 |
|
| 2035 |
# Delta.DEBUG = True |
| 2036 |
delta_again = Delta(flat_rows_list=flat_expected, iterable_compare_func_was_used=True) |
| 2037 |
expected_delta_dict = { |
| 2038 |
'iterable_item_removed': { |
| 2039 |
'root[2]': { |
| 2040 |
'id': 1, |
| 2041 |
'val': 3 |
| 2042 |
}, |
| 2043 |
'root[0]': { |
| 2044 |
'id': 1, |
| 2045 |
'val': 3, |
| 2046 |
'nested': [{'id': 2, 'val': 3}] |
| 2047 |
}, |
| 2048 |
'root[3]': { |
| 2049 |
'id': 3, |
| 2050 |
'val': 3 |
| 2051 |
}, |
| 2052 |
"root[2]['nested'][0]": { |
| 2053 |
"id": 1, |
| 2054 |
"val": 1 |
| 2055 |
}, |
| 2056 |
"root[0]['nested'][1]": { |
| 2057 |
"id": 2, |
| 2058 |
"val": 3 |
| 2059 |
} |
| 2060 |
}, |
| 2061 |
'iterable_item_moved': { |
| 2062 |
'root[0]': { |
| 2063 |
'new_path': 'root[2]', |
| 2064 |
'value': { |
| 2065 |
'id': 1, |
| 2066 |
'val': 3, |
| 2067 |
'nested': [{'id': 2, 'val': 3}] |
| 2068 |
} |
| 2069 |
}, |
| 2070 |
"root[0]['nested'][1]": { |
| 2071 |
'new_path': "root[2]['nested'][0]", |
| 2072 |
'value': { |
| 2073 |
'id': 2, |
| 2074 |
'val': 3 |
| 2075 |
} |
| 2076 |
}, |
| 2077 |
'root[3]': { |
| 2078 |
'new_path': 'root[0]', |
| 2079 |
'value': { |
| 2080 |
'id': 3, |
| 2081 |
'val': 3 |
| 2082 |
} |
| 2083 |
} |
| 2084 |
}, |
| 2085 |
'values_changed': { |
| 2086 |
"root[2]['val']": { |
| 2087 |
'new_value': 3, |
| 2088 |
'new_path': "root[0]['val']", |
| 2089 |
}, |
| 2090 |
"root[2]['nested'][0]['val']": { |
| 2091 |
'new_path': "root[0]['nested'][1]['val']", |
| 2092 |
'new_value': 3, |
| 2093 |
}, |
| 2094 |
} |
| 2095 |
} |
| 2096 |
assert expected_delta_dict == delta_again.diff |
| 2097 |
assert t2 == t1 + delta_again |
| 2098 |
|
| 2099 |
def test_compare_func_with_duplicates_added(self): |
| 2100 |
t1 = [{'id': 3, 'val': 3}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}] |
| 2101 |
t2 = [{'id': 1, 'val': 1}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}, {'id': 3, 'val': 3}] |
| 2102 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 2103 |
expected = { |
| 2104 |
'iterable_item_added': { |
| 2105 |
'root[2]': { |
| 2106 |
'id': 1, |
| 2107 |
'val': 3 |
| 2108 |
} |
| 2109 |
}, |
| 2110 |
'iterable_item_moved': { |
| 2111 |
'root[0]': { |
| 2112 |
'new_path': 'root[3]', |
| 2113 |
'value': { |
| 2114 |
'id': 3, |
| 2115 |
'val': 3 |
| 2116 |
} |
| 2117 |
}, |
| 2118 |
'root[2]': { |
| 2119 |
'new_path': 'root[0]', |
| 2120 |
'value': { |
| 2121 |
'id': 1, |
| 2122 |
'val': 1 |
| 2123 |
} |
| 2124 |
} |
| 2125 |
}, |
| 2126 |
'values_changed': { |
| 2127 |
"root[0]['val']": { |
| 2128 |
'new_value': 1, |
| 2129 |
'old_value': 3, |
| 2130 |
'new_path': "root[2]['val']" |
| 2131 |
} |
| 2132 |
}, |
| 2133 |
} |
| 2134 |
assert expected == ddiff |
| 2135 |
delta = Delta(ddiff) |
| 2136 |
recreated_t2 = t1 + delta |
| 2137 |
assert t2 == recreated_t2 |
| 2138 |
|
| 2139 |
def test_compare_func_swap(self): |
| 2140 |
t1 = [{'id': 1, 'val': 1}, {'id': 1, 'val': 3}] |
| 2141 |
t2 = [{'id': 1, 'val': 3}, {'id': 1, 'val': 1}] |
| 2142 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 2143 |
expected = {'values_changed': {"root[0]['val']": {'new_value': 3, 'old_value': 1}, "root[1]['val']": {'new_value': 1, 'old_value': 3}}} |
| 2144 |
assert expected == ddiff |
| 2145 |
delta = Delta(ddiff) |
| 2146 |
recreated_t2 = t1 + delta |
| 2147 |
assert t2 == recreated_t2 |
| 2148 |
|
| 2149 |
def test_compare_func_path_specific(self): |
| 2150 |
t1 = {"path1": [{'id': 1, 'val': 1}, {'id': 2, 'val': 3}], "path2": [{'ID': 4, 'val': 3}, {'ID': 3, 'val': 1}, ], "path3": [{'no_id': 5, 'val': 1}, {'no_id': 6, 'val': 3}]} |
| 2151 |
t2 = {"path1": [{'id': 1, 'val': 1}, {'id': 2, 'val': 3}], "path2": [{'ID': 3, 'val': 1}, {'ID': 4, 'val': 3}], "path3": [{'no_id': 5, 'val': 1}, {'no_id': 6, 'val': 3}]} |
| 2152 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 2153 |
expected = {'iterable_item_moved': {"root['path2'][0]": {'new_path': "root['path2'][1]", 'value': {'ID': 4, 'val': 3}},"root['path2'][1]": {'new_path': "root['path2'][0]", 'value': {'ID': 3, 'val': 1}}}} |
| 2154 |
assert expected == ddiff |
| 2155 |
delta = Delta(ddiff) |
| 2156 |
recreated_t2 = t1 + delta |
| 2157 |
assert t2 == recreated_t2 |
| 2158 |
|
| 2159 |
def test_compare_func_nested_changes(self): |
| 2160 |
t1 = { |
| 2161 |
"TestTable": [ |
| 2162 |
{ |
| 2163 |
"id": "022fb580-800e-11ea-a361-39b3dada34b5", |
| 2164 |
"name": "Max", |
| 2165 |
"NestedTable": [ |
| 2166 |
{ |
| 2167 |
"id": "022fb580-800e-11ea-a361-39b3dada34a6", |
| 2168 |
"NestedField": "Test Field" |
| 2169 |
} |
| 2170 |
] |
| 2171 |
}, |
| 2172 |
{ |
| 2173 |
"id": "022fb580-800e-11ea-a361-12354656532", |
| 2174 |
"name": "Bob", |
| 2175 |
"NestedTable": [ |
| 2176 |
{ |
| 2177 |
"id": "022fb580-800e-11ea-a361-39b3dada34c7", |
| 2178 |
"NestedField": "Test Field 2" |
| 2179 |
}, |
| 2180 |
] |
| 2181 |
}, |
| 2182 |
] |
| 2183 |
} |
| 2184 |
t2 = {"TestTable": [ |
| 2185 |
{ |
| 2186 |
"id": "022fb580-800e-11ea-a361-12354656532", |
| 2187 |
"name": "Bob (Changed Name)", |
| 2188 |
"NestedTable": [ |
| 2189 |
{ |
| 2190 |
"id": "022fb580-800e-11ea-a361-39b3dada34c7", |
| 2191 |
"NestedField": "Test Field 2 (Changed Nested Field)" |
| 2192 |
}, |
| 2193 |
{ |
| 2194 |
"id": "new id", |
| 2195 |
"NestedField": "Test Field 3" |
| 2196 |
}, |
| 2197 |
{ |
| 2198 |
"id": "newer id", |
| 2199 |
"NestedField": "Test Field 4" |
| 2200 |
}, |
| 2201 |
] |
| 2202 |
}, |
| 2203 |
{ |
| 2204 |
"id": "adding_some_random_id", |
| 2205 |
"name": "New Name", |
| 2206 |
"NestedTable": [ |
| 2207 |
{ |
| 2208 |
"id": "random_nested_id_added", |
| 2209 |
"NestedField": "New Nested Field" |
| 2210 |
}, |
| 2211 |
{ |
| 2212 |
"id": "random_nested_id_added2", |
| 2213 |
"NestedField": "New Nested Field2" |
| 2214 |
}, |
| 2215 |
{ |
| 2216 |
"id": "random_nested_id_added3", |
| 2217 |
"NestedField": "New Nested Field43" |
| 2218 |
}, |
| 2219 |
] |
| 2220 |
} |
| 2221 |
]} |
| 2222 |
|
| 2223 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 2224 |
delta = Delta(ddiff) |
| 2225 |
recreated_t2 = t1 + delta |
| 2226 |
assert t2 == recreated_t2 |
| 2227 |
|
| 2228 |
def test_compare_func_deep_nested_changes(self): |
| 2229 |
|
| 2230 |
t1 = { |
| 2231 |
"Locations": [ |
| 2232 |
{ |
| 2233 |
"id": "c4fa7b12-f365-42a9-9544-3efc11963558", |
| 2234 |
"Items": [ |
| 2235 |
{ |
| 2236 |
"id": "2399528f-2556-4e2c-bf9b-c8ea17bc323f" |
| 2237 |
}, |
| 2238 |
{ |
| 2239 |
"id": "2399528f-2556-4e2c-bf9b-c8ea17bc323f1", |
| 2240 |
}, |
| 2241 |
{ |
| 2242 |
"id": "2399528f-2556-4e2c-bf9b-c8ea17bc323f2" |
| 2243 |
}, |
| 2244 |
{ |
| 2245 |
"id": "2399528f-2556-4e2c-bf9b-c8ea17bc323f3" |
| 2246 |
} |
| 2247 |
] |
| 2248 |
}, |
| 2249 |
{ |
| 2250 |
"id": "d9095676-bc41-4cbf-9fd2-7148bb26bcc4", |
| 2251 |
"Items": [ |
| 2252 |
{ |
| 2253 |
"id": "26b78305-df71-40c0-8e98-dcd40b7f716d" |
| 2254 |
}, |
| 2255 |
{ |
| 2256 |
"id": "3235125d-0110-4d0e-847a-24912cf73feb" |
| 2257 |
}, |
| 2258 |
{ |
| 2259 |
"id": "7699552a-add9-4338-aeb9-662bec14c175" |
| 2260 |
}, |
| 2261 |
{ |
| 2262 |
"id": "015e74f0-2c2a-45c0-a172-21758d14bf3a" |
| 2263 |
} |
| 2264 |
] |
| 2265 |
}, |
| 2266 |
{ |
| 2267 |
"id": "41b38757-8984-47fd-890d-8c4ed18c3c47", |
| 2268 |
"Items": [ |
| 2269 |
{ |
| 2270 |
"id": "494e839e-37b1-4cac-b1dc-a44f3e6e7ada" |
| 2271 |
}, |
| 2272 |
{ |
| 2273 |
"id": "60547ca6-3ef0-4b67-8826-2c7b76e67011" |
| 2274 |
}, |
| 2275 |
{ |
| 2276 |
"id": "cee762a0-fbd8-48bb-ba92-be32cf3cf250" |
| 2277 |
}, |
| 2278 |
{ |
| 2279 |
"id": "7a0da2b7-c1e6-45b4-8810-fec7b4b6186d" |
| 2280 |
} |
| 2281 |
] |
| 2282 |
}, |
| 2283 |
{ |
| 2284 |
"id": "c0be071a-5457-497d-9a78-ff7cb561d4d3", |
| 2285 |
"Items": [ |
| 2286 |
{ |
| 2287 |
"id": "e54dcdff-ec99-4941-92eb-c12bb3cbeb91" |
| 2288 |
} |
| 2289 |
] |
| 2290 |
}, |
| 2291 |
{ |
| 2292 |
"id": "dfe4b37b-8df3-4dc6-8686-0588937fbe10", |
| 2293 |
"Items": [ |
| 2294 |
{ |
| 2295 |
"id": "27a574ae-08db-47f9-a9dc-18df59287f4d" |
| 2296 |
}, |
| 2297 |
{ |
| 2298 |
"id": "23edf031-8c4e-43d6-b5bf-4d5ee9008a36", |
| 2299 |
"Containers": [ |
| 2300 |
{"id": "1", "val": 1}, |
| 2301 |
{"id": "2", "val": 2}, |
| 2302 |
{"id": "3", "val": 3}, |
| 2303 |
] |
| 2304 |
}, |
| 2305 |
{ |
| 2306 |
"id": "e1e54643-23ee-496d-b7d2-de67c4bb7d68" |
| 2307 |
}, |
| 2308 |
{ |
| 2309 |
"id": "2f910da3-8cd0-4cf5-81c9-23668fc9477f" |
| 2310 |
}, |
| 2311 |
{ |
| 2312 |
"id": "5e36d258-2a82-49ee-b4fc-db0a8c28b404" |
| 2313 |
}, |
| 2314 |
{ |
| 2315 |
"id": "4bf2ce8d-05ed-4718-a529-8c9e4704e38f" |
| 2316 |
}, |
| 2317 |
] |
| 2318 |
}, |
| 2319 |
] |
| 2320 |
} |
| 2321 |
|
| 2322 |
t2 = { |
| 2323 |
"Locations": [ |
| 2324 |
{ |
| 2325 |
"id": "41b38757-8984-47fd-890d-8c4ed18c3c47", |
| 2326 |
"Items": [ |
| 2327 |
{ |
| 2328 |
"id": "60547ca6-3ef0-4b67-8826-2c7b76e67011" |
| 2329 |
}, |
| 2330 |
{ |
| 2331 |
"id": "cee762a0-fbd8-48bb-ba92-be32cf3cf250" |
| 2332 |
}, |
| 2333 |
{ |
| 2334 |
"id": "7a0da2b7-c1e6-45b4-8810-fec7b4b6186d" |
| 2335 |
} |
| 2336 |
] |
| 2337 |
}, |
| 2338 |
{ |
| 2339 |
"id": "c0be071a-5457-497d-9a78-ff7cb561d4d3", |
| 2340 |
"Items": [ |
| 2341 |
{ |
| 2342 |
"id": "e54dcdff-ec99-4941-92eb-c12bb3cbeb91" |
| 2343 |
} |
| 2344 |
] |
| 2345 |
}, |
| 2346 |
{ |
| 2347 |
"id": "dfe4b37b-8df3-4dc6-8686-0588937fbe10", |
| 2348 |
"Items": [ |
| 2349 |
{ |
| 2350 |
"id": "27a574ae-08db-47f9-a9dc-18df59287f4d" |
| 2351 |
}, |
| 2352 |
{ |
| 2353 |
"id": "27a574ae-08db-47f9-a9dc-88df59287f4d" |
| 2354 |
}, |
| 2355 |
{ |
| 2356 |
"id": "23edf031-8c4e-43d6-b5bf-4d5ee9008a36", |
| 2357 |
"Containers": [ |
| 2358 |
{"id": "1", "val": 1}, |
| 2359 |
{"id": "3", "val": 3}, |
| 2360 |
{"id": "2", "val": 2}, |
| 2361 |
] |
| 2362 |
}, |
| 2363 |
{ |
| 2364 |
"id": "e1e54643-23ee-496d-b7d2-de67c4bb7d68" |
| 2365 |
}, |
| 2366 |
{ |
| 2367 |
"id": "2f910da3-8cd0-4cf5-81c9-23668fc9477f" |
| 2368 |
}, |
| 2369 |
{ |
| 2370 |
"id": "5e36d258-2a82-49ee-b4fc-db0a8c28b404" |
| 2371 |
}, |
| 2372 |
{ |
| 2373 |
"id": "4bf2ce8d-05ed-4718-a529-8c9e4704e38f" |
| 2374 |
}, |
| 2375 |
] |
| 2376 |
}, |
| 2377 |
] |
| 2378 |
} |
| 2379 |
|
| 2380 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2) |
| 2381 |
|
| 2382 |
delta2 = Delta(ddiff) |
| 2383 |
expected_move_1 = {'new_path': "root['Locations'][2]['Items'][2]['Containers'][2]", 'value': {'id': '2', 'val': 2}} |
| 2384 |
expected_move_2 = {'new_path': "root['Locations'][2]['Items'][2]['Containers'][1]", 'value': {'id': '3', 'val': 3}} |
| 2385 |
assert ddiff["iterable_item_moved"]["root['Locations'][4]['Items'][1]['Containers'][1]"] == expected_move_1 |
| 2386 |
assert ddiff["iterable_item_moved"]["root['Locations'][4]['Items'][1]['Containers'][2]"] == expected_move_2 |
| 2387 |
recreated_t2 = t1 + delta2 |
| 2388 |
assert t2 == recreated_t2 |
| 2389 |
|
| 2390 |
def test_delta_force1(self): |
| 2391 |
t1 = { |
| 2392 |
'x': { |
| 2393 |
'y': [1, 2, 3] |
| 2394 |
}, |
| 2395 |
'q': { |
| 2396 |
'r': 'abc', |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
t2 = { |
| 2401 |
'x': { |
| 2402 |
'y': [1, 2, 3, 4] |
| 2403 |
}, |
| 2404 |
'q': { |
| 2405 |
'r': 'abc', |
| 2406 |
't': 0.5, |
| 2407 |
} |
| 2408 |
} |
| 2409 |
|
| 2410 |
diff = DeepDiff(t1, t2) |
| 2411 |
|
| 2412 |
delta = Delta(diff=diff, force=True) |
| 2413 |
result = {} + delta |
| 2414 |
expected = {'x': {'y': {3: 4}}, 'q': {'t': 0.5}} |
| 2415 |
assert expected == result |
| 2416 |
|
| 2417 |
def test_delta_force_fill(self): |
| 2418 |
t1 = { |
| 2419 |
'x': { |
| 2420 |
'y': [{"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}] |
| 2421 |
}, |
| 2422 |
'q': { |
| 2423 |
'r': 'abc', |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
t2 = { |
| 2428 |
'x': { |
| 2429 |
'y': [{"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}] |
| 2430 |
}, |
| 2431 |
'q': { |
| 2432 |
'r': 'abc', |
| 2433 |
't': 0.5, |
| 2434 |
} |
| 2435 |
} |
| 2436 |
|
| 2437 |
diff = DeepDiff(t1, t2) |
| 2438 |
|
| 2439 |
delta = Delta(diff=diff, force=True) |
| 2440 |
result = {"x": {"y": [1,]}} + delta |
| 2441 |
expected = {'x': {'y': [1]}, 'q': {'t': 0.5}} |
| 2442 |
assert expected == result |
| 2443 |
|
| 2444 |
|
| 2445 |
delta = Delta(diff=diff, force=True, fill=None) |
| 2446 |
result = {"x": {"y": [1,]}} + delta |
| 2447 |
expected = {'x': {'y': [1, None, None, None, {"b": "c"}, {"b": "c"}, {"b": "c"}]}, 'q': {'t': 0.5}} |
| 2448 |
assert expected == result |
| 2449 |
|
| 2450 |
|
| 2451 |
def fill_func(obj, value, path): |
| 2452 |
return value.copy() |
| 2453 |
|
| 2454 |
delta = Delta(diff=diff, force=True, fill=fill_func) |
| 2455 |
result = {"x": {"y": [1,]}} + delta |
| 2456 |
expected = {'x': {'y': [1, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}, {"b": "c"}]}, 'q': {'t': 0.5}} |
| 2457 |
assert expected == result |
| 2458 |
|
| 2459 |
def test_flatten_dict_with_one_key_added(self): |
| 2460 |
t1 = {"field1": {"joe": "Joe"}} |
| 2461 |
t2 = {"field1": {"joe": "Joe Nobody"}, "field2": {"jimmy": "Jimmy"}} |
| 2462 |
diff = DeepDiff(t1, t2) |
| 2463 |
delta = Delta(diff=diff, always_include_values=True) |
| 2464 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2465 |
flat_expected = [ |
| 2466 |
{'path': ['field2', 'jimmy'], 'value': 'Jimmy', 'action': 'dictionary_item_added'}, |
| 2467 |
{'path': ['field1', 'joe'], 'action': 'values_changed', 'value': 'Joe Nobody'}, |
| 2468 |
] |
| 2469 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 2470 |
assert flat_expected == flat_result |
| 2471 |
|
| 2472 |
delta_again = Delta(flat_rows_list=flat_expected, force=True) # We need to enable force so it creates the dictionary when added to t1 |
| 2473 |
expected_data_again_diff = {'dictionary_item_added': {"root['field2']['jimmy']": 'Jimmy'}, 'values_changed': {"root['field1']['joe']": {'new_value': 'Joe Nobody'}}} |
| 2474 |
|
| 2475 |
assert delta.diff != delta_again.diff, "Since a dictionary containing a single field was created, the flat dict acted like one key was added." |
| 2476 |
assert expected_data_again_diff == delta_again.diff, "Since a dictionary containing a single field was created, the flat dict acted like one key was added." |
| 2477 |
|
| 2478 |
assert t2 == t1 + delta_again |
| 2479 |
|
| 2480 |
def test_flatten_dict_with_multiple_keys_added(self): |
| 2481 |
t1 = {"field1": {"joe": "Joe"}} |
| 2482 |
t2 = {"field1": {"joe": "Joe Nobody"}, "field2": {"jimmy": "Jimmy", "sar": "Sarah"}} |
| 2483 |
diff = DeepDiff(t1, t2) |
| 2484 |
delta = Delta(diff=diff, always_include_values=True) |
| 2485 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2486 |
flat_expected = [ |
| 2487 |
{'path': ['field2'], 'value': {'jimmy': 'Jimmy', 'sar': 'Sarah'}, 'action': 'dictionary_item_added'}, |
| 2488 |
{'path': ['field1', 'joe'], 'action': 'values_changed', 'value': 'Joe Nobody'}, |
| 2489 |
] |
| 2490 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 2491 |
assert flat_expected == flat_result |
| 2492 |
|
| 2493 |
delta_again = Delta(flat_rows_list=flat_expected) |
| 2494 |
assert delta.diff == delta_again.diff |
| 2495 |
|
| 2496 |
def test_flatten_list_with_one_item_added(self): |
| 2497 |
t1 = {"field1": {"joe": "Joe"}} |
| 2498 |
t2 = {"field1": {"joe": "Joe"}, "field2": ["James"]} |
| 2499 |
t3 = {"field1": {"joe": "Joe"}, "field2": ["James", "Jack"]} |
| 2500 |
diff = DeepDiff(t1, t2) |
| 2501 |
delta = Delta(diff=diff, always_include_values=True) |
| 2502 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2503 |
flat_expected = [{'path': ['field2', 0], 'value': 'James', 'action': 'iterable_item_added'}] |
| 2504 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 2505 |
assert flat_expected == flat_result |
| 2506 |
|
| 2507 |
delta_again = Delta(flat_rows_list=flat_expected, force=True) |
| 2508 |
assert {'iterable_item_added': {"root['field2'][0]": 'James'}} == delta_again.diff |
| 2509 |
# delta_again.DEBUG = True |
| 2510 |
assert t2 == t1 + delta_again |
| 2511 |
|
| 2512 |
diff2 = DeepDiff(t2, t3) |
| 2513 |
delta2 = Delta(diff=diff2, always_include_values=True) |
| 2514 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 2515 |
flat_expected2 = [{'path': ['field2', 1], 'value': 'Jack', 'action': 'iterable_item_added'}] |
| 2516 |
flat_expected2 = [FlatDeltaRow(**i) for i in flat_expected2] |
| 2517 |
|
| 2518 |
assert flat_expected2 == flat_result2 |
| 2519 |
|
| 2520 |
delta_again2 = Delta(flat_rows_list=flat_expected2, force=True) |
| 2521 |
|
| 2522 |
assert {'iterable_item_added': {"root['field2'][1]": 'Jack'}} == delta_again2.diff |
| 2523 |
assert t3 == t2 + delta_again2 |
| 2524 |
|
| 2525 |
def test_flatten_set_with_one_item_added(self): |
| 2526 |
t1 = {"field1": {"joe": "Joe"}} |
| 2527 |
t2 = {"field1": {"joe": "Joe"}, "field2": {"James"}} |
| 2528 |
t3 = {"field1": {"joe": "Joe"}, "field2": {"James", "Jack"}} |
| 2529 |
diff = DeepDiff(t1, t2) |
| 2530 |
delta = Delta(diff=diff, always_include_values=True) |
| 2531 |
assert t2 == t1 + delta |
| 2532 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2533 |
flat_expected = [{'path': ['field2'], 'value': 'James', 'action': 'set_item_added'}] |
| 2534 |
flat_expected = [FlatDeltaRow(**i) for i in flat_expected] |
| 2535 |
assert flat_expected == flat_result |
| 2536 |
|
| 2537 |
delta_again = Delta(flat_rows_list=flat_expected, force=True) |
| 2538 |
assert {'set_item_added': {"root['field2']": {'James'}}} == delta_again.diff |
| 2539 |
assert t2 == t1 + delta_again |
| 2540 |
|
| 2541 |
diff = DeepDiff(t2, t3) |
| 2542 |
delta2 = Delta(diff=diff, always_include_values=True) |
| 2543 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 2544 |
flat_expected2 = [{'path': ['field2'], 'value': 'Jack', 'action': 'set_item_added'}] |
| 2545 |
flat_expected2 = [FlatDeltaRow(**i) for i in flat_expected2] |
| 2546 |
|
| 2547 |
assert flat_expected2 == flat_result2 |
| 2548 |
|
| 2549 |
delta_again2 = Delta(flat_rows_list=flat_expected2, force=True) |
| 2550 |
assert {'set_item_added': {"root['field2']": {'Jack'}}} == delta_again2.diff |
| 2551 |
assert t3 == t2 + delta_again2 |
| 2552 |
|
| 2553 |
def test_flatten_tuple_with_one_item_added(self): |
| 2554 |
t1 = {"field1": {"joe": "Joe"}} |
| 2555 |
t2 = {"field1": {"joe": "Joe"}, "field2": ("James", )} |
| 2556 |
t3 = {"field1": {"joe": "Joe"}, "field2": ("James", "Jack")} |
| 2557 |
diff = DeepDiff(t1, t2) |
| 2558 |
delta = Delta(diff=diff, always_include_values=True) |
| 2559 |
assert t2 == t1 + delta |
| 2560 |
flat_expected = delta.to_flat_rows(report_type_changes=False) |
| 2561 |
expected_result = [{'path': ['field2', 0], 'value': 'James', 'action': 'iterable_item_added'}] |
| 2562 |
expected_result = [FlatDeltaRow(**i) for i in expected_result] |
| 2563 |
|
| 2564 |
assert expected_result == flat_expected |
| 2565 |
|
| 2566 |
delta_again = Delta(flat_rows_list=flat_expected, force=True) |
| 2567 |
assert {'iterable_item_added': {"root['field2'][0]": 'James'}} == delta_again.diff |
| 2568 |
assert {'field1': {'joe': 'Joe'}, 'field2': ['James']} == t1 + delta_again, "We lost the information about tuple when we convert to flat dict." |
| 2569 |
|
| 2570 |
diff = DeepDiff(t2, t3) |
| 2571 |
delta2 = Delta(diff=diff, always_include_values=True, force=True) |
| 2572 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 2573 |
expected_result2 = [{'path': ['field2', 1], 'value': 'Jack', 'action': 'iterable_item_added'}] |
| 2574 |
expected_result2 = [FlatDeltaRow(**i) for i in expected_result2] |
| 2575 |
|
| 2576 |
assert expected_result2 == flat_result2 |
| 2577 |
assert t3 == t2 + delta2 |
| 2578 |
|
| 2579 |
delta_again2 = Delta(flat_rows_list=flat_result2) |
| 2580 |
assert {'iterable_item_added': {"root['field2'][1]": 'Jack'}} == delta_again2.diff |
| 2581 |
assert t3 == t2 + delta_again2 |
| 2582 |
|
| 2583 |
def test_flatten_list_with_multiple_item_added(self): |
| 2584 |
t1 = {"field1": {"joe": "Joe"}} |
| 2585 |
t2 = {"field1": {"joe": "Joe"}, "field2": ["James", "Jack"]} |
| 2586 |
diff = DeepDiff(t1, t2) |
| 2587 |
delta = Delta(diff=diff, always_include_values=True) |
| 2588 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2589 |
expected_result = [{'path': ['field2'], 'value': ['James', 'Jack'], 'action': 'dictionary_item_added'}] |
| 2590 |
expected_result = [FlatDeltaRow(**i) for i in expected_result] |
| 2591 |
|
| 2592 |
assert expected_result == flat_result |
| 2593 |
|
| 2594 |
delta2 = Delta(diff=diff, bidirectional=True, always_include_values=True) |
| 2595 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 2596 |
assert expected_result == flat_result2 |
| 2597 |
|
| 2598 |
delta_again = Delta(flat_rows_list=flat_result) |
| 2599 |
assert delta.diff == delta_again.diff |
| 2600 |
|
| 2601 |
def test_flatten_attribute_added(self): |
| 2602 |
t1 = picklalbe_obj_without_item |
| 2603 |
t2 = PicklableClass(10) |
| 2604 |
diff = DeepDiff(t1, t2) |
| 2605 |
delta = Delta(diff=diff, always_include_values=True) |
| 2606 |
flat_result = delta.to_flat_rows(report_type_changes=False) |
| 2607 |
expected_result = [{'path': ['item'], 'value': 10, 'action': 'attribute_added'}] |
| 2608 |
expected_result = [FlatDeltaRow(**i) for i in expected_result] |
| 2609 |
|
| 2610 |
assert expected_result == flat_result |
| 2611 |
|
| 2612 |
delta_again = Delta(flat_rows_list=flat_result) |
| 2613 |
assert delta.diff == delta_again.diff |
| 2614 |
|
| 2615 |
def test_flatten_when_simple_type_change(self): |
| 2616 |
t1 = [1, 2, '3'] |
| 2617 |
t2 = [1, 2, 3] |
| 2618 |
|
| 2619 |
diff = DeepDiff(t1, t2) |
| 2620 |
expected_diff = { |
| 2621 |
'type_changes': {'root[2]': {'old_type': str, 'new_type': int, 'old_value': '3', 'new_value': 3}} |
| 2622 |
} |
| 2623 |
|
| 2624 |
assert expected_diff == diff |
| 2625 |
delta = Delta(diff=diff) |
| 2626 |
with pytest.raises(ValueError) as exc_info: |
| 2627 |
delta.to_flat_rows(report_type_changes=False) |
| 2628 |
assert str(exc_info.value).startswith("When converting to flat dictionaries") |
| 2629 |
|
| 2630 |
delta2 = Delta(diff=diff, always_include_values=True) |
| 2631 |
flat_result2 = delta2.to_flat_rows(report_type_changes=False) |
| 2632 |
expected_result2 = [{'path': [2], 'action': 'values_changed', 'value': 3}] |
| 2633 |
expected_result2 = [FlatDeltaRow(**i) for i in expected_result2] |
| 2634 |
|
| 2635 |
assert expected_result2 == flat_result2 |
| 2636 |
|
| 2637 |
delta3 = Delta(diff=diff, always_include_values=True, bidirectional=True) |
| 2638 |
flat_result3 = delta3.to_flat_rows(report_type_changes=False) |
| 2639 |
|
| 2640 |
expected_result3 = [{'path': [2], 'action': 'values_changed', 'value': 3, 'old_value': '3'}] |
| 2641 |
expected_result3 = [FlatDeltaRow(**i) for i in expected_result3] |
| 2642 |
assert expected_result3 == flat_result3 |
| 2643 |
|
| 2644 |
delta_again = Delta(flat_rows_list=flat_result3) |
| 2645 |
assert {'values_changed': {'root[2]': {'new_value': 3, 'old_value': '3'}}} == delta_again.diff |
| 2646 |
|
| 2647 |
def test_subtract_delta1(self): |
| 2648 |
t1 = {'field_name1': ['yyy']} |
| 2649 |
t2 = {'field_name1': ['xxx', 'yyy']} |
| 2650 |
delta_diff = {'iterable_items_removed_at_indexes': {"root['field_name1']": {(0, 'GET'): 'xxx'}}} |
| 2651 |
expected_reverse_diff = {'iterable_items_added_at_indexes': {"root['field_name1']": {(0, 'GET'): 'xxx'}}} |
| 2652 |
|
| 2653 |
delta = Delta(delta_diff=delta_diff, bidirectional=True) |
| 2654 |
reversed_diff = delta._get_reverse_diff() |
| 2655 |
assert expected_reverse_diff == reversed_diff |
| 2656 |
assert t2 != {'field_name1': ['yyy', 'xxx']} == t1 - delta, "Since iterable_items_added_at_indexes is used when ignore_order=True, the order is not necessarily the original order." |
| 2657 |
|
| 2658 |
def test_subtract_delta_made_from_flat_dicts1(self): |
| 2659 |
t1 = {'field_name1': ['xxx', 'yyy']} |
| 2660 |
t2 = {'field_name1': []} |
| 2661 |
diff = DeepDiff(t1, t2) |
| 2662 |
delta = Delta(diff=diff, bidirectional=True) |
| 2663 |
flat_rows_list = delta.to_flat_rows(include_action_in_path=False, report_type_changes=True) |
| 2664 |
expected_flat_dicts = [{ |
| 2665 |
'path': ['field_name1', 0], |
| 2666 |
'value': 'xxx', |
| 2667 |
'action': 'iterable_item_removed', |
| 2668 |
'type': str, |
| 2669 |
}, { |
| 2670 |
'path': ['field_name1', 1], |
| 2671 |
'value': 'yyy', |
| 2672 |
'action': 'iterable_item_removed', |
| 2673 |
'type': str, |
| 2674 |
}] |
| 2675 |
expected_flat_dicts = [FlatDeltaRow(**i) for i in expected_flat_dicts] |
| 2676 |
|
| 2677 |
assert expected_flat_dicts == flat_rows_list |
| 2678 |
|
| 2679 |
delta1 = Delta(flat_rows_list=flat_rows_list, bidirectional=True, force=True) |
| 2680 |
assert t1 == t2 - delta1 |
| 2681 |
|
| 2682 |
delta2 = Delta(flat_rows_list=[flat_rows_list[0]], bidirectional=True, force=True) |
| 2683 |
middle_t = t2 - delta2 |
| 2684 |
assert {'field_name1': ['xxx']} == middle_t |
| 2685 |
|
| 2686 |
delta3 = Delta(flat_rows_list=[flat_rows_list[1]], bidirectional=True, force=True) |
| 2687 |
assert t1 == middle_t - delta3 |
| 2688 |
|
| 2689 |
def test_subtract_delta_made_from_flat_dicts2(self): |
| 2690 |
t1 = {'field_name1': []} |
| 2691 |
t2 = {'field_name1': ['xxx', 'yyy']} |
| 2692 |
diff = DeepDiff(t1, t2) |
| 2693 |
delta = Delta(diff=diff, bidirectional=True) |
| 2694 |
flat_rows_list = delta.to_flat_rows(include_action_in_path=False, report_type_changes=True) |
| 2695 |
expected_flat_dicts = [{ |
| 2696 |
'path': ['field_name1', 0], |
| 2697 |
'value': 'xxx', |
| 2698 |
'action': 'iterable_item_added', |
| 2699 |
'type': str, |
| 2700 |
}, { |
| 2701 |
'path': ['field_name1', 1], |
| 2702 |
'value': 'yyy', |
| 2703 |
'action': 'iterable_item_added', |
| 2704 |
'type': str, |
| 2705 |
}] |
| 2706 |
expected_flat_dicts = [FlatDeltaRow(**i) for i in expected_flat_dicts] |
| 2707 |
|
| 2708 |
assert expected_flat_dicts == flat_rows_list |
| 2709 |
|
| 2710 |
delta1 = Delta(flat_rows_list=flat_rows_list, bidirectional=True, force=True) |
| 2711 |
assert t1 == t2 - delta1 |
| 2712 |
|
| 2713 |
# We need to subtract the changes in the reverse order if we want to feed the flat dict rows individually to Delta |
| 2714 |
delta2 = Delta(flat_rows_list=[flat_rows_list[0]], bidirectional=True, force=True) |
| 2715 |
middle_t = t2 - delta2 |
| 2716 |
assert {'field_name1': ['yyy']} == middle_t |
| 2717 |
|
| 2718 |
delta3 = Delta(flat_rows_list=[flat_rows_list[1]], bidirectional=True, force=True) |
| 2719 |
delta3.DEBUG = True |
| 2720 |
assert t1 == middle_t - delta3 |
| 2721 |
|
| 2722 |
def test_list_of_alphabet_and_its_delta(self): |
| 2723 |
l1 = "A B C D E F G D H".split() |
| 2724 |
l2 = "B C X D H Y Z".split() |
| 2725 |
diff = DeepDiff(l1, l2) |
| 2726 |
|
| 2727 |
# Problem: The index of values_changed should be either all for AFTER removals or BEFORE removals. |
| 2728 |
# What we have here is that F & G transformation to Y and Z is not compatible with A and E removal |
| 2729 |
# it is really meant for the removals to happen first, and then have indexes in L2 for values changing |
| 2730 |
# rather than indexes in L1. Here what we need to have is: |
| 2731 |
# A B C D E F G D H |
| 2732 |
# A B C-X-E |
| 2733 |
# B C D F G D H # removal |
| 2734 |
|
| 2735 |
# What we really need is to report is as it is in difflib for delta specifically: |
| 2736 |
# A B C D E F G D H |
| 2737 |
# B C D E F G D H delete t1[0:1] --> t2[0:0] ['A'] --> [] |
| 2738 |
# B C D E F G D H equal t1[1:3] --> t2[0:2] ['B', 'C'] --> ['B', 'C'] |
| 2739 |
# B C X D H replace t1[3:7] --> t2[2:3] ['D', 'E', 'F', 'G'] --> ['X'] |
| 2740 |
# B C X D H equal t1[7:9] --> t2[3:5] ['D', 'H'] --> ['D', 'H'] |
| 2741 |
# B C X D H Y Z insert t1[9:9] --> t2[5:7] [] --> ['Y', 'Z'] |
| 2742 |
|
| 2743 |
# So in this case, it needs to also include information about what stays equal in the delta |
| 2744 |
# NOTE: the problem is that these operations need to be performed in a specific order. |
| 2745 |
# DeepDiff removes that order and just buckets all insertions vs. replace vs. delete in their own buckets. |
| 2746 |
# For times that we use Difflib, we may want to keep the information for the array_change key |
| 2747 |
# just for the sake of delta, but not for reporting in deepdiff itself. |
| 2748 |
# that way we can re-apply the changes as they were reported in delta. |
| 2749 |
|
| 2750 |
delta = Delta(diff) |
| 2751 |
assert l2 == l1 + delta |
| 2752 |
with pytest.raises(ValueError) as exc_info: |
| 2753 |
l1 == l2 - delta |
| 2754 |
assert "Please recreate the delta with bidirectional=True" == str(exc_info.value) |
| 2755 |
|
| 2756 |
delta2 = Delta(diff, bidirectional=True) |
| 2757 |
assert l2 == l1 + delta2 |
| 2758 |
assert l1 == l2 - delta2 |
| 2759 |
|
| 2760 |
dump = Delta(diff, bidirectional=True).dumps() |
| 2761 |
delta3 = Delta(dump, bidirectional=True) |
| 2762 |
|
| 2763 |
assert l2 == l1 + delta3 |
| 2764 |
assert l1 == l2 - delta3 |
| 2765 |
|
| 2766 |
dump4 = Delta(diff, bidirectional=True, serializer=json_dumps).dumps() |
| 2767 |
delta4 = Delta(dump4, bidirectional=True, deserializer=json_loads) |
| 2768 |
|
| 2769 |
assert l2 == l1 + delta4 |
| 2770 |
assert l1 == l2 - delta4 |
| 2771 |
|
| 2772 |
flat_rows = delta2.to_flat_rows() |
| 2773 |
|
| 2774 |
expected_flat_rows = [ |
| 2775 |
FlatDeltaRow(path=[3], action='values_changed', value='X', old_value='D', type=str, old_type=str, new_path=[2]), |
| 2776 |
FlatDeltaRow(path=[6], action='values_changed', value='Z', old_value='G', type=str, old_type=str), |
| 2777 |
FlatDeltaRow(path=[5], action='values_changed', value='Y', old_value='F', type=str, old_type=str), |
| 2778 |
FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_deleted, value=[], old_value=['A'], type=list, old_type=list, t1_from_index=0, t1_to_index=1, t2_from_index=0, t2_to_index=0), |
| 2779 |
FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_equal, value=None, old_value=None, type=type(None), old_type=type(None), t1_from_index=1, t1_to_index=3, t2_from_index=0, t2_to_index=2), |
| 2780 |
FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_replaced, value=['X'], old_value=['D', 'E', 'F', 'G'], type=list, old_type=list, t1_from_index=3, t1_to_index=7, t2_from_index=2, t2_to_index=3), |
| 2781 |
FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_equal, value=None, old_value=None, type=type(None), old_type=type(None), t1_from_index=7, t1_to_index=9, t2_from_index=3, t2_to_index=5), |
| 2782 |
FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_inserted, value=['Y', 'Z'], old_value=[], type=list, old_type=list, t1_from_index=9, t1_to_index=9, t2_from_index=5, t2_to_index=7) |
| 2783 |
] |
| 2784 |
|
| 2785 |
# The order of the first 3 items is not deterministic |
| 2786 |
assert not DeepDiff(expected_flat_rows[:3], flat_rows[:3], ignore_order=True) |
| 2787 |
assert expected_flat_rows[3:] == flat_rows[3:] |
| 2788 |
|
| 2789 |
delta5 = Delta(flat_rows_list=flat_rows, bidirectional=True, force=True) |
| 2790 |
|
| 2791 |
|
| 2792 |
assert l2 == l1 + delta5 |
| 2793 |
assert l1 == l2 - delta5 |
| 2794 |
|
| 2795 |
def test_delta_flat_rows(self): |
| 2796 |
t1 = {"key1": "value1"} |
| 2797 |
t2 = {"field2": {"key2": "value2"}} |
| 2798 |
diff = DeepDiff(t1, t2, verbose_level=2) |
| 2799 |
delta = Delta(diff, bidirectional=True) |
| 2800 |
assert t1 + delta == t2 |
| 2801 |
flat_rows = delta.to_flat_rows() |
| 2802 |
# we need to set force=True because when we create flat rows, if a nested |
| 2803 |
# dictionary with a single key is created, the path in the flat row will be |
| 2804 |
# the path to the leaf node. |
| 2805 |
delta2 = Delta(flat_rows_list=flat_rows, bidirectional=True, force=True) |
| 2806 |
assert t1 + delta2 == t2 |
| 2807 |
|
| 2808 |
def test_delta_bool(self): |
| 2809 |
flat_rows_list = [FlatDeltaRow(path=['dollar_to_cent'], action='values_changed', value=False, old_value=True, type=bool, old_type=bool)] |
| 2810 |
value = {'dollar_to_cent': False} |
| 2811 |
delta = Delta(flat_rows_list=flat_rows_list, bidirectional=True, force=True) |
| 2812 |
assert {'dollar_to_cent': True} == value - delta |
| 2813 |
|
| 2814 |
def test_detla_add_to_empty_iterable_and_flatten(self): |
| 2815 |
t1 = {'models': [], 'version_id': 0} |
| 2816 |
t2 = {'models': ['Super Duty F-250'], 'version_id': 1} |
| 2817 |
t3 = {'models': ['Super Duty F-250', 'Focus'], 'version_id': 1} |
| 2818 |
diff = DeepDiff(t1, t2, verbose_level=2) |
| 2819 |
delta = Delta(diff, bidirectional=True) |
| 2820 |
assert t1 + delta == t2 |
| 2821 |
flat_rows = delta.to_flat_rows() |
| 2822 |
delta2 = Delta(flat_rows_list=flat_rows, bidirectional=True) # , force=True |
| 2823 |
assert t1 + delta2 == t2 |
| 2824 |
assert t2 - delta2 == t1 |
| 2825 |
|
| 2826 |
diff3 = DeepDiff(t2, t3) |
| 2827 |
delta3 = Delta(diff3, bidirectional=True) |
| 2828 |
flat_dicts3 = delta3.to_flat_dicts() |
| 2829 |
|
| 2830 |
delta3_again = Delta(flat_dict_list=flat_dicts3, bidirectional=True) |
| 2831 |
assert t2 + delta3_again == t3 |
| 2832 |
assert t3 - delta3_again == t2 |
| 2833 |
|
| 2834 |
|
| 2835 |
def test_flat_dict_and_deeply_nested_dict(self): |
| 2836 |
beforeImage = [ |
| 2837 |
{ |
| 2838 |
"usage": "Mailing", |
| 2839 |
"standardization": "YES", |
| 2840 |
"primaryIndicator": True, |
| 2841 |
"addressIdentifier": "Z8PDWBG42YC", |
| 2842 |
"addressLines": ["871 PHILLIPS FERRY RD"], |
| 2843 |
}, |
| 2844 |
{ |
| 2845 |
"usage": "Residence", |
| 2846 |
"standardization": "YES", |
| 2847 |
"primaryIndicator": False, |
| 2848 |
"addressIdentifier": "Z8PDWBG42YC", |
| 2849 |
"addressLines": ["871 PHILLIPS FERRY RD"], |
| 2850 |
}, |
| 2851 |
{ |
| 2852 |
"usage": "Mailing", |
| 2853 |
"standardization": None, |
| 2854 |
"primaryIndicator": False, |
| 2855 |
"addressIdentifier": "MHPP3BY0BYC", |
| 2856 |
"addressLines": ["871 PHILLIPS FERRY RD", "APT RV92"], |
| 2857 |
}, |
| 2858 |
] |
| 2859 |
allAfterImage = [ |
| 2860 |
{ |
| 2861 |
"usage": "Residence", |
| 2862 |
"standardization": "NO", |
| 2863 |
"primaryIndicator": False, |
| 2864 |
"addressIdentifier": "Z8PDWBG42YC", |
| 2865 |
"addressLines": ["871 PHILLIPS FERRY RD"], |
| 2866 |
}, |
| 2867 |
{ |
| 2868 |
"usage": "Mailing", |
| 2869 |
"standardization": None, |
| 2870 |
"primaryIndicator": False, |
| 2871 |
"addressIdentifier": "MHPP3BY0BYC", |
| 2872 |
"addressLines": ["871 PHILLIPS FERRY RD", "APT RV92"], |
| 2873 |
}, |
| 2874 |
{ |
| 2875 |
"usage": "Mailing", |
| 2876 |
"standardization": "NO", |
| 2877 |
"primaryIndicator": True, |
| 2878 |
"addressIdentifier": "Z8PDWBG42YC", |
| 2879 |
"addressLines": ["871 PHILLIPS FERRY RD"], |
| 2880 |
}, |
| 2881 |
] |
| 2882 |
|
| 2883 |
diff = DeepDiff( |
| 2884 |
beforeImage, |
| 2885 |
allAfterImage, |
| 2886 |
ignore_order=True, |
| 2887 |
report_repetition=True, |
| 2888 |
) |
| 2889 |
# reverse_diff = DeepDiff( |
| 2890 |
# allAfterImage, |
| 2891 |
# beforeImage, |
| 2892 |
# ignore_order=True, |
| 2893 |
# report_repetition=True, |
| 2894 |
# ) |
| 2895 |
delta = Delta( |
| 2896 |
diff, always_include_values=True, bidirectional=True |
| 2897 |
) |
| 2898 |
# reverse_delta = Delta( |
| 2899 |
# reverse_diff, always_include_values=True, bidirectional=True |
| 2900 |
# ) |
| 2901 |
allAfterImageAgain = beforeImage + delta |
| 2902 |
diff2 = DeepDiff(allAfterImage, allAfterImageAgain, ignore_order=True) |
| 2903 |
assert not diff2 |
| 2904 |
|
| 2905 |
# print("\ndelta.diff") |
| 2906 |
# pprint(delta.diff) |
| 2907 |
# print("\ndelta._get_reverse_diff()") |
| 2908 |
# pprint(delta._get_reverse_diff()) |
| 2909 |
# print("\nreverse_delta.diff") |
| 2910 |
# pprint(reverse_delta.diff) |
| 2911 |
beforeImageAgain = allAfterImage - delta |
| 2912 |
diff3 = DeepDiff(beforeImage, beforeImageAgain, ignore_order=True) |
| 2913 |
assert not diff3 |
| 2914 |
|
| 2915 |
# ------ now let's recreate the delta from flat dicts ------- |
| 2916 |
|
| 2917 |
flat_dict_list = delta.to_flat_dicts() |
| 2918 |
|
| 2919 |
delta2 = Delta( |
| 2920 |
flat_dict_list=flat_dict_list, |
| 2921 |
always_include_values=True, |
| 2922 |
bidirectional=True, |
| 2923 |
raise_errors=False, |
| 2924 |
force=True, |
| 2925 |
) |
| 2926 |
# print("\ndelta from flat dicts") |
| 2927 |
# pprint(delta2.diff) |
| 2928 |
allAfterImageAgain2 = beforeImage + delta2 |
| 2929 |
diff4 = DeepDiff(allAfterImage, allAfterImageAgain2, ignore_order=True) |
| 2930 |
assert not diff4 |
| 2931 |
|
| 2932 |
beforeImageAgain2 = allAfterImage - delta2 |
| 2933 |
diff4 = DeepDiff(beforeImage, beforeImageAgain2, ignore_order=True) |
| 2934 |
assert not diff4 |
| 2935 |
|
| 2936 |
def test_moved_and_changed_flat(self): |
| 2937 |
"""Items that both move (due to prepended inserts) and change values |
| 2938 |
should produce t2 with no phantom entries after delta replay.""" |
| 2939 |
t1 = [{"id": "a", "val": 1}, {"id": "b", "val": 2}, |
| 2940 |
{"id": "c", "val": 3}] |
| 2941 |
t2 = [ |
| 2942 |
{"id": "new1", "val": 10}, # inserted at [0] |
| 2943 |
{"id": "new2", "val": 20}, # inserted at [1] |
| 2944 |
{"id": "a", "val": 0.5}, # moved [0]→[2], val changed |
| 2945 |
{"id": "b", "val": 1.0}, # moved [1]→[3], val changed |
| 2946 |
{"id": "c", "val": 1.5}, # moved [2]→[4], val changed |
| 2947 |
] |
| 2948 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, |
| 2949 |
threshold_to_diff_deeper=0) |
| 2950 |
result = t1 + Delta(ddiff, force=True, raise_errors=True, |
| 2951 |
log_errors=False) |
| 2952 |
assert [item for item in result if "id" not in item] == [], \ |
| 2953 |
"Phantom entries (dicts missing 'id') found in result" |
| 2954 |
assert result == t2 |
| 2955 |
|
| 2956 |
def test_moved_and_changed_nested(self): |
| 2957 |
"""Same bug in a nested structure: inner list items that both move and |
| 2958 |
change values should produce no phantom entries after delta replay.""" |
| 2959 |
t1 = {"rows": [ |
| 2960 |
{"id": "r1", "items": [{"id": "a", "val": 1}, |
| 2961 |
{"id": "b", "val": 2}]}, |
| 2962 |
]} |
| 2963 |
t2 = {"rows": [ |
| 2964 |
{"id": "r1", "items": [ |
| 2965 |
{"id": "new1", "val": 99}, # inserted at [0] |
| 2966 |
{"id": "a", "val": 0.5}, # moved [0]→[1], val changed |
| 2967 |
{"id": "b", "val": 1.0}, # moved [1]→[2], val changed |
| 2968 |
]}, |
| 2969 |
]} |
| 2970 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, |
| 2971 |
threshold_to_diff_deeper=0) |
| 2972 |
result = t1 + Delta(ddiff, force=True, raise_errors=True, |
| 2973 |
log_errors=False) |
| 2974 |
assert [item for item in result["rows"][0][ |
| 2975 |
"items"] if "id" not in item] == [], \ |
| 2976 |
"Phantom entries (dicts missing 'id') found in nested result" |
| 2977 |
assert result == t2 |
| 2978 |
|
| 2979 |
def test_appended_only_no_movement_sanity_check(self): |
| 2980 |
""" |
| 2981 |
When new items are only appended (existing items keep their positions), |
| 2982 |
stock Delta produces the correct result with no phantom entries. |
| 2983 |
""" |
| 2984 |
t1 = [{"id": "a", "val": 1}, {"id": "b", "val": 2}] |
| 2985 |
t2 = [ |
| 2986 |
{"id": "a", "val": 0.5}, # stays at [0], val changed |
| 2987 |
{"id": "b", "val": 1.0}, # stays at [1], val changed |
| 2988 |
{"id": "new1", "val": 10}, # appended |
| 2989 |
{"id": "new2", "val": 20}, # appended |
| 2990 |
] |
| 2991 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, |
| 2992 |
threshold_to_diff_deeper=0) |
| 2993 |
result = t1 + Delta(ddiff, force=True, raise_errors=True, |
| 2994 |
log_errors=False) |
| 2995 |
assert [item for item in result if "id" not in item] == [] |
| 2996 |
assert result == t2 |
| 2997 |
|
| 2998 |
def test_moved_and_type_changed(self): |
| 2999 |
"""Items that both move and change type should apply correctly.""" |
| 3000 |
t1 = [{"id": "a", "val": "1"}, {"id": "b", "val": "2"}] |
| 3001 |
t2 = [ |
| 3002 |
{"id": "new1", "val": 99}, |
| 3003 |
{"id": "a", "val": 1}, # moved [0]->[1], val str->int |
| 3004 |
{"id": "b", "val": 2}, # moved [1]->[2], val str->int |
| 3005 |
] |
| 3006 |
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, |
| 3007 |
threshold_to_diff_deeper=0) |
| 3008 |
result = t1 + Delta(ddiff, force=True, raise_errors=True, |
| 3009 |
log_errors=False) |
| 3010 |
assert [item for item in result if "id" not in item] == [], \ |
| 3011 |
"Phantom entries (dicts missing 'id') found in result" |
| 3012 |
assert result == t2 |