public void t01_verify_if_can_create_db()
 {
     int result;
     using (NoticesContext ctx = new NoticesContext())
     {
         FuneralNotice notice = noticeBuilder.WithId(3);
         
         ctx.FuneralNotices.Add(notice);
         result = ctx.SaveChanges();
     }
     Assert.IsTrue(result == 1);
 }
        public void t05_notice_check_enums_trick()
        {
            List<FuneralNotice> result;
            using (NoticesContext ctx = new NoticesContext())
            {
                FuneralNotice notice = noticeBuilder
                    .WithId(5)
                    .WithSourceType(SourceType.RAINBOW);

                ctx.FuneralNotices.Add(notice);
                ctx.SaveChanges();

                result = ctx.FuneralNotices
                    .Where(x => x.Source == SourceType.RAINBOW)
                    .ToList();
            }
            
            Assert.That(result, Is.Not.Empty);
            Assert.That(result.First().SourceString, Is.EqualTo(SourceType.RAINBOW.ToString()) );
        }
        public void t03_add_notice_and_auditlog()
        {
            AuditFuneralNotice audit;
            using (NoticesContext ctx = new NoticesContext())
            {
                FuneralNotice notice = noticeBuilder.WithId(4);
                ctx.FuneralNotices.Add(notice);

                audit = new AuditFuneralNotice(notice, AuditState.SavedToDb);
                ctx.AuditLogFuneralNotices.Add(audit);
                int result = ctx.SaveChanges();
                
                Assert.IsTrue(result == 2);
            }
            
            //read
            using (NoticesContext ctx = new NoticesContext())
            {
                audit = ctx.AuditLogFuneralNotices.FirstOrDefault(x => x.Id == 1);
            }
            Assert.IsTrue(audit.Id == 1);
        }
        protected virtual void Seed(NoticesContext context)
        {
            var notice = new FuneralNotice(1, "test", "test")
            {
                MemorialId = 1,
                ParentBranchId = "1",
                CedarCode = "cedarCode",
                BranchId = "1",
                KnownAs = "knownAs",
                Obituary = "Obituary",
                Source = SourceType.PERMAVITA,
                DateOfDeath = DateTime.UtcNow,
                DateOfFuneral = DateTime.UtcNow,
            };

            try
            {
                context.FuneralNotices.Add(notice);
                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                StringBuilder builder = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    builder.AppendLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:".Frmt(
                        eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        builder.AppendLine("- Property: \"{0}\", Error: \"{1}\"".Frmt(ve.PropertyName, ve.ErrorMessage));
                    }
                }
                Console.Write(builder.ToString());
                throw;
            }

        }
        public void t07_SeedIndex()
        {
            indexer.ClearAllNoticesInIndex();
            LoggerConfiguration.Inst.NoticesTimeToLeave = new TimeSpan(0,1,0);

            int id = 2;
            using (var ctx = new NoticesContext())
            {
                for (int i = 0; i < 999; i++)
                {
                    FuneralNotice fn = noticeBuilder.WithId(2 + i);
                    fn.OnlineMemorialUrl = string.Empty;
                    ctx.FuneralNotices.Add(fn);
                }
                ctx.SaveChanges();
            }

            
            Assert.That(1, Is.EqualTo(1));

            indexer.ReIndexAll();
            var result = indexer.ReindexedCount;
            Assert.That(result, Is.GreaterThan(2));
        }
        public void t06_Reindex_all()
        {
            var date = DateTime.UtcNow.AddDays(-8); //too old
            FuneralNotice fn1 = noticeBuilder.WithId(5).WithDateOfFunerale(date); //funeral
            fn1.OnlineMemorialUrl = string.Empty;
            FuneralNotice fn2 = noticeBuilder.WithId(6).WithDateOfFunerale(date); //online

            using (var ctx = new NoticesContext())
            {
                ctx.FuneralNotices.Add(fn1);
                ctx.FuneralNotices.Add(fn2);
                ctx.SaveChanges();
            }
            
            indexer.Add(fn1);
            indexer.Add(fn2);
            indexer.ReIndexAll();

            var result = indexer.ReindexedCount;

            Assert.That(result, Is.EqualTo(2));
        }
        public void t06_audits_verify_getLast()
        {
            AuditFuneralNotice result;
            using (NoticesContext ctx = new NoticesContext())
            {
                FuneralNotice notice = noticeBuilder
                    .WithId(6);

                var audit1 = new AuditFuneralNotice(notice, AuditState.Creating);
                var audit2 = new AuditFuneralNotice(notice, AuditState.IndexingFailed);

                ctx.FuneralNotices.Add(notice);
                ctx.AuditLogFuneralNotices.AddRange(new[] { audit1, audit2 });
                ctx.SaveChanges();
            }
            using (NoticesContext ctx = new NoticesContext())
            {
                result = ctx.AuditLogFuneralNotices.GetLast(6);
            }

            Assert.That(result.AuditState, Is.EqualTo(AuditState.IndexingFailed) );
        }