Skip to content

Commit c91f2c7

Browse files
authored
Feature/dakonr dx sync to (#188)
* return diff to improve dx * added tests for sync_to and sync_from methods * run black on init * moved pylint: disable=too-many-arguments comment
1 parent f6afd05 commit c91f2c7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

diffsync/__init__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,14 @@ def load_from_dict(self, data: Dict):
526526
# Synchronization between DiffSync instances
527527
# ------------------------------------------------------------------------------
528528

529-
def sync_from(
529+
def sync_from( # pylint: disable=too-many-arguments
530530
self,
531531
source: "DiffSync",
532532
diff_class: Type[Diff] = Diff,
533533
flags: DiffSyncFlags = DiffSyncFlags.NONE,
534534
callback: Optional[Callable[[Text, int, int], None]] = None,
535535
diff: Optional[Diff] = None,
536-
): # pylint: disable=too-many-arguments:
536+
) -> Diff:
537537
"""Synchronize data from the given source DiffSync object into the current DiffSync object.
538538
539539
Args:
@@ -543,6 +543,10 @@ def sync_from(
543543
callback (function): Function with parameters (stage, current, total), to be called at intervals as the
544544
calculation of the diff and subsequent sync proceed.
545545
diff (Diff): An existing diff to be used rather than generating a completely new diff.
546+
Returns:
547+
Diff: Diff between origin object and source
548+
Raises:
549+
DiffClassMismatch: The provided diff's class does not match the diff_class
546550
"""
547551
if diff_class and diff:
548552
if not isinstance(diff, diff_class):
@@ -558,14 +562,16 @@ def sync_from(
558562
if result:
559563
self.sync_complete(source, diff, flags, syncer.base_logger)
560564

561-
def sync_to(
565+
return diff
566+
567+
def sync_to( # pylint: disable=too-many-arguments
562568
self,
563569
target: "DiffSync",
564570
diff_class: Type[Diff] = Diff,
565571
flags: DiffSyncFlags = DiffSyncFlags.NONE,
566572
callback: Optional[Callable[[Text, int, int], None]] = None,
567573
diff: Optional[Diff] = None,
568-
): # pylint: disable=too-many-arguments
574+
) -> Diff:
569575
"""Synchronize data from the current DiffSync object into the given target DiffSync object.
570576
571577
Args:
@@ -575,8 +581,12 @@ def sync_to(
575581
callback (function): Function with parameters (stage, current, total), to be called at intervals as the
576582
calculation of the diff and subsequent sync proceed.
577583
diff (Diff): An existing diff that will be used when determining what needs to be synced.
584+
Returns:
585+
Diff: Diff between origin object and target
586+
Raises:
587+
DiffClassMismatch: The provided diff's class does not match the diff_class
578588
"""
579-
target.sync_from(self, diff_class=diff_class, flags=flags, callback=callback, diff=diff)
589+
return target.sync_from(self, diff_class=diff_class, flags=flags, callback=callback, diff=diff)
580590

581591
def sync_complete(
582592
self,

tests/unit/test_diffsync.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def test_diffsync_diff_self_with_no_data_has_no_diffs(generic_diffsync):
4242
def test_diffsync_sync_self_with_no_data_is_noop(generic_diffsync):
4343
generic_diffsync.sync_complete = mock.Mock()
4444
generic_diffsync.sync_from(generic_diffsync)
45-
generic_diffsync.sync_to(generic_diffsync)
45+
diff = generic_diffsync.sync_to(generic_diffsync)
46+
# Check if the returning Diff object has diffs
47+
assert not diff.has_diffs()
4648
# sync_complete() should only be called if something actually changed
4749
assert not generic_diffsync.sync_complete.called
4850

@@ -588,12 +590,13 @@ def test_diffsync_sync_to_w_diff(backend_a, backend_b):
588590
backend_a.diff_from = mock.Mock()
589591
backend_a.diff_to = mock.Mock()
590592
# Perform full sync
591-
backend_b.sync_to(backend_a, diff=diff)
593+
result_diff = backend_b.sync_to(backend_a, diff=diff)
592594
# Assert none of the diff methods have been called
593595
assert not backend_b.diff_from.called
594596
assert not backend_b.diff_to.called
595597
assert not backend_a.diff_from.called
596598
assert not backend_a.diff_to.called
599+
assert result_diff.has_diffs()
597600

598601

599602
def test_diffsync_sync_from_w_diff(backend_a, backend_b):

0 commit comments

Comments
 (0)