Skip to content

Commit 225da0a

Browse files
ubaumannKircheneer
authored andcommitted
Add 'skip' counter to diff.summary() (#168)
* Add 'skip' to diff.summary() * Fix location of variable and comment * Update summary in example 6 * Add comment to skip callculation in diff.summary()
1 parent 6aa23cc commit 225da0a

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

diffsync/diff.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self):
3333
3434
`self.children[group][unique_id] == DiffElement(...)`
3535
"""
36+
self.models_processed = 0
3637

3738
def __len__(self):
3839
"""Total number of DiffElements stored herein."""
@@ -115,6 +116,15 @@ def summary(self) -> Mapping[Text, int]:
115116
child_summary = child.summary()
116117
for key in summary:
117118
summary[key] += child_summary[key]
119+
summary[DiffSyncActions.SKIP] = (
120+
self.models_processed
121+
- summary[DiffSyncActions.CREATE]
122+
# Updated elements are doubly accumulated in models_processed as they exist in SCR and DST.
123+
- 2 * summary[DiffSyncActions.UPDATE]
124+
- summary[DiffSyncActions.DELETE]
125+
# 'no-change' elements are doubly accumulated in models_processed as they exist in SCR and DST.
126+
- 2 * summary["no-change"]
127+
)
118128
return summary
119129

120130
def str(self, indent: int = 0):

diffsync/enum.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@ class DiffSyncActions: # pylint: disable=too-few-public-methods
9595
CREATE = "create"
9696
UPDATE = "update"
9797
DELETE = "delete"
98+
SKIP = "skip"
9899
NO_CHANGE = None

diffsync/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def calculate_diffs(self) -> Diff:
9393
self.diff.add(diff_element)
9494

9595
self.logger.info("Diff calculation complete")
96+
self.diff.models_processed = self.models_processed
9697
self.diff.complete()
9798
return self.diff
9899

examples/06-ip-prefixes/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ From this `diff`, we can check the summary of what would happen.
8282

8383
```py
8484
>>> diff.summary()
85-
{'create': 2, 'update': 1, 'delete': 1, 'no-change': 0}
85+
{'create': 2, 'update': 1, 'delete': 1, 'no-change': 0, 'skip': 0}
8686
```
8787

8888
And, also go into the details. We can see how the `'+'` and + `'-'` represent the actual changes in the target adapter: create, delete or update (when both symbols appear).
@@ -119,5 +119,5 @@ Now, if we reload the IPAM B, and try to check the difference, we should see no
119119
>>> new_ipam_b.load()
120120
>>> diff = ipam_a.diff_to(new_ipam_b)
121121
>>> diff.summary()
122-
{'create': 0, 'update': 0, 'delete': 0, 'no-change': 3}
122+
{'create': 0, 'update': 0, 'delete': 0, 'no-change': 3, 'skip': 0}
123123
```

tests/unit/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ def diff_with_children():
449449
address_element.add_attrs(source={"state": "NC"}, dest={"state": "NC"})
450450
diff.add(address_element)
451451

452+
diff.models_processed = 8
453+
452454
return diff
453455

454456

tests/unit/test_diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_diff_empty():
3434
def test_diff_summary_with_no_diffs():
3535
diff = Diff()
3636

37-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 0}
37+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 0, "skip": 0}
3838

3939

4040
def test_diff_str_with_no_diffs():
@@ -91,7 +91,7 @@ def test_diff_summary_with_diffs(diff_with_children):
9191
# Delete person "Sully"
9292
# Update interface "device1_eth0"
9393
# No change to address "RTP" and device "device1"
94-
assert diff_with_children.summary() == {"create": 1, "update": 1, "delete": 1, "no-change": 2}
94+
assert diff_with_children.summary() == {"create": 1, "update": 1, "delete": 1, "no-change": 2, "skip": 0}
9595

9696

9797
def test_diff_str_with_diffs(diff_with_children):

tests/unit/test_diffsync.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -810,42 +810,42 @@ def test_diffsync_diff_with_skip_unmatched_src_flag(
810810
backend_a, backend_a_with_extra_models, backend_a_minus_some_models
811811
):
812812
diff = backend_a.diff_from(backend_a_with_extra_models)
813-
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23}
813+
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23, "skip": 0}
814814

815815
# SKIP_UNMATCHED_SRC should mean that extra models in the src are not flagged for creation in the dest
816816
diff = backend_a.diff_from(backend_a_with_extra_models, flags=DiffSyncFlags.SKIP_UNMATCHED_SRC)
817-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23}
817+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
818818

819819
# SKIP_UNMATCHED_SRC should NOT mean that extra models in the dst are not flagged for deletion in the src
820820
diff = backend_a.diff_from(backend_a_minus_some_models, flags=DiffSyncFlags.SKIP_UNMATCHED_SRC)
821-
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11}
821+
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11, "skip": 0}
822822

823823

824824
def test_diffsync_diff_with_skip_unmatched_dst_flag(
825825
backend_a, backend_a_with_extra_models, backend_a_minus_some_models
826826
):
827827
diff = backend_a.diff_from(backend_a_minus_some_models)
828-
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11}
828+
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11, "skip": 0}
829829

830830
# SKIP_UNMATCHED_DST should mean that missing models in the src are not flagged for deletion from the dest
831831
diff = backend_a.diff_from(backend_a_minus_some_models, flags=DiffSyncFlags.SKIP_UNMATCHED_DST)
832-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 11}
832+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 11, "skip": 2}
833833

834834
# SKIP_UNMATCHED_DST should NOT mean that extra models in the src are not flagged for creation in the dest
835835
diff = backend_a.diff_from(backend_a_with_extra_models, flags=DiffSyncFlags.SKIP_UNMATCHED_DST)
836-
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23}
836+
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23, "skip": 0}
837837

838838

839839
def test_diffsync_diff_with_skip_unmatched_both_flag(
840840
backend_a, backend_a_with_extra_models, backend_a_minus_some_models
841841
):
842842
# SKIP_UNMATCHED_BOTH should mean that extra models in the src are not flagged for creation in the dest
843843
diff = backend_a.diff_from(backend_a_with_extra_models, flags=DiffSyncFlags.SKIP_UNMATCHED_BOTH)
844-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23}
844+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
845845

846846
# SKIP_UNMATCHED_BOTH should mean that missing models in the src are not flagged for deletion from the dest
847847
diff = backend_a.diff_from(backend_a_minus_some_models, flags=DiffSyncFlags.SKIP_UNMATCHED_BOTH)
848-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 11}
848+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 11, "skip": 2}
849849

850850

851851
def test_diffsync_sync_with_skip_unmatched_src_flag(backend_a, backend_a_with_extra_models):

tests/unit/test_diffsync_model_flags.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424
def test_diffsync_diff_with_skip_unmatched_src_flag_on_models(backend_a, backend_a_with_extra_models):
2525
# Validate that there are 2 extras objects out of the box
2626
diff = backend_a.diff_from(backend_a_with_extra_models)
27-
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23}
27+
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23, "skip": 0}
2828

2929
# Check that only 1 object is affected by the flag
3030
backend_a_with_extra_models.get(
3131
backend_a_with_extra_models.site, "lax"
3232
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
3333
diff = backend_a.diff_from(backend_a_with_extra_models)
34-
assert diff.summary() == {"create": 1, "update": 0, "delete": 0, "no-change": 23}
34+
assert diff.summary() == {"create": 1, "update": 0, "delete": 0, "no-change": 23, "skip": 1}
3535

3636
backend_a_with_extra_models.get(
3737
backend_a_with_extra_models.device, "nyc-spine3"
3838
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
3939
diff = backend_a.diff_from(backend_a_with_extra_models)
40-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23}
40+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
4141

4242

4343
def test_diffsync_sync_with_skip_unmatched_src_flag_on_models(backend_a, backend_a_with_extra_models):
@@ -58,23 +58,23 @@ def test_diffsync_sync_with_skip_unmatched_src_flag_on_models(backend_a, backend
5858
assert "nyc-spine3" not in backend_a.get(backend_a.site, "nyc").devices
5959

6060
diff = backend_a.diff_from(backend_a_with_extra_models)
61-
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23}
61+
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
6262

6363

6464
def test_diffsync_diff_with_skip_unmatched_dst_flag_on_models(backend_a, backend_a_minus_some_models):
6565
# Validate that there are 3 extras objects out of the box
6666
diff = backend_a.diff_from(backend_a_minus_some_models)
67-
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11}
67+
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11, "skip": 0}
6868

6969
# Check that only the device "rdu-spine1" and its 2 interfaces are affected by the flag
7070
backend_a.get(backend_a.device, "rdu-spine1").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
7171
diff = backend_a.diff_from(backend_a_minus_some_models)
72-
assert diff.summary() == {"create": 0, "update": 0, "delete": 9, "no-change": 11}
72+
assert diff.summary() == {"create": 0, "update": 0, "delete": 9, "no-change": 11, "skip": 1}
7373

7474
# Check that only one additional device "sfo-spine2" and its 3 interfaces are affected by the flag
7575
backend_a.get(backend_a.device, "sfo-spine2").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
7676
diff = backend_a.diff_from(backend_a_minus_some_models)
77-
assert diff.summary() == {"create": 0, "update": 0, "delete": 5, "no-change": 11}
77+
assert diff.summary() == {"create": 0, "update": 0, "delete": 5, "no-change": 11, "skip": 2}
7878

7979

8080
def test_diffsync_sync_with_skip_unmatched_dst_flag_on_models(backend_a, backend_a_minus_some_models):

0 commit comments

Comments
 (0)