public void IgnoreRelationshipAdded_IsFalse()
        {
            TestContext.DeleteAll(x => x.AuditEntryProperties);
            TestContext.DeleteAll(x => x.AuditEntries);
            TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);
            TestContext.DeleteAll(x => x.Association_OneToMany_Rights);

            var audit = AuditHelper.AutoSaveAudit();
            Assert.AreEqual(false, audit.Configuration.IgnoreRelationshipAdded);

            using (var ctx = new TestContext())
            {
                var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 2).First();

                var right = new Association_OneToMany_Right {ColumnInt = 0};

                left.Rights = new List<Association_OneToMany_Right> {right};
                ctx.SaveChanges();
            }

            using (var ctx = new TestContext())
            {
                var list = ctx.Association_OneToMany_Lefts.OrderBy(x => x.ID).ToList();
                var right = ctx.Association_OneToMany_Rights.First();

                // DELETE one
                list[0].Rights.Clear();

                // INSERT one
                list[1].Rights = new List<Association_OneToMany_Right> {right};

                ctx.SaveChanges(audit);
            }

            // UnitTest - Audit
            {
                var entries = audit.Entries;

                // Entries
                {
                    // Entries Count
                    Assert.AreEqual(2, entries.Count);

                    // Entries State
                    Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                    Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);
                }
            }

            // UnitTest - Audit (Database)
            {
                using (var ctx = new TestContext())
                {
                    // ENSURE order
                    var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).ToList();

                    // Entries
                    {
                        // Entries Count
                        Assert.AreEqual(2, entries.Count);

                        // Entries State
                        Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                        Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);
                    }
                }
            }
        }
        public void Association_OneToMany()
        {
            int leftID;
            int rightID_0;
            int rightID_1;

            TestContext.DeleteAll(x => x.AuditEntryProperties);
            TestContext.DeleteAll(x => x.AuditEntries);
            TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);
            TestContext.DeleteAll(x => x.Association_OneToMany_Rights);

            var audit = AuditHelper.AutoSaveAudit();

            using (var ctx = new TestContext())
            {
                var left   = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First();
                var right0 = new Association_OneToMany_Right {
                    ColumnInt = 0
                };
                var right1 = new Association_OneToMany_Right {
                    ColumnInt = 1
                };

                left.Rights = new List <Association_OneToMany_Right> {
                    right0, right1
                };

                ctx.SaveChanges(audit);

                leftID    = left.ID;
                rightID_0 = right0.ID;
                rightID_1 = right1.ID;
            }

            // UnitTest - Audit
            {
                var entries = audit.Entries;

                // Entries
                {
                    // Entries Count
                    Assert.AreEqual(5, entries.Count);

                    // Entries State
                    Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                    Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State);

                    // Entries EntitySetName
                    Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName);

                    // Entries TypeName
                    Assert.AreEqual(null, entries[0].EntityTypeName);
                    Assert.AreEqual(null, entries[1].EntityTypeName);
                }

                // Properties
                {
                    var propertyIndex = -1;

                    // Properties Count
                    Assert.AreEqual(2, entries[0].Properties.Count);
                    Assert.AreEqual(2, entries[1].Properties.Count);

                    // Association_OneToMany_Left_Rights_Source;ID
                    propertyIndex = 0;
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName);
                    Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].NewValue);
                    Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].NewValue);

                    propertyIndex = 1;
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName);
                    Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].NewValue);
                    Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].NewValue);
                }
            }

            // UnitTest - Audit (Database)
            {
                using (var ctx = new TestContext())
                {
                    // ENSURE order
                    var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList();
                    entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList());

                    // Entries
                    {
                        // Entries Count
                        Assert.AreEqual(5, entries.Count);

                        // Entries State
                        Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                        Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State);

                        // Entries EntitySetName
                        Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName);

                        // Entries TypeName
                        Assert.AreEqual(null, entries[0].EntityTypeName);
                        Assert.AreEqual(null, entries[1].EntityTypeName);
                    }

                    // Properties
                    {
                        var propertyIndex = -1;

                        // Properties Count
                        Assert.AreEqual(2, entries[0].Properties.Count);
                        Assert.AreEqual(2, entries[1].Properties.Count);

                        // Association_OneToMany_Left_Rights_Source;ID
                        propertyIndex = 0;
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName);
                        Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].NewValue);
                        Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].NewValue);

                        propertyIndex = 1;
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName);
                        Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].NewValue);
                        Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].NewValue);
                    }
                }
            }
        }
        public void Association_OneToMany()
        {
            int leftID;
            int rightID_0;
            int rightID_1;

            TestContext.DeleteAll(x => x.AuditEntries);
            TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);
            TestContext.DeleteAll(x => x.Association_OneToMany_Rights);

            var audit = AuditHelper.AutoSaveAudit();

            using (var ctx = new TestContext())
            {
                var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First();
                var right0 = new Association_OneToMany_Right() { ColumnInt = 0 };
                var right1 = new Association_OneToMany_Right() { ColumnInt = 1 };

                left.Rights = new List<Association_OneToMany_Right>() { right0, right1 };

                ctx.SaveChanges();

                leftID = left.ID;
                rightID_0 = right0.ID;
                rightID_1 = right1.ID;
            }

            using (var ctx = new TestContext())
            {
                TestContext.DeleteAll(ctx, x => x.Association_OneToMany_Rights);
                ctx.SaveChanges(audit);
            }

            // UnitTest - Audit
            {
                var entries = audit.Entries;

                // Entries
                {
                    // Entries Count
                    Assert.AreEqual(4, entries.Count);

                    // Entries State
                    Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State);
                    Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);

                    // Entries EntitySetName
                    Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName);

                    // Entries TypeName
                    Assert.AreEqual(null, entries[0].TypeName);
                    Assert.AreEqual(null, entries[1].TypeName);
                }

                // Properties
                {
                    var propertyIndex = -1;

                    // Properties Count
                    Assert.AreEqual(2, entries[0].Properties.Count);
                    Assert.AreEqual(2, entries[1].Properties.Count);

                    // Association_OneToMany_Left_Rights_Source;ID
                    propertyIndex = 0;
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Source;ID", entries[0].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Source;ID", entries[1].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue);
                    Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue);

                    propertyIndex = 1;
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Target;ID", entries[0].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual("Association_OneToMany_Left_Rights_Target;ID", entries[1].Properties[propertyIndex].PropertyName);
                    Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].OldValue);
                    Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue);
                    Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue);
                }
            }

            // UnitTest - Audit (Database)
            {
                using (var ctx = new TestContext())
                {
                    // ENSURE order
                    var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList();
                    entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList());

                    // Entries
                    {
                        // Entries Count
                        Assert.AreEqual(4, entries.Count);

                        // Entries State
                        Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State);
                        Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);

                        // Entries EntitySetName
                        Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName);

                        // Entries TypeName
                        Assert.AreEqual(null, entries[0].TypeName);
                        Assert.AreEqual(null, entries[1].TypeName);
                    }

                    // Properties
                    {
                        var propertyIndex = -1;

                        // Properties Count
                        Assert.AreEqual(2, entries[0].Properties.Count);
                        Assert.AreEqual(2, entries[1].Properties.Count);

                        // Association_OneToMany_Left_Rights_Source;ID
                        propertyIndex = 0;
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Source;ID", entries[0].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Source;ID", entries[1].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue);
                        Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue);

                        propertyIndex = 1;
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Target;ID", entries[0].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual("Association_OneToMany_Left_Rights_Target;ID", entries[1].Properties[propertyIndex].PropertyName);
                        Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].OldValue);
                        Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue);
                        Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue);
                    }
                }
            }
        }
Exemple #4
0
        public void IgnoreRelationshipDeleted_IsFalse()
        {
            TestContext.DeleteAll(x => x.AuditEntryProperties);
            TestContext.DeleteAll(x => x.AuditEntries);
            TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);
            TestContext.DeleteAll(x => x.Association_OneToMany_Rights);

            var audit = AuditHelper.AutoSaveAudit();

            Assert.AreEqual(false, audit.Configuration.IgnoreRelationshipDeleted);

            using (var ctx = new TestContext())
            {
                var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 2).First();

                var right = new Association_OneToMany_Right {
                    ColumnInt = 0
                };

                left.Rights = new List <Association_OneToMany_Right> {
                    right
                };
                ctx.SaveChanges();
            }

            using (var ctx = new TestContext())
            {
                var list  = ctx.Association_OneToMany_Lefts.OrderBy(x => x.ID).ToList();
                var right = ctx.Association_OneToMany_Rights.First();

                // DELETE one
                list[0].Rights.Clear();

                // INSERT one
                list[1].Rights = new List <Association_OneToMany_Right> {
                    right
                };

                ctx.SaveChanges(audit);
            }

            // UnitTest - Audit
            {
                var entries = audit.Entries;

                // Entries
                {
                    // Entries Count
                    Assert.AreEqual(2, entries.Count);

                    // Entries State
                    Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                    Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);
                }
            }

            // UnitTest - Audit (Database)
            {
                using (var ctx = new TestContext())
                {
                    // ENSURE order
                    var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).ToList();

                    // Entries
                    {
                        // Entries Count
                        Assert.AreEqual(2, entries.Count);

                        // Entries State
                        Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State);
                        Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State);
                    }
                }
            }
        }