コード例 #1
0
        public async Task ShouldCreateUnDeletedLogForMultiplePropertiesChanged()
        {
            var options = new DbContextOptionsBuilder <TestTrackerContext>()
                          .UseSqlServer(TestConnectionString)
                          .Options;

            string oldDescription = rdg.Get <string>();
            string newDescription = rdg.Get <string>();

            var deletable = new SoftDeletableModel
            {
                Description = oldDescription
            };

            using (TestTrackerContext ttc = new TestTrackerContext(options))
            {
                ttc.Set <SoftDeletableModel>().Attach(deletable);
                ttc.Entry(deletable).State = EntityState.Added;
                await ttc.SaveChangesAsync();

                deletable.AssertAuditForAddition(ttc, deletable.Id, null, x => x.Id, x => x.Description);

                deletable.IsDeleted = true;
                await ttc.SaveChangesAsync();

                deletable.AssertAuditForSoftDeletion(ttc, deletable.Id, null,
                                                     new AuditLogDetail
                {
                    PropertyName  = nameof(deletable.IsDeleted),
                    OriginalValue = false.ToString(),
                    NewValue      = true.ToString()
                });

                deletable.IsDeleted   = false;
                deletable.Description = newDescription;
                await ttc.SaveChangesAsync();

                deletable.AssertAuditForUndeletion(ttc, deletable.Id, null,
                                                   new AuditLogDetail
                {
                    PropertyName  = nameof(deletable.IsDeleted),
                    OriginalValue = true.ToString(),
                    NewValue      = false.ToString()
                },
                                                   new AuditLogDetail
                {
                    PropertyName  = nameof(deletable.Description),
                    OriginalValue = oldDescription,
                    NewValue      = newDescription
                });
            }
        }
コード例 #2
0
        public void CanRaiseUnDeleteEvent()
        {
            GlobalTrackingConfig.SetSoftDeletableCriteria <ISoftDeletable>
                (x => x.IsDeleted);

            using (TestTrackerContext context = GetNewContextInstance())
            {
                EntityTracker.TrackAllProperties <SoftDeletableModel>();

                bool eventRaised = false;

                context.OnAuditLogGenerated += (sender, args) =>
                {
                    SoftDeletableModel eventEntity = args.Entity as SoftDeletableModel;

                    if (args.Log.EventType == EventType.UnDeleted &&
                        args.Log.TypeFullName == typeof(SoftDeletableModel).FullName &&
                        eventEntity != null)
                    {
                        eventRaised = true;
                    }
                };

                SoftDeletableModel existingEntity = ObjectFactory
                                                    .Create <SoftDeletableModel>(save: true, testDbContext: context);

                existingEntity.Delete();

                context.SaveChanges();

                //now undelete
                existingEntity.IsDeleted = false;
                context.SaveChanges();

                //assert
                Assert.IsTrue(eventRaised);

                existingEntity.AssertAuditForUndeletion(context, existingEntity.Id, null,
                                                        new AuditLogDetail
                {
                    PropertyName  = nameof(existingEntity.IsDeleted),
                    OriginalValue = true.ToString(),
                    NewValue      = false.ToString()
                });
            }
        }
コード例 #3
0
        public async Task ShouldCreateUnDeletedLogForMultiplePropertiesChanged()
        {
            string oldDescription = RandomText;
            string newDescription = RandomText;

            var deletable = new SoftDeletableModel
            {
                Description = oldDescription
            };

            Db.Set <SoftDeletableModel>().Attach(deletable);
            Db.Entry(deletable).State = EntityState.Added;
            await Db.SaveChangesAsync();

            deletable.AssertAuditForAddition(Db, deletable.Id, null,
                                             x => x.Id, x => x.Description);

            deletable.Delete();
            await Db.SaveChangesAsync();

            deletable.AssertAuditForSoftDeletion(Db, deletable.Id, null,
                                                 new AuditLogDetail
            {
                PropertyName  = nameof(deletable.IsDeleted),
                OriginalValue = false.ToString(),
                NewValue      = true.ToString()
            });

            deletable.IsDeleted   = false;
            deletable.Description = newDescription;
            await Db.SaveChangesAsync();

            deletable.AssertAuditForUndeletion(Db, deletable.Id, null,
                                               new AuditLogDetail
            {
                PropertyName  = nameof(deletable.IsDeleted),
                OriginalValue = true.ToString(),
                NewValue      = false.ToString()
            },
                                               new AuditLogDetail
            {
                PropertyName  = nameof(deletable.Description),
                OriginalValue = oldDescription,
                NewValue      = newDescription
            });
        }
コード例 #4
0
        public void ShouldCreateUnDeletedLog()
        {
            var deletable = new SoftDeletableModel
            {
                Description = RandomText,
            };

            Db.Set <SoftDeletableModel>().Attach(deletable);
            Db.Entry(deletable).State = EntityState.Added;
            Db.SaveChanges();

            deletable.AssertAuditForAddition(Db, deletable.Id, null,
                                             x => x.Id, x => x.Description);

            deletable.Delete();
            Db.SaveChanges();

            deletable.AssertAuditForSoftDeletion(Db, deletable.Id, null,
                                                 new AuditLogDetail
            {
                PropertyName  = nameof(deletable.IsDeleted),
                OriginalValue = false.ToString(),
                NewValue      = true.ToString()
            });

            deletable.IsDeleted = false;
            Db.SaveChanges();

            deletable.AssertAuditForUndeletion(Db, deletable.Id, null,
                                               new AuditLogDetail
            {
                PropertyName  = nameof(deletable.IsDeleted),
                OriginalValue = true.ToString(),
                NewValue      = false.ToString()
            });
        }