Skip to content

Commit

Permalink
Fix for iterable moved items what are found with iterable_compare_func.
Browse files Browse the repository at this point in the history
To stay consistent with other types of reporting, moved items
should be relative to t2. Also, moved items should branch deeper
to look for more nested changes (similar to item added and removed).
  • Loading branch information
dtorres-sf committed Jul 24, 2024
1 parent be22027 commit 80de733
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
32 changes: 5 additions & 27 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,30 +862,6 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(

else: # check if item value has changed

# if (i != j):
# # Item moved
# change_level = level.branch_deeper(
# x,
# y,
# child_relationship_class=child_relationship_class,
# child_relationship_param=i,
# child_relationship_param2=j
# )
# self._report_result('iterable_item_moved', change_level)

# item_id = id(x)
# if parents_ids and item_id in parents_ids:
# continue
# parents_ids_added = add_to_frozen_set(parents_ids, item_id)

# # Go one level deeper
# next_level = level.branch_deeper(
# x,
# y,
# child_relationship_class=child_relationship_class,
# child_relationship_param=j)
# self._diff(next_level, parents_ids_added)

if (i != j and ((x == y) or self.iterable_compare_func)):
# Item moved
change_level = level.branch_deeper(
Expand All @@ -896,20 +872,22 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
child_relationship_param2=j
)
self._report_result('iterable_item_moved', change_level, local_tree=local_tree)
continue

item_id = id(x)
if parents_ids and item_id in parents_ids:
continue
parents_ids_added = add_to_frozen_set(parents_ids, item_id)

# Go one level deeper
# Intentionally setting j as the first child relationship param in cases of a moved item.
# If the item was moved using an iterable_compare_func then we want to make sure that the index
# is relative to t2.
next_level = level.branch_deeper(
x,
y,
child_relationship_class=child_relationship_class,
child_relationship_param=i,
child_relationship_param2=j,
child_relationship_param=j,
child_relationship_param2=i
)
self._diff(next_level, parents_ids_added, local_tree=local_tree)

Expand Down
87 changes: 53 additions & 34 deletions tests/fixtures/compare_func_result1.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
{
"dictionary_item_added": [
"root['Cars'][3]['dealers']"
],
"dictionary_item_removed": [
"root['Cars'][3]['production']"
],
"values_changed": {
"root['Cars'][3]['model']": {
"new_value": "Supra",
"old_value": "supra"
}
"dictionary_item_added": [
"root['Cars'][3]['dealers']"
],
"dictionary_item_removed": [
"root['Cars'][3]['production']"
],
"values_changed": {
"root['Cars'][2]['dealers'][0]['quantity']": {
"new_value": 50,
"old_value": 20
},
"iterable_item_added": {
"root['Cars'][0]": {
"id": "7",
"make": "Toyota",
"model": "8Runner"
}
"root['Cars'][1]['model_numbers'][2]": {
"new_value": 3,
"old_value": 4
},
"root['Cars'][3]['model']": {
"new_value": "Supra",
"old_value": "supra"
}
},
"iterable_item_added": {
"root['Cars'][2]['dealers'][1]": {
"id": 200,
"address": "200 Fake St",
"quantity": 10
},
"root['Cars'][1]['model_numbers'][3]": 4,
"root['Cars'][0]": {
"id": "7",
"make": "Toyota",
"model": "8Runner"
}
},
"iterable_item_removed": {
"root['Cars'][2]['dealers'][0]": {
"id": 103,
"address": "103 Fake St",
"quantity": 50
},
"iterable_item_removed": {
"root['Cars'][1]": {
"id": "2",
"make": "Toyota",
"model": "Highlander",
"dealers": [
{
"id": 123,
"address": "123 Fake St",
"quantity": 50
},
{
"id": 125,
"address": "125 Fake St",
"quantity": 20
}
]
"root['Cars'][1]": {
"id": "2",
"make": "Toyota",
"model": "Highlander",
"dealers": [
{
"id": 123,
"address": "123 Fake St",
"quantity": 50
},
{
"id": 125,
"address": "125 Fake St",
"quantity": 20
}
]
}
}
}

0 comments on commit 80de733

Please sign in to comment.