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() }); }
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) }); }
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 }); }
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() }); }