Esempio n. 1
0
        public void HookedDbContext_MustCallHooks_IfModelIsInvalidButUnchanged()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPreInsertHook());
            var tsEntity  = new TimestampedSoftDeletedEntity();
            var valEntity = new ValidatedEntity();

            context.Entities.Add(tsEntity);
            context.Entry(valEntity).State = EntityState.Unchanged;

            Assert.DoesNotThrow(() => context.SaveChanges());

            Assert.AreEqual(tsEntity.CreatedAt.Date, DateTime.Today);
        }
Esempio n. 2
0
        public void HookedDbContext_ShouldNotHook_IfAnyChangedObjectsAreInvalid()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPreInsertHook());
            var tsEntity  = new TimestampedSoftDeletedEntity();
            var valEntity = new ValidatedEntity();

            context.Entities.Add(tsEntity);
            context.ValidatedEntities.Add(valEntity);

            Assert.Throws <DbEntityValidationException>(() => context.SaveChanges());

            Assert.AreNotEqual(tsEntity.CreatedAt.Date, DateTime.Today);
        }
Esempio n. 3
0
        public void HookedDbContext_MustNotCallHooks_IfModelIsInvalid()
        {
            var hooks = new IHook[]
            {
                new TimestampPreInsertHook()
            };

            var context         = new LocalContext(hooks);
            var validatedEntity = new ValidatedEntity();

            context.ValidatedEntities.Add(validatedEntity);

            try
            { context.SaveChanges(); }
            catch { }

            Assert.AreNotEqual(validatedEntity.CreatedAt.Date, DateTime.Today);
        }
Esempio n. 4
0
        public void HookedDbContext_ShouldHook_IfValidateBeforeSaveIsDisabled_AndChangedObjectsAreInvalid()
        {
            var context = new LocalContext();

            context.Configuration.ValidateOnSaveEnabled = false;
            context.RegisterHook(new TimestampPreInsertHook());
            var tsEntity  = new TimestampedSoftDeletedEntity();
            var valEntity = new ValidatedEntity();

            context.Entities.Add(tsEntity);
            context.ValidatedEntities.Add(valEntity);

            Assert.IsTrue(context.GetValidationErrors().Any(x => !x.IsValid));

            Assert.Throws <DbUpdateException>(() => context.SaveChanges());

            Assert.AreEqual(tsEntity.CreatedAt.Date, DateTime.Today);
            Assert.AreEqual(valEntity.CreatedAt.Date, DateTime.Today);
        }
Esempio n. 5
0
        public void HookedDbContext_ShouldNotPostHook_IfExceptionIsHit()
        {
            var runCheckingHook = new RunCheckedPreInsertHook();
            var hooks           = new IHook[]
            {
                runCheckingHook,
                new TimestampPostInsertHook()
            };

            var context = new LocalContext(hooks);

            var valEntity = new ValidatedEntity();

            valEntity.CreatedAt = DateTime.Now;
            context.ValidatedEntities.Add(valEntity);

            Assert.Throws <DbEntityValidationException>(() => context.SaveChanges());

            Assert.IsFalse(runCheckingHook.HasRun);
            Assert.IsFalse(valEntity.ModifiedAt.HasValue);
        }