Beispiel #1
0
        public async Task EarlySavingAudit_Enabled()
        {
            var inserted  = new List <EntityFrameworkEvent>();
            var updated   = new List <EntityFrameworkEvent>();
            var messageId = Guid.NewGuid();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <TestContext>(_ => _.EarlySavingAudit(true));

            Audit.Core.Configuration.Setup()
            .UseDynamicProvider(_ => _
                                .OnInsert(ev => inserted.Add(EntityFrameworkEvent.FromJson(ev.GetEntityFrameworkEvent().ToJson())))
                                .OnReplace((id, ev) => updated.Add(EntityFrameworkEvent.FromJson(ev.GetEntityFrameworkEvent().ToJson()))));

            using (var context = new TestContext())
            {
                Message message = new Message
                {
                    MessageId = messageId
                };
                await context.AddAsync(message);

                await context.SaveChangesAsync();
            }

            Assert.AreEqual(1, inserted.Count);
            Assert.AreEqual(1, updated.Count);
            Assert.AreEqual(0, inserted[0].Result);
            Assert.AreEqual(1, updated[0].Result);
            Assert.AreEqual(1, inserted[0].Entries.Count);
            Assert.AreEqual(1, updated[0].Entries.Count);
        }
        public override async Task <Classes> ExecuteAsync(TestContext context)
        {
            var @class = new Classes()
            {
                Location = _dto.Location,
                Teacher  = _dto.Teacher,
                Name     = _dto.Name
            };

            await context.AddAsync(@class);

            return(@class);
        }
Beispiel #3
0
        public async Task ExceptionInAuditEntity()
        {
            var messageId = Guid.NewGuid();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <TestContext>(_ => _.EarlySavingAudit(true));

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(ef => ef
                                .AuditTypeExplicitMapper(m => m
                                                         .Map <Message, MessageAudit>()
                                                         .AuditEntityAction <MessageAudit>((auditEvent, eventEntry, entity) =>
            {
                entity.AuditData      = eventEntry.ToJson();
                entity.AuditTimestamp = DateTimeOffset.UtcNow;
                entity.AuditAction    = eventEntry.Action;
            })
                                                         )
                                );

            using (var context = new TestContext())
            {
                Message message = new Message
                {
                    MessageId = messageId
                };
                await context.AddAsync(message);

                await context.SaveChangesAsync();
            }

            using (var context = new TestContext())
            {
                Message message = new Message
                {
                    MessageId = messageId
                };
                await context.AddAsync(message);

                Assert.CatchAsync <Exception>(async() => await context.SaveChangesAsync());
            }

            using (var context = new TestContext())
            {
                Assert.AreEqual(1, await context.Messages.CountAsync(e => e.MessageId == messageId));
                Assert.AreEqual(1, await context.MessageAudits.CountAsync(e => e.MessageId == messageId));
            }
        }
Beispiel #4
0
        public async Task ExceptionInAudit()
        {
            const string exceptionMessage = "test";
            var          messageId        = Guid.NewGuid();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <TestContext>(_ => _.EarlySavingAudit());

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(ef => ef
                                .UseDbContext <TestContext>()
                                .AuditTypeExplicitMapper(m => m
                                                         .Map <Message, MessageAudit>()
                                                         .AuditEntityAction <MessageAudit>((auditEvent, eventEntry, entity) =>
            {
                throw new Exception(exceptionMessage);
                return(Task.FromResult(true));
            })
                                                         )
                                );

            using (var context = new TestContext())
            {
                Message message = new Message
                {
                    MessageId = messageId
                };
                await context.AddAsync(message);

                Exception exception = Assert.CatchAsync <Exception>(async() => await context.SaveChangesAsync());
                Assert.AreEqual(exceptionMessage, exception.Message);
            }

            using (var context = new TestContext())
            {
                Assert.AreEqual(0, await context.Messages.CountAsync(e => e.MessageId == messageId));
                Assert.AreEqual(0, await context.MessageAudits.CountAsync(e => e.MessageId == messageId));
            }
        }
        public override async Task <Students> ExecuteAsync(TestContext context)
        {
            var @class = await context.Classes
                         .FindAsync(_classId);

            if (@class == null)
            {
                throw new ArgumentNullException($"Class {@class} is not exist");
            }

            var student = new Students()
            {
                Name    = _dto.Name,
                Dob     = _dto.Dob,
                Surname = _dto.Surname,
                Gpa     = _dto.Gpa,
                Class   = @class
            };

            await context.AddAsync(student);

            return(student);
        }