Esempio n. 1
0
        public void Test_SaveChangesInterceptor_MultiThread(int threads)
        {
            var locker   = new object();
            var inserted = new List <AuditEventEntityFramework>();

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _
                                .OnInsert(ev =>
            {
                lock (locker)
                {
                    inserted.Add(AuditEvent.FromJson <AuditEventEntityFramework>(ev.ToJson()));
                }
            }));

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <InterceptContext>(_ => _
                                           .AuditEventType("{context}**")
                                           .IncludeEntityObjects(true));

            using (var ctx = new InterceptContext(new DbContextOptionsBuilder().Options))
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();
            }

            Action <int> insertAction = (int index) =>
            {
                var guid = Guid.NewGuid().ToString();
                var dept = new InterceptContext.Department()
                {
                    Id = index, Name = guid, Comments = "test"
                };
                using (var ctx = new InterceptContext(new DbContextOptionsBuilder().AddInterceptors(new AuditSaveChangesInterceptor()).Options))
                {
                    ctx.Departments.Add(dept);
                    ctx.SaveChanges();
                }
            };

            var tasks = new List <Task>();

            for (int i = 0; i < threads; i++)
            {
                int a = i;
                tasks.Add(Task.Run(() => insertAction(a)));
            }

            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(threads, inserted.Count);
            Assert.AreEqual("InterceptContext**", inserted[0].EventType);

            Assert.AreEqual(true, inserted[0].EntityFrameworkEvent.Success);
            Assert.AreEqual(1, inserted[0].EntityFrameworkEvent.Entries.Count);
            Assert.AreEqual("Insert", inserted[0].EntityFrameworkEvent.Entries[0].Action);
            Assert.AreEqual(3, inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues.Count);
            Assert.AreEqual("test", inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues["Comments"].ToString());
            Assert.IsTrue(Enumerable.Range(0, threads).All(i => inserted.Any(ev => ev.EntityFrameworkEvent.Entries[0].PrimaryKey["Id"].ToString() == i.ToString())));
        }
Esempio n. 2
0
        public async Task Test_SaveChangesInterceptor_FailureAsync()
        {
            var inserted = new List <AuditEventEntityFramework>();
            var replaced = new List <AuditEventEntityFramework>();

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _
                                .OnInsert(ev => inserted.Add(AuditEvent.FromJson <AuditEventEntityFramework>(ev.ToJson())))
                                .OnReplace((id, ev) => replaced.Add(AuditEvent.FromJson <AuditEventEntityFramework>(ev.ToJson()))));

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <InterceptContext>(_ => _
                                           .AuditEventType("{context}**")
                                           .IncludeEntityObjects(true));

            var dept = new InterceptContext.Department()
            {
                Id = 1, Name = "test", Comments = "test"
            };

            using (var ctx = new InterceptContext(new DbContextOptionsBuilder().Options))
            {
                // not intercepted
                await ctx.Database.EnsureDeletedAsync();

                await ctx.Database.EnsureCreatedAsync();

                await ctx.Departments.AddAsync(dept);

                await ctx.SaveChangesAsync();
            }
            Assert.AreEqual(0, inserted.Count);
            var guid = Guid.NewGuid().ToString();

            dept.Name = guid;
            using (var ctx = new InterceptContext(new DbContextOptionsBuilder().AddInterceptors(new AuditSaveChangesInterceptor()).Options))
            {
                await ctx.Departments.AddAsync(dept);

                try
                {
                    await ctx.SaveChangesAsync();
                }
                catch (ArgumentException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("item with the same key"));
                }
            }

            Assert.AreEqual(1, inserted.Count);
            Assert.AreEqual(0, replaced.Count);
            Assert.AreEqual("InterceptContext**", inserted[0].EventType);
            Assert.AreEqual(false, inserted[0].EntityFrameworkEvent.Success);
            Assert.IsTrue(inserted[0].EntityFrameworkEvent.ErrorMessage.Contains("item with the same key"));
            Assert.AreEqual(1, inserted[0].EntityFrameworkEvent.Entries.Count);
            Assert.IsTrue(inserted[0].EntityFrameworkEvent.Entries[0].Entity.ToString().Contains(guid));
            Assert.AreEqual("Insert", inserted[0].EntityFrameworkEvent.Entries[0].Action);
            Assert.AreEqual(3, inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues.Count);
            Assert.AreEqual(guid, inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues["Name"].ToString());
        }
Esempio n. 3
0
        public async Task Test_SaveChangesInterceptor_HappyPathAsync()
        {
            var inserted = new List <AuditEventEntityFramework>();
            var replaced = new List <AuditEventEntityFramework>();

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _
                                .OnInsert(ev => inserted.Add(AuditEvent.FromJson <AuditEventEntityFramework>(ev.ToJson())))
                                .OnReplace((id, ev) => replaced.Add(AuditEvent.FromJson <AuditEventEntityFramework>(ev.ToJson()))));

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <InterceptContext>(_ => _
                                           .AuditEventType("{context}**")
                                           .ForEntity <InterceptContext.Department>(dc => dc.Override(d => d.Comments, "override"))
                                           .IncludeEntityObjects(true));

            var guid = Guid.NewGuid().ToString();
            var dept = new InterceptContext.Department()
            {
                Name = guid, Comments = "test"
            };

            using (var ctx = new InterceptContext(new DbContextOptionsBuilder().AddInterceptors(new AuditSaveChangesInterceptor()).Options))
            {
                await ctx.Database.EnsureDeletedAsync();

                await ctx.Database.EnsureCreatedAsync();

                await ctx.Departments.AddAsync(dept);

                await ctx.SaveChangesAsync();
            }

            Assert.AreEqual(1, inserted.Count);
            Assert.AreEqual(0, replaced.Count);
            Assert.AreEqual("InterceptContext**", inserted[0].EventType);

            Assert.AreEqual(true, inserted[0].EntityFrameworkEvent.Success);
            Assert.AreEqual(1, inserted[0].EntityFrameworkEvent.Entries.Count);
            Assert.IsTrue(inserted[0].EntityFrameworkEvent.Entries[0].Entity.ToString().Contains(guid));
            Assert.AreEqual("Insert", inserted[0].EntityFrameworkEvent.Entries[0].Action);
            Assert.AreEqual(3, inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues.Count);
            Assert.AreEqual(guid, inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues["Name"].ToString());
            Assert.AreEqual("override", inserted[0].EntityFrameworkEvent.Entries[0].ColumnValues["Comments"].ToString());
        }