public void Should_PostInsertHook_InterfaceHook_Calls_Into_GenericMethod()
        {
            var hook   = new TimestampPostInsertHook();
            var entity = new TimestampedSoftDeletedEntity();

            ((IHook)hook).Hook(entity, null);
            Assert.AreEqual(DateTimeOffset.UtcNow.Date, entity.CreationDateTime.Date);
        }
示例#2
0
        public void PreUpdateHook_InterfaceHookCallsIntoGenericMethod()
        {
            var hook   = new TimestampPreUpdateHook();
            var entity = new TimestampedSoftDeletedEntity();

            ((IHook)hook).Hook(entity, null);
            Assert.AreEqual(DateTimeOffset.UtcNow.Date, entity.LastModificationDateTime.Value.Date);
        }
        public void Should_PreDeleteHook_Reassign_To_Modified_State()
        {
            var hook     = new SoftDeletePreDeleteHook();
            var metadata = new HookEntityMetadata(EntityState.Deleted);
            var entity   = new TimestampedSoftDeletedEntity();

            hook.Hook(entity, metadata);

            Assert.AreEqual(true, metadata.HasStateChanged);
            Assert.AreEqual(EntityState.Modified, metadata.State);
        }
示例#4
0
        public void HookedDbContext_AfterConstruction_CanRegisterNewHooks()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPreInsertHook());

            var entity = new TimestampedSoftDeletedEntity();

            context.Entities.Add(entity);
            context.SaveChanges();

            Assert.AreEqual(entity.CreatedAt.Date, DateTime.Today);
        }
示例#5
0
        public void HookedDbContext_CanLateBindPostActionHooks()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPostInsertHook());

            var tsEntity = new TimestampedSoftDeletedEntity();

            tsEntity.CreatedAt = DateTime.Now;
            context.Entities.Add(tsEntity);
            context.SaveChanges();

            Assert.AreEqual(DateTime.Today, tsEntity.ModifiedAt.Value.Date);
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
0
        public void HookedDbContext_MustCallHooks_WhenRunningSaveChanges()
        {
            var hooks = new IHook[]
            {
                new TimestampPreInsertHook()
            };

            var context = new LocalContext(hooks);
            var entity  = new TimestampedSoftDeletedEntity();

            context.Entities.Add(entity);
            context.SaveChanges();

            Assert.AreEqual(entity.CreatedAt.Date, DateTime.Today);
        }
示例#9
0
        public void HookedDbContext_MustNotCallHooks_WhenGetValidationErrorsIsCalled()
        {
            var hooks = new IHook[]
            {
                new TimestampPreInsertHook()
            };

            var context = new LocalContext(hooks);
            var entity  = new TimestampedSoftDeletedEntity();

            context.Entities.Add(entity);
            context.GetValidationErrors();

            Assert.AreNotEqual(entity.CreatedAt.Date, DateTime.Today);
        }
示例#10
0
        public void HookedDbContext_MustOnlyHookWhenObjectIsInTheSameState()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPreInsertHook());
            context.RegisterHook(new TimestampPreUpdateHook());

            var tsEntity = new TimestampedSoftDeletedEntity();

            tsEntity.CreatedAt = DateTime.Now;
            context.Entities.Add(tsEntity);
            context.SaveChanges();

            Assert.AreEqual(DateTime.Today, tsEntity.CreatedAt.Date);
            Assert.IsFalse(tsEntity.ModifiedAt.HasValue);
        }
示例#11
0
        public void HookedDbContext_PostActionHookMethod_MustHaveTheContextPassedInTheMetadata()
        {
            var context    = new LocalContext();
            var postAction = A.Fake <PostInsertHook <ITimeStamped> >();

            A.CallTo(() => postAction.HookStates).Returns(EntityState.Added);

            context.RegisterHook(postAction);

            // We aren't testing the hook here
            var entity = new TimestampedSoftDeletedEntity {
                CreatedAt = DateTime.Now
            };

            context.Entities.Add(entity);
            context.SaveChanges();
            A.CallTo(() => postAction.Hook(entity, A <HookEntityMetadata> .That.Matches(m => m.CurrentContext == context))).MustHaveHappened();
        }
示例#12
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);
        }
示例#13
0
        public void HookedDbContext_ShouldPostHook_IfNoExceptionIsHit()
        {
            var runCheckingHook = new RunCheckedPreInsertHook();
            var hooks           = new IHook[]
            {
                runCheckingHook,
                new TimestampPostInsertHook()
            };


            var context = new LocalContext(hooks);

            var tsEntity = new TimestampedSoftDeletedEntity();

            tsEntity.CreatedAt = DateTime.Now;
            context.Entities.Add(tsEntity);
            context.SaveChanges();

            Assert.IsTrue(runCheckingHook.HasRun);
            Assert.AreEqual(DateTime.Today, tsEntity.ModifiedAt.Value.Date);
        }