Beispiel #1
0
        public void Test_General()
        {
            var guid = Guid.NewGuid().ToString();
            var evs  = new List <AuditEvent>();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsEntities>(x => x.
                                        IncludeEntityObjects(true));
            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(x => x
                                .OnInsertAndReplace(ev =>
            {
                evs.Add(ev);
            }));
            using (var ctx = new BlogsEntities())
            {
                var post = new Post()
                {
                    Id          = 1,
                    DateCreated = DateTime.Now,
                    Content     = "test-content",
                    BlogId      = 1
                };
                ctx.Posts.Add(post);
                ctx.SaveChanges();

                var postProxy = ctx.Posts.First();
                postProxy.Content = guid;
                ctx.SaveChanges();
            }

            Assert.AreEqual(2, evs.Count);

            Assert.AreEqual("Posts", evs[0].GetEntityFrameworkEvent().Entries[0].Table);
            Assert.AreEqual("Posts", evs[1].GetEntityFrameworkEvent().Entries[0].Table);

            var p1 = evs[0].GetEntityFrameworkEvent().Entries[0].Entity as Post;
            var p2 = evs[1].GetEntityFrameworkEvent().Entries[0].Entity as Post;

            Assert.AreEqual("test-content", p1.Content);
            Assert.AreEqual(guid, p2.Content);
            Assert.IsTrue(evs[0].Environment.CallingMethodName.Contains(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name));
        }
Beispiel #2
0
        public void Test_FunctionMapping()
        {
            AuditEventEntityFramework auditEvent = null;

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(x => x.OnInsertAndReplace(ev =>
            {
                auditEvent = ev as AuditEventEntityFramework;
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsEntities>(config => config
                                        .IncludeEntityObjects(true)
                                        .AuditEventType("{context}:{database}"))
            .Reset()
            .UseOptOut();

            var title = Guid.NewGuid().ToString();

            using (var ctx = new BlogsEntities())
            {
                var blog = new Blog()
                {
                    Title       = title,
                    BloggerName = "test"
                };
                ctx.Blogs.Add(blog);
                // this will execute via SP
                ctx.SaveChanges();
            }

            Assert.AreEqual(1, auditEvent.EntityFrameworkEvent.Entries.Count);
            // PK is zero because the insertion via SP
            Assert.IsTrue((int)(auditEvent.EntityFrameworkEvent.Entries[0].PrimaryKey["Id"]) == 0);
            Assert.IsTrue((int)(auditEvent.EntityFrameworkEvent.Entries[0].ColumnValues["Id"]) == 0);
            Assert.AreEqual("Insert", auditEvent.EntityFrameworkEvent.Entries[0].Action);
            Assert.AreEqual("Blogs", auditEvent.EntityFrameworkEvent.Entries[0].Table);
            Assert.AreEqual(title, auditEvent.EntityFrameworkEvent.Entries[0].ColumnValues["Title"]);
            Assert.IsTrue(auditEvent.Environment.CallingMethodName.Contains(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name));
        }
Beispiel #3
0
        public void Test_Delete_Ignored()
        {
            bool neverTrue = false;

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(x => x.OnInsertAndReplace(ev =>
            {
                neverTrue = true;
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd)
            .WithAction(x => x.OnScopeCreated(sc =>
            {
                var efEvent = sc.Event.GetEntityFrameworkEvent();
            }));

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsEntities>(config => config
                                        .IncludeEntityObjects(false)
                                        .AuditEventType("{context}:{database}"))
            .UseOptOut()
            .IgnoreAny(x =>
            {
                Assert.AreEqual("Post", x.Name);
                return(x.Name == "Post");
            });

            using (var ctx = new BlogsEntities())
            {
                var post = new Post()
                {
                    DateCreated = DateTime.Now,
                    Content     = "test-content-x",
                    BlogId      = 1
                };
                ctx.Posts.Add(post);
                ctx.SaveChanges();
            }

            Assert.False(neverTrue);
        }
Beispiel #4
0
        public void Test_EF_IgnoreOverride_CheckCrossContexts()
        {
            var list = new List <AuditEventEntityFramework>();

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(x => x.OnInsertAndReplace(ev =>
            {
                list.Add(ev as AuditEventEntityFramework);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsEntities>(config => config
                                        .ForEntity <Blog>(_ => _.Ignore(blog => blog.BloggerName)));
            Audit.EntityFramework.Configuration.Setup()
            .ForContext <DataBaseContext>(config => config
                                          .ForEntity <Blog>(_ => _.Override <string>("Title", null)));

            var title = Guid.NewGuid().ToString();

            using (var ctx = new BlogsEntities())
            {
                var blog = new Blog()
                {
                    Title       = title,
                    BloggerName = "test"
                };
                ctx.Blogs.Add(blog);
                ctx.SaveChanges();
            }

            Assert.AreEqual(1, list.Count);
            var entries = list[0].EntityFrameworkEvent.Entries;

            Assert.AreEqual(1, entries.Count);
            Assert.AreEqual("Insert", entries[0].Action);
            Assert.IsFalse(entries[0].ColumnValues.ContainsKey("BloggerName"));
            Assert.AreEqual(title, entries[0].ColumnValues["Title"]);
        }