public void ShouldCreateSoftDeleteLogForMultiplePropertiesChanged()
        {
            //create a softdeletable entity and soft delete it
            var deletable = new SoftDeletableModel();

            Db.SoftDeletableModels.Add(deletable);

            //save it to database
            Db.SaveChanges();

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

            //soft delete entity
            deletable.Delete();
            deletable.Description = RandomText;

            //save changes
            Db.SaveChanges();

            //assert for soft deletion
            deletable.AssertAuditForSoftDeletion(Db, deletable.Id, null, new AuditLogDetail
            {
                NewValue      = true.ToString(),
                OriginalValue = false.ToString(),
                PropertyName  = nameof(deletable.IsDeleted)
            },
                                                 new AuditLogDetail
            {
                NewValue      = deletable.Description,
                OriginalValue = null,
                PropertyName  = nameof(deletable.Description)
            });
        }
Example #2
0
        public void Should_Be_Soft_Deleted()
        {
            GlobalTrackingConfig.SetSoftDeletableCriteria <ISoftDeletable>(x => x.IsDeleted);

            SoftDeletableModel entity = ObjectFactory.Create <SoftDeletableModel>(save: true);

            SoftDeletableModel newEntity = new SoftDeletableModel
            {
                Id          = entity.Id,
                Description = entity.Description
            };

            TestTrackerContext newContext2 = GetNewContextInstance(options);

            newEntity.Delete();
            newContext2.Entry(newEntity).State = EntityState.Modified;
            newContext2.SaveChanges();

            newEntity.AssertAuditForSoftDeletion(newContext2, newEntity.Id, null,
                                                 new AuditLogDetail
            {
                PropertyName  = nameof(entity.IsDeleted),
                OriginalValue = false.ToString(),
                NewValue      = true.ToString()
            });
        }
Example #3
0
        public void ShouldCreateSoftDeleteLog2()
        {
            var options = new DbContextOptionsBuilder <TestTrackerContext>()
                          .UseSqlServer(TestConnectionString)
                          .Options;

            //create a softdeletable entity and soft delete it
            var deletable = new SoftDeletableModel();

            using (TestTrackerContext ttc = new TestTrackerContext(options))
            {
                ttc.SoftDeletableModels.Add(deletable); //add
                ttc.SaveChanges();                      //add

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

                ttc.SoftDeletableModels.Remove(deletable); //soft delete
                ttc.SaveChanges();                         //soft delete

                //assert for soft deletion
                deletable.AssertAuditForSoftDeletion(ttc, deletable.Id, null, new AuditLogDetail
                {
                    NewValue      = true.ToString(),
                    OriginalValue = false.ToString(),
                    PropertyName  = nameof(deletable.IsDeleted)
                });
            }
        }
Example #4
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
                });
            }
        }
        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()
                });
            }
        }
        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
            });
        }
Example #7
0
        public void ShouldCreateSoftDeleteLogForMultiplePropertiesChanged()
        {
            var options = new DbContextOptionsBuilder <TestTrackerContext>()
                          .UseSqlServer(TestConnectionString)
                          .Options;

            //create a softdeletable entity and soft delete it
            var deletable = new SoftDeletableModel();

            using (TestTrackerContext ttc = new TestTrackerContext(options))
            {
                ttc.SoftDeletableModels.Add(deletable);

                //save it to database
                ttc.SaveChanges();

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

                //soft delete entity
                deletable.IsDeleted   = true;
                deletable.Description = rdg.Get <string>();

                //save changes
                ttc.SaveChanges();

                //assert for soft deletion
                deletable.AssertAuditForSoftDeletion(ttc, deletable.Id, null, new AuditLogDetail
                {
                    NewValue      = true.ToString(),
                    OriginalValue = false.ToString(),
                    PropertyName  = nameof(deletable.IsDeleted)
                },
                                                     new AuditLogDetail
                {
                    NewValue      = deletable.Description,
                    OriginalValue = null,
                    PropertyName  = nameof(deletable.Description)
                });
            }
        }
        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()
            });
        }