Exemple #1
0
        public void Main(string[] args)
        {
            using (var myDbContext = new MyDBContext())
            {
                myDbContext.Database.EnsureDeleted();
                myDbContext.Database.EnsureCreated();

                var customer = new Customer()
                {
                    FirstName = "TestFirstName",
                    LastName = "TEstLAstNAme"
                };
                myDbContext.Customers.Add(customer);

                var auditablePropCount =
                    customer.GetType()
                        .GetProperties()
                        .Count(p => !p.GetCustomAttributes(typeof (DoNotAudit), true).Any());

                var nonAuditablePropCount =
                    customer.GetType()
                        .GetProperties()
                        .Count(p => p.GetCustomAttributes(typeof(DoNotAudit), true).Any());
                myDbContext.SaveChanges("Test User");

                customer.LastName = "TestLastName"; // This should throw an exception below
                myDbContext.SaveChanges("Test User");

                Console.WriteLine($"Added object with {auditablePropCount} auditable properties and {nonAuditablePropCount} non-auditable properties." );

                var auditLogs = myDbContext.GetAuditLogs().ToList();
                Console.WriteLine($"Audit log contains {auditLogs.Count()} entries.");
                foreach (var auditLog in myDbContext.GetAuditLogs())
                {
                    Console.WriteLine($"AuditLogId:{auditLog.AuditLogId} TableName:{auditLog.TableName} ColumnName:{auditLog.ColumnName} OriginalValue:{auditLog.OriginalValue} NewValue:{auditLog.NewValue} EventDateTime:{auditLog.EventDateTime}");
                }

                if (auditLogs.Count() == auditablePropCount)
                    Console.WriteLine("Test succeeded.");
                else
                    throw new Exception("Something is wrong.");

                Console.Read();
                myDbContext.Database.EnsureDeleted();
            }
        }
        public void ShouldAddData()
        {
            using (var db = _provider.GetService<TestDbContext>())
            {
                // Arrange
                db.SeedTestData();
                var expectedCount = 5;

                //// Act
                var customer = new Customer { CustomerId = 5, FirstName = "Misty", LastName = "Shock" };
                db.Customers.Add(customer);
                db.SaveChanges(_currentUser);
                var actualCount = db.Customers.Count();

                //// Assert
                Assert.Equal(expectedCount, actualCount);
                Assert.True(true);
            }
        }
        public void ShouldCreateAuditLogs()
        {
            using (var db = _provider.GetService<TestDbContext>())
            {
                // Arrange
                var expectedCount = 1;

                // Act
                var customer = new Customer { CustomerId = 5, FirstName = "Misty", LastName = "Shock" };
                db.Customers.Add(customer);
                db.SaveChanges(_currentUser);
                var auditLogs = db.GetAuditLogs().ToList();
                var first = auditLogs.FirstOrDefault() as CustomAuditLog;

                // Assert
                Assert.Equal(expectedCount, auditLogs.Count);
                Assert.NotNull(first);

                var differences = first.Differences;
                var diff = JsonConvert.DeserializeObject<List<PropertyDiff>>(differences);

                Assert.Equal(diff.Count, 3); // should be 3 fields -> Id, FirstName, LastName
            }
        }