public void CanRaiseDeleteEvent()
        {
            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <NormalModel>();

                bool eventRaised = false;

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

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

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

                context.NormalModels.Remove(existingEntity);
                context.SaveChanges();

                //assert
                Assert.IsTrue(eventRaised);

                existingEntity.AssertAuditForDeletion(context, existingEntity.Id, null,
                                                      x => x.Description,
                                                      x => x.Id);
            }
        }
        public void CanSkipTrackingUsingEvent()
        {
            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <TrackedModelWithMultipleProperties>();

                bool eventRaised = false;

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

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

                var entity = ObjectFactory.Create <TrackedModelWithMultipleProperties>(save: true, testDbContext: context);

                //assert
                Assert.IsTrue(eventRaised);

                //make sure log is saved in database
                entity.AssertNoLogs(context, entity.Id, EventType.Added);
            }
        }
Example #3
0
        public DisconnectedContextTests()
        {
            var builder = new DbContextOptionsBuilder <TestTrackerContext>();

            builder.UseInMemoryDatabase("TestTrackerContext");
            options       = builder.Options;
            Db            = new TestTrackerContext(options);
            ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerContext>(Db);
        }
        public TrackerIdentityContextIntegrationTests()
        {
            optionsBuilder = new DbContextOptionsBuilder <TestTrackerIdentityContext>();
            optionsBuilder.UseInMemoryDatabase("TestTrackerContext");
            var options = optionsBuilder.Options;

            Db            = new TestTrackerIdentityContext(options);
            ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
        }
        public SoftDeleteTests()
        {
            var optionsBuilder = new DbContextOptionsBuilder <TestTrackerContext>();

            optionsBuilder.UseInMemoryDatabase("TestTrackerContext");
            var options = optionsBuilder.Options;

            Db            = new TestTrackerContext(options);
            ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerContext>(Db);
        }
        public void CanChangeEntityInEvent()
        {
            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <TrackedModelWithMultipleProperties>();

                bool eventRaised = false;

                string modifiedValue = RandomText;

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

                    if (args.Log.EventType == EventType.Modified &&
                        args.Log.TypeFullName == typeof(TrackedModelWithMultipleProperties).FullName &&
                        eventEntity != null)
                    {
                        eventEntity.Name = modifiedValue;
                        eventRaised      = true;
                    }
                };

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

                string originalValue = existingEntity.Name;
                string newValue      = RandomText;
                existingEntity.Name = newValue;

                context.SaveChanges();

                //assert
                Assert.IsTrue(eventRaised);

                existingEntity.AssertAuditForModification(context, existingEntity.Id, null,
                                                          new AuditLogDetail
                {
                    PropertyName  = nameof(existingEntity.Name),
                    OriginalValue = originalValue,
                    NewValue      = newValue
                });

                context.Entry(existingEntity).Reload();
                Assert.AreEqual(existingEntity.Name, modifiedValue);
            }
        }
        public void CanRaiseUnDeleteEvent()
        {
            GlobalTrackingConfig.SetSoftDeletableCriteria <ISoftDeletable>
                (x => x.IsDeleted);

            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <SoftDeletableModel>();

                bool eventRaised = false;

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

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

                var 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 void CanRaiseModifyEvent()
        {
            //TODO: modify test tracker context and identity test tracker context so that on disposal they revert the changes
            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <TrackedModelWithMultipleProperties>();

                bool modifyEventRaised = false;

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

                    if (args.Log.EventType == EventType.Modified &&
                        args.Log.TypeFullName == typeof(TrackedModelWithMultipleProperties).FullName &&
                        eventEntity != null)
                    {
                        modifyEventRaised = true;
                    }
                };

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

                string originalValue = existingEntity.Name;
                existingEntity.Name = RandomText;

                context.SaveChanges();

                //assert
                Assert.IsTrue(modifyEventRaised);

                existingEntity.AssertAuditForModification(context, existingEntity.Id, null,
                                                          new AuditLogDetail
                {
                    PropertyName  = nameof(existingEntity.Name),
                    OriginalValue = originalValue,
                    NewValue      = existingEntity.Name
                });
            }
        }
        public void CanRaiseAddEvent()
        {
            using (var context = GetNewContextInstance())
            {
                ObjectFactory = new Common.Testing.Code.ObjectFactory <TestTrackerIdentityContext>(Db);
                EntityTracker.TrackAllProperties <TrackedModelWithMultipleProperties>();

                bool eventRaised = false;

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

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

                var entity = ObjectFactory.Create <TrackedModelWithMultipleProperties>(false);

                entity.Description = RandomText;

                context.TrackedModelsWithMultipleProperties.Add(entity);

                context.SaveChanges();

                //assert
                Assert.IsTrue(eventRaised);

                //make sure log is saved in database
                entity.AssertAuditForAddition(context, entity.Id, null,
                                              x => x.Id,
                                              x => x.Description);
            }
        }