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}}}");
        }
Ejemplo n.º 2
0
        public void DictionaryWithNullableItemsShouldThrowExpression()
        {
            var         provider = new DictionaryBatchSyncProvider <int, int>();
            Func <Task> act      = async() => await provider.AddAsync(new List <int> {
            }, CancellationToken.None);

            act.Should().Throw <NullReferenceException>().WithMessage($"The {nameof(provider.Items)} cannot be null.");
        }
Ejemplo n.º 3
0
        public void DictionaryBatchSyncProviderShouldHaveAValidStringForNonNullableItems()
        {
            var provider = new DictionaryBatchSyncProvider <int, int>();

            provider.Items = new Dictionary <int, int>();
            provider.Items.Add(1, 1);

            provider.ToString().Should().Be(provider.Items.ToString());
        }
        public void Sync_Class_DestinationComparerBatchSyncProviderIsSetWithoutSettingComparerAgent()
        {
            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
            };

            Func <Task> act = async() => await syncAgent
                              .Configure((c) => c.SyncMode.SyncModePreset = SyncModePreset.TwoWay)
                              .SetComparerAgent(null)
                              .SetDestinationProvider(destination)
                              .SetSourceProvider(source)
                              .SyncAsync(CancellationToken.None).ConfigureAwait(false);

            act.Should().ThrowAsync <NullReferenceException>().WithMessage($"The {nameof(BatchSyncAgent<int?, Event>.ComparerAgent)} must be set first.");
        }
        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);
        }
        /// <summary>
        /// Sets the destination items.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TItem">The type of the item.</typeparam>
        /// <param name="syncAgent">The sync agent.</param>
        /// <param name="items">The destination items.</param>
        /// <returns></returns>
        public static IBatchSyncAgent <TKey, TItem> SetDestinationProvider <TKey, TItem>(this IBatchSyncAgent <TKey, TItem> syncAgent, IDictionary <TKey, TItem> items)
        {
            if (items == null)
            {
                throw new NullReferenceException($"The destination {nameof(items)} cannot be null.");
            }
            if (syncAgent.ComparerAgent == null)
            {
                throw new NullReferenceException($"The {nameof(syncAgent.ComparerAgent)} must be set first.");
            }
            if (syncAgent.KeySelector == null)
            {
                throw new NullReferenceException($"The {nameof(syncAgent.KeySelector)} must be set first.");
            }

            var syncProvider = new DictionaryBatchSyncProvider <TKey, TItem> {
                Items = items, KeySelector = syncAgent.KeySelector
            };

            syncAgent.ComparerAgent.SetDestinationProvider(syncProvider);
            syncAgent.DestinationProvider = syncProvider;

            return(syncAgent);
        }
Ejemplo n.º 7
0
        public void DictionaryBatchSyncProviderShouldHaveAValidStringForNullableItems()
        {
            var provider = new DictionaryBatchSyncProvider <int, int>();

            provider.ToString().Should().NotBeNullOrWhiteSpace();
        }