private static IBatchSyncAgent <int?, Event> CreateSyncAgent() { return(BatchSyncAgent <int?, Event> .Create() .Configure((c) => c.BatchSize = 3) .SetComparerAgent(KeyComparerAgent <int?> .Create()) .SetKeySelector(x => x.Id) .SetCompareItemFunc((s, d) => { if (s.Title == d.Title && s.ModifiedDate == d.ModifiedDate) { return MatchComparisonResultType.Same; } else if (s.ModifiedDate < d.ModifiedDate) { return MatchComparisonResultType.NewerDestination; } else if (s.ModifiedDate > d.ModifiedDate) { return MatchComparisonResultType.NewerSource; } else { return MatchComparisonResultType.Conflict; } })); }
public async Task Sync_Class_TwoWayWithSyncProviders() { var syncAgent = CreateSyncAgent(); DictionaryBatchSyncProvider <int?, Event> source = new DictionaryBatchSyncProvider <int?, Event> { Items = CreateSourceEventDictionary(), KeySelector = syncAgent.KeySelector } , destination = new DictionaryBatchSyncProvider <int?, Event> { Items = CreateDestinationEventDictionary(), KeySelector = syncAgent.KeySelector }; await syncAgent .Configure((c) => c.SyncMode.SyncModePreset = SyncModePreset.TwoWay) .SetComparerAgent(KeyComparerAgent <int?> .Create()) .SetSourceProvider(source) .SetDestinationProvider(destination) .SyncAsync(CancellationToken.None).ConfigureAwait(false); IDictionary <int?, Event> expectedSource = CreateSourceEventDictionary() , expectedDestination = CreateDestinationEventDictionary(); expectedSource[1] = expectedDestination[1]; expectedSource.Add(3, expectedDestination[3]); expectedSource.Add(7, expectedDestination[7]); expectedSource.Add(8, expectedDestination[8]); expectedDestination[4] = expectedSource[4]; expectedDestination.Add(6, expectedSource[6]); source.Items.Should().BeEquivalentTo(expectedSource); destination.Items.Should().BeEquivalentTo(expectedDestination); syncAgent.ToString().Should().Be($"{nameof(syncAgent.Configurations)}: {{{syncAgent.Configurations}}}"); }
public async Task Compare_Guid_NonEmptyLists() { var commonGuid = Guid.NewGuid(); List <Guid> source = new List <Guid> { commonGuid } , destination = new List <Guid> { commonGuid }; for (int i = 0; i < 3; i++) { source.Add(Guid.NewGuid()); destination.Add(Guid.NewGuid()); } var keysComparisonResult = await KeyComparerAgent <Guid> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); keysComparisonResult.KeysInSourceOnly.Count.Should().Be(3); keysComparisonResult.KeysInDestinationOnly.Count.Should().Be(3); keysComparisonResult.KeysInSourceOnly.Should().NotBeEquivalentTo(keysComparisonResult.KeysInDestinationOnly); keysComparisonResult.Matches.Should().BeEquivalentTo(new List <Guid> { commonGuid }); }
public void Sync_Class_NullableKeySelector() { Func <Task> act = async() => await BatchSyncAgent <int?, Event> .Create() .SetComparerAgent(KeyComparerAgent <int?> .Create()) .SetCompareItemFunc((s, d) => MatchComparisonResultType.Conflict) .SyncAsync(CancellationToken.None).ConfigureAwait(false); act.Should().ThrowAsync <NullReferenceException>().WithMessage($"The {nameof(BatchSyncAgent<int?, Event>.KeySelector)} cannot be null."); }
public void Compare_Int_NoDestinationProvider() { List <int> source = new List <int>(); Func <Task> act = async() => await KeyComparerAgent <int> .Create() .SetSourceProvider(source) .CompareAsync(CancellationToken.None).ConfigureAwait(false); act.Should().Throw <NullReferenceException>().WithMessage($"The {nameof(ComparerAgent<int>.DestinationProvider)} cannot be null."); }
public void Sync_Class_DestinationProviderIsNotSet() { Func <Task> act = async() => await BatchSyncAgent <int?, Event> .Create() .SetComparerAgent(KeyComparerAgent <int?> .Create()) .SetKeySelector(x => x.Id) .SetCompareItemFunc((s, d) => MatchComparisonResultType.Conflict) .SetSourceProvider(CreateSourceEventDictionary()) .SyncAsync(CancellationToken.None).ConfigureAwait(false); act.Should().ThrowAsync <NullReferenceException>().WithMessage($"The {nameof(BatchSyncAgent<int?, Event>.DestinationProvider)} cannot be null."); }
public async Task Compare_Int_NullableSourceList() { List <int> source = null , destination = new List <int>(); var comparisonResult = await KeyComparerAgent <int> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); comparisonResult.KeysInSourceOnly.Should().BeEmpty(); comparisonResult.KeysInDestinationOnly.Should().BeEmpty(); comparisonResult.Matches.Should().BeEmpty(); }
public void Compare_Int_NullableKeyInDestination() { List <int?> source = new List <int?>() , destination = new List <int?> { null, 1 }; Func <Task> act = async() => await KeyComparerAgent <int?> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); act.Should().Throw <NullReferenceException>().WithMessage("Null-able keys found in the destination list."); }
public void Compare_Int_DuplicatedKeyInDestination() { List <int?> source = new List <int?> { 1 } , destination = new List <int?> { 3, 2, 2 }; Func <Task> act = async() => await KeyComparerAgent <int?> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); act.Should().Throw <ArgumentException>().WithMessage("Key '2' already exists in the destination list."); }
internal static IBatchSyncAgent <int, Hobby> CreateSyncAgent(IDictionary <int, Hobby> sourceDictionary, IDictionary <int, Hobby> destinationDictionary) { return(BatchSyncAgent <int, Hobby> .Create() .SetComparerAgent(KeyComparerAgent <int> .Create()) .SetKeySelector(x => x.Id) .SetCompareItemFunc((s, d) => { if (s.Name == d.Name) { return MatchComparisonResultType.Same; } else { return MatchComparisonResultType.Conflict; } }) .SetSourceProvider(sourceDictionary) .SetDestinationProvider(destinationDictionary)); }
public async Task Compare_Guid_EquivalentLists() { List <Guid> source = new List <Guid>() , destination = new List <Guid>(); for (int i = 0; i < 3; i++) { var guid = Guid.NewGuid(); source.Add(guid); destination.Add(guid); } var keysComparisonResult = await KeyComparerAgent <Guid> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); keysComparisonResult.KeysInSourceOnly.Count.Should().Be(0); keysComparisonResult.KeysInDestinationOnly.Count.Should().Be(0); keysComparisonResult.Matches.Count.Should().Be(3); }
public async Task Sync_Class_WithExternalComparerAgent(SyncModePreset syncModePreset) { var syncAgent = CreateSyncAgent(); DictionaryBatchSyncProvider <int?, Event> source = new DictionaryBatchSyncProvider <int?, Event> { Items = CreateSourceEventDictionary(), KeySelector = syncAgent.KeySelector } , destination = new DictionaryBatchSyncProvider <int?, Event> { Items = CreateDestinationEventDictionary(), KeySelector = syncAgent.KeySelector }; var keyComparisonResult = await KeyComparerAgent <int?> .Create() .SetSourceProvider(source.Items.Keys) .SetDestinationProvider(destination.Items.Keys) .CompareAsync(CancellationToken.None); // The sync agent should handle changes to source and destination gracefully. if (syncModePreset == SyncModePreset.MirrorToDestination) { await source.DeleteAsync(new List <int?> { 5 }, CancellationToken.None).ConfigureAwait(false); } else if (syncModePreset == SyncModePreset.MirrorToSource) { await destination.DeleteAsync(new List <int?> { 5 }, CancellationToken.None).ConfigureAwait(false); } await syncAgent .Configure((c) => c.SyncMode.SyncModePreset = syncModePreset) .SetComparerAgent(null) .SetSourceProvider((IBatchSyncProvider <int?, Event>)source) .SetDestinationProvider((IBatchSyncProvider <int?, Event>)destination) .SyncAsync(keyComparisonResult, CancellationToken.None).ConfigureAwait(false); source.Items.Should().BeEquivalentTo(destination.Items); }
public async Task Compare_NullableInt_NonEmptyLists() { List <int?> source = new List <int?> { 10, 5, 20 } , destination = new List <int?> { 10, 25, 5 }; var keysComparisonResult = await KeyComparerAgent <int?> .Create() .SetSourceProvider(source) .SetDestinationProvider(destination) .CompareAsync(CancellationToken.None).ConfigureAwait(false); keysComparisonResult.KeysInSourceOnly.Should().BeEquivalentTo(new List <int?> { 20 }); keysComparisonResult.KeysInDestinationOnly.Should().BeEquivalentTo(new List <int?> { 25 }); keysComparisonResult.Matches.Should().BeEquivalentTo(new List <int?> { 5, 10 }); }