Exemplo n.º 1
0
        public void when_contacts_are_same_person_they_should_be_merged()
        {
            var fixture = new Fixture();

            var contact1 = fixture.Create <Contact>();
            var contact2 = fixture.Create <Contact>();

            var leftList = new[] { contact2 }.ToList();
            var rightList = new[] { contact1 }.ToList();

            var signal = new ManualResetEvent(false);

            MergeOutput <Contact> actual = null;
            int outpuItems = 0;

            var target = new DefaultIdentityBatchMerge(new AlwaysSameIdentity(), new AlwaysMerge())
            {
                ProcessedItem = x => { outpuItems++; actual = x; },
                Finish        = () => signal.Set()
            };

            target.MergeAsync(leftList, rightList);

            signal.WaitOne();

            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.MergeOperation == MergeOperation.Merged);
            Assert.IsTrue(outpuItems == 1);
        }
Exemplo n.º 2
0
        public void when_error_call_error_handler()
        {
            var fixture = new Fixture();

            var leftList  = fixture.CreateMany <Contact>().ToList();
            var rightList = fixture.CreateMany <Contact>().ToList();

            var signal = new ManualResetEvent(false);

            Exception actual = null;

            var faultyMerger = new Mock <IIdentityMerge <Contact> >();

            faultyMerger.Setup(x => x.Merge(It.IsAny <Contact>(), It.IsAny <Contact>())).Throws <ApplicationException>();

            var target = new DefaultIdentityBatchMerge(new AlwaysSameIdentity(), faultyMerger.Object)
            {
                ProcessedItem = x => { },
                Error         = exc => { actual = exc; signal.Set(); }
            };

            target.MergeAsync(leftList, rightList);

            signal.WaitOne();

            Assert.AreEqual(typeof(AggregateException), actual.GetType());
            Assert.IsTrue(((AggregateException)actual).InnerExceptions.All(x => x.GetType() == typeof(ApplicationException)));
        }
Exemplo n.º 3
0
        public void when_left_list_is_empty_all_right_list_should_be_Added()
        {
            var fixture = new Fixture();

            var contact1 = fixture.Create <Contact>();
            var contact2 = fixture.Create <Contact>();

            var emptyList = new List <Contact>();
            var rightList = new[] { contact1, contact2 }.ToList();

            var signal = new ManualResetEvent(false);

            var actual = new List <MergeOutput <Contact> >();

            var target = new DefaultIdentityBatchMerge(new NeverSameIdentity(), It.IsAny <IIdentityMerge <Contact> >())
            {
                Finish        = () => signal.Set(),
                ProcessedItem = x => actual.Add(x)
            };

            target.MergeAsync(emptyList, rightList);

            signal.WaitOne();

            Assert.IsTrue(actual.All(x => x.MergeOperation == MergeOperation.Added));
            Assert.IsTrue(actual.Any(x => x.Result.Print() == contact1.Print()));
            Assert.IsTrue(actual.Any(x => x.Result.Print() == contact2.Print()));
            Assert.IsTrue(actual.Count == 2);
        }
Exemplo n.º 4
0
        public void when_ProcessedItem_is_null_should_throw_error()
        {
            var target = new DefaultIdentityBatchMerge(It.IsAny <IIdentityComparer <Contact> >(), It.IsAny <IIdentityMerge <Contact> >());

            try
            {
                target.Merge(It.IsAny <List <Contact> >(), It.IsAny <List <Contact> >());
                Assert.Fail("exception not thrown");
            }
            catch (ArgumentNullException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception)
            {
                Assert.Fail("bad exception");
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            string file       = @".\Test\Contacts1.json";
            double confidence = DefaultConfidence;

            if (args.Length > 0)
            {
                file = @args[0];
            }
            if (args.Length > 1)
            {
                if (!double.TryParse(args[1], out confidence))
                {
                    confidence = DefaultConfidence;
                }
            }

            ConfigureListener();

            var leftList  = new InMemoryContactsRepository().FindAll().ToList();
            var rightList = Helper.ReadFromFile(file);

            var batchMerge = new DefaultIdentityBatchMerge(new SimpleIdentityComparer(confidence), new SimpleIdentityMerge())
            {
                ProcessedItem = OnProcessedItem,
                Error         = OnError,
                Finish        = OnFinish
            };

            Helper.LogInfo("Working...");
            Helper.LogInfo("-------------------------------");

            batchMerge.MergeAsync(leftList, rightList);

            EndSignal.WaitOne();

            Helper.LogInfo("Press any key to exit...");

            System.Console.Read();
        }