Skip to content

Commit 94dfcac

Browse files
Merge pull request #30 from networktocode/dga-fix-diff
Add support for diff_class in DiffElement
2 parents 9261dc8 + 517f538 commit 94dfcac

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

dsync/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,12 @@ def diff_object_pair(self, src_obj: Optional[DSyncModel], dst_obj: Optional[DSyn
852852
return None
853853

854854
diff_element = DiffElement(
855-
obj_type=model, name=shortname, keys=keys, source_name=self.src_dsync.name, dest_name=self.dst_dsync.name,
855+
obj_type=model,
856+
name=shortname,
857+
keys=keys,
858+
source_name=self.src_dsync.name,
859+
dest_name=self.dst_dsync.name,
860+
diff_class=self.diff_class,
856861
)
857862

858863
if src_obj:

dsync/diff.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717

1818
from functools import total_ordering
19-
from typing import Any, Iterator, Iterable, Mapping, Optional, Text
19+
from typing import Any, Iterator, Iterable, Mapping, Optional, Text, Type
2020

2121
from .exceptions import ObjectAlreadyExists
2222
from .utils import intersection, OrderedDefaultDict
@@ -126,7 +126,13 @@ class DiffElement: # pylint: disable=too-many-instance-attributes
126126
"""DiffElement object, designed to represent a single item/object that may or may not have any diffs."""
127127

128128
def __init__(
129-
self, obj_type: Text, name: Text, keys: Mapping, source_name: Text = "source", dest_name: Text = "dest"
129+
self,
130+
obj_type: Text,
131+
name: Text,
132+
keys: Mapping,
133+
source_name: Text = "source",
134+
dest_name: Text = "dest",
135+
diff_class: Type[Diff] = Diff,
130136
): # pylint: disable=too-many-arguments
131137
"""Instantiate a DiffElement.
132138
@@ -137,6 +143,7 @@ def __init__(
137143
keys: Primary keys and values uniquely describing this object, as in DSyncModel.get_identifiers().
138144
source_name: Name of the source DSync object
139145
dest_name: Name of the destination DSync object
146+
diff_class: Diff or subclass thereof to use to calculate the diffs to use for synchronization
140147
"""
141148
if not isinstance(obj_type, str):
142149
raise ValueError(f"obj_type must be a string (not {type(obj_type)})")
@@ -152,7 +159,7 @@ def __init__(
152159
# Note: *_attrs == None if no target object exists; it'll be an empty dict if it exists but has no _attributes
153160
self.source_attrs: Optional[Mapping] = None
154161
self.dest_attrs: Optional[Mapping] = None
155-
self.child_diff = Diff()
162+
self.child_diff = diff_class()
156163

157164
def __lt__(self, other):
158165
"""Logical ordering of DiffElements.

tests/unit/test_dsync.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ def check_diff_symmetry(diff1, diff2):
271271

272272
def test_dsync_diff_from_with_custom_diff_class(backend_a, backend_b):
273273
diff_ba = backend_a.diff_from(backend_b, diff_class=TrackedDiff)
274+
diff_children = diff_ba.get_children()
275+
274276
assert isinstance(diff_ba, TrackedDiff)
277+
for child in diff_children:
278+
if child.child_diff:
279+
assert isinstance(child.child_diff, TrackedDiff)
275280
assert diff_ba.is_complete is True
276281

277282

0 commit comments

Comments
 (0)