Пример #1
0
        public void Should_Audit_When_EntityState_Modified()
        {
            var options      = DatabaseOptions <FakeDbContext>();
            var fakeEntities = new FakeEntities();

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AddRange(fakeEntities);
                context.SaveChanges();
            }

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AttachRange(fakeEntities);
                fakeEntities.ForEach(entity => entity.EntityName = "Modified");
                context.Audit();

                var auditEntries   = context.GetCurrentEntries();
                var modifyDateTime = (DateTime)auditEntries.First().CurrentValues[nameof(FakeEntity.ModifiedTime)];

                Assert.All(auditEntries, prop => Assert.Equal("Modified", prop.CurrentValues[nameof(FakeEntity.EntityName)]));
                Assert.All(auditEntries, prop => Assert.Equal(Principal.Identity.Name, prop.CurrentValues[nameof(FakeEntity.ModifiedBy)]));
                Assert.All(auditEntries, prop => Assert.Equal(modifyDateTime, (DateTime)prop.CurrentValues[nameof(FakeEntity.ModifiedTime)], TimeSpan.FromMinutes(1)));
            }
        }
Пример #2
0
        public void Should_Audit_When_EntityState_SoftDeleted()
        {
            var options      = DatabaseOptions <FakeDbContext>();
            var fakeEntities = new FakeEntities();

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AddRange(fakeEntities);
                context.SaveChanges();
            }

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.RemoveRange(fakeEntities);
                context.Audit();

                var auditEntries = context.GetCurrentEntries();

                Assert.All(auditEntries, prop => Assert.Equal(Principal.Identity.Name, prop.CurrentValues[nameof(FakeEntity.ModifiedBy)]));
                Assert.All(auditEntries, prop => Assert.True((bool)prop.CurrentValues[nameof(FakeEntity.IsDeleted)]));
                Assert.All(auditEntries, prop => Assert.Equal((DateTime)prop.CurrentValues[nameof(FakeEntity.ModifiedTime)], (DateTime)prop.CurrentValues[nameof(FakeEntity.DeletedTime)], TimeSpan.FromMinutes(1)));
            }
        }
Пример #3
0
        public void Should_Audit_When_EntityState_Added()
        {
            var fakeEntities = new FakeEntities();

            using (IDbContext context = new FakeDbContext(DatabaseOptions <FakeDbContext>()))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AddRange(fakeEntities);
                context.Audit();

                var auditEntries  = context.GetCurrentEntries();
                var entryDateTime = (DateTime)auditEntries.First().CurrentValues[nameof(FakeEntity.CreatedTime)];

                Assert.All(auditEntries, prop => Assert.Equal(Principal.Identity.Name, prop.CurrentValues[nameof(FakeEntity.CreatedBy)]));
                Assert.All(auditEntries, prop => Assert.Equal(entryDateTime, (DateTime)prop.CurrentValues[nameof(FakeEntity.CreatedTime)], TimeSpan.FromMinutes(1)));
                Assert.All(auditEntries, prop => Assert.Null(prop.CurrentValues[nameof(FakeEntity.ModifiedBy)]));
                Assert.All(auditEntries, prop => Assert.Null(prop.CurrentValues[nameof(FakeEntity.ModifiedTime)]));
            }
        }