Ejemplo n.º 1
0
        public async Task Test_EF_CustomFields_OnScopeCreated()
        {
            var evs = new List <AuditEventEntityFramework>();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(false));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseDynamicProvider(x => x
                                .OnInsertAndReplace(ev =>
            {
                // Do nothing, will include the events on OnScopeCreated
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd)
            .WithAction(_ => _.OnScopeCreated(scope =>
            {
                evs.Add(AuditEvent.FromJson <AuditEventEntityFramework>(scope.Event.ToJson()));
            }));

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                var user = new User()
                {
                    Id       = id,
                    Name     = "fede",
                    Password = "******",
                    Token    = "Test123"
                };
                ctx.Users.Add(user);
                ctx.AddAuditCustomField("TestCustomField", 1L);
                await ctx.SaveChangesAsync();

                ctx.Users.Remove(user);
                ctx.AddAuditCustomField("TestCustomField", 2L);
                await ctx.SaveChangesAsync();
            }

            Assert.AreEqual(2, evs.Count);
            Assert.IsTrue(evs[0].CustomFields.ContainsKey("TestCustomField"));
            Assert.AreEqual(1L, evs[0].CustomFields["TestCustomField"]);
            Assert.AreEqual(2L, evs[1].CustomFields["TestCustomField"]);
        }
Ejemplo n.º 2
0
        public async Task Test_EF_SaveChangesAsyncAcceptChangesOverride()
        {
            var evs = new List <AuditEvent>();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(true));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseDynamicProvider(x => x
                                .OnInsertAndReplace(ev =>
            {
                evs.Add(ev);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                var user = new User()
                {
                    Id       = id,
                    Name     = "fede",
                    Password = "******",
                    Token    = "aaabbb"
                };
                ctx.Users.Add(user);
                await ctx.SaveChangesAsync(true);

                ctx.Users.Remove(user);
                await ctx.SaveChangesAsync();
            }

            Assert.AreEqual(2, evs.Count);
        }
Ejemplo n.º 3
0
        public async Task Test_EF_Provider_ExplicitMapper_MapExplicit_Async()
        {
            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(false)
                                       .ExcludeTransactionId(true));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseEntityFramework(ef => ef
                                .AuditTypeExplicitMapper(config => config
                                                         .MapExplicit <UserAudit>(ee => ee.Table == "User", (ee, userAudit) =>
            {
                userAudit.AuditId   = 1;
                userAudit.UserId    = (int)ee.ColumnValues["Id"];
                userAudit.AuditUser = "******";
            })
                                                         .Map((User user, UserAudit userAudit) =>
            {
                // Should never get here, since the user table is handled explicitly
                userAudit.Action = "Invalid";
            })
                                                         .AuditEntityAction((ee, ent, obj) =>
            {
                ((UserAudit)obj).Action     = ent.Action;
                ((UserAudit)obj).AuditUser += "er";
            }))
                                .IgnoreMatchedProperties(false));

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_user_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                await ctx.Database.EnsureDeletedAsync();

                await ctx.Database.EnsureCreatedAsync();

                var user = new User()
                {
                    Id       = id,
                    Name     = "test",
                    Password = "******",
                    Token    = "token"
                };
                ctx.Users.Add(user);
                await ctx.SaveChangesAsync();
            }

            // Assert
            using (var ctx = new BlogsMemoryContext(options))
            {
                var audit = ctx.UserAudits.Single(u => u.UserId == id);
                Assert.AreEqual(1, audit.AuditId);
                Assert.AreEqual("Insert", audit.Action);
                Assert.AreEqual("user", audit.AuditUser);
                Assert.AreEqual("test", audit.Name);
                Assert.AreEqual(id, audit.UserId);
            }
        }