コード例 #1
0
        public void GetBsonArrayDiffWithNoDifferences()
        {
            var arrayA = new BsonArray(Enumerable.Range(1, 5));
            var arrayB = new BsonArray(Enumerable.Range(1, 5));

            Assert.AreEqual(null, BsonDiff.GetDifferences(arrayA, arrayB).Difference);
            Assert.AreEqual(null, BsonDiff.GetDifferences(arrayB, arrayA).Difference);
        }
コード例 #2
0
        public void ArrayBsonValue()
        {
            var arrayA = new BsonArray(Enumerable.Range(1, 5));
            var arrayB = new BsonArray(Enumerable.Range(1, 5));

            Assert.IsFalse(BsonDiff.HasDifferences((BsonValue)arrayA, arrayB));
            Assert.IsFalse(BsonDiff.HasDifferences((BsonValue)arrayB, arrayA));
        }
コード例 #3
0
        public void ArrayHasMissingItems()
        {
            var arrayA = new BsonArray(Enumerable.Range(1, 5));
            var arrayB = new BsonArray(Enumerable.Range(1, 2));

            Assert.IsTrue(BsonDiff.HasDifferences(arrayA, arrayB));
            Assert.IsTrue(BsonDiff.HasDifferences(arrayB, arrayA));
        }
コード例 #4
0
        public void NullArray()
        {
            var arrayA = new BsonArray(Enumerable.Range(1, 5));

            Assert.IsTrue(BsonDiff.HasDifferences(arrayA, null));
            Assert.IsTrue(BsonDiff.HasDifferences(null, arrayA));
            Assert.IsFalse(BsonDiff.HasDifferences((BsonArray)null, null));
        }
コード例 #5
0
        public void MismatchedValueType()
        {
            var valueA = (BsonValue) new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });
            var valueB = BsonValue.Create(123);

            Assert.IsTrue(BsonDiff.HasDifferences(valueA, valueB));
            Assert.IsTrue(BsonDiff.HasDifferences(valueB, valueA));
        }
コード例 #6
0
        public void NullValue()
        {
            var valueA = (BsonValue) new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });

            Assert.IsTrue(BsonDiff.HasDifferences(valueA, null));
            Assert.IsTrue(BsonDiff.HasDifferences(null, valueA));
            Assert.IsFalse(BsonDiff.HasDifferences((BsonValue)null, null));
        }
コード例 #7
0
        public void DocumentHasDifferentBasicPropertyValue()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 30 }
            });

            Assert.IsTrue(BsonDiff.HasDifferences(documentA, documentB));
            Assert.IsTrue(BsonDiff.HasDifferences(documentB, documentA));
        }
コード例 #8
0
        public void DocumentHasMissingProperties()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 }
            });

            Assert.IsTrue(BsonDiff.HasDifferences(documentA, documentB));
            Assert.IsTrue(BsonDiff.HasDifferences(documentB, documentA));
        }
コード例 #9
0
        public void DocumentHasSamePropertyCountButDifferentNames()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "YearsOld", 20 },
                { "Name", "Peter" }
            });

            Assert.IsTrue(BsonDiff.HasDifferences(documentA, documentB));
            Assert.IsTrue(BsonDiff.HasDifferences(documentB, documentA));
        }
コード例 #10
0
        public void GetDocumentDiffWithNoDifferences()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });

            Assert.IsFalse(BsonDiff.GetDifferences(documentA, documentB).HasDifference);
        }
コード例 #11
0
        public void DocumentBsonValue()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true }
            });

            Assert.IsFalse(BsonDiff.HasDifferences((BsonValue)documentA, documentB));
            Assert.IsFalse(BsonDiff.HasDifferences((BsonValue)documentB, documentA));
        }
 /// <summary>
 /// Performed prior to saving, this detect changes between the original and current state of entries, updating states where necessary.
 /// </summary>
 public void DetectChanges()
 {
     foreach (var typeGroup in EntryLookupByType.Values)
     {
         for (var i = typeGroup.Count - 1; i >= 0; i--)
         {
             var entry = typeGroup[i];
             if (entry.State == EntityEntryState.NoChanges || entry.State == EntityEntryState.Updated)
             {
                 if (BsonDiff.HasDifferences(entry.OriginalValues, entry.CurrentValues))
                 {
                     entry.State = EntityEntryState.Updated;
                 }
                 else
                 {
                     entry.State = EntityEntryState.NoChanges;
                 }
             }
         }
     }
 }
コード例 #13
0
        public void GetDocumentDiffWithNullDocuments()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Michael" }
            });

            var resultWithSameValues = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Michael" }
            });

            var resultWithClearedValues = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", BsonUndefined.Value },
                { "Name", BsonUndefined.Value }
            });

            Assert.AreEqual(resultWithClearedValues, BsonDiff.GetDifferences(documentA, null).Difference);
            Assert.AreEqual(resultWithSameValues, BsonDiff.GetDifferences(null, documentA).Difference);
        }
コード例 #14
0
        public void GetBsonArrayDiffWithSameCountButDifferentValues()
        {
            var arrayA = new BsonArray(new int[] { 1, 2, 3, 4, 5, 6 });
            var arrayB = new BsonArray(new int[] { 1, 3, 5, 7, 9, 11 });
            var resultWithArrayBAsDefault = new BsonDocument(new Dictionary <string, object>
            {
                { "1", 3 },
                { "2", 5 },
                { "3", 7 },
                { "4", 9 },
                { "5", 11 }
            });
            var resultWithArrayAAsDefault = new BsonDocument(new Dictionary <string, object>
            {
                { "1", 2 },
                { "2", 3 },
                { "3", 4 },
                { "4", 5 },
                { "5", 6 }
            });

            Assert.AreEqual(resultWithArrayBAsDefault, BsonDiff.GetDifferences(arrayA, arrayB).Difference);
            Assert.AreEqual(resultWithArrayAAsDefault, BsonDiff.GetDifferences(arrayB, arrayA).Difference);
        }
コード例 #15
0
        public void GetBsonArrayDiffWithNullArrays()
        {
            var arrayA = new BsonArray(Enumerable.Range(1, 5));
            var resultWithSameValues = new BsonDocument(new Dictionary <string, object>
            {
                { "0", 1 },
                { "1", 2 },
                { "2", 3 },
                { "3", 4 },
                { "4", 5 }
            });
            var resultWithClearedValues = new BsonDocument(new Dictionary <string, object>
            {
                { "0", BsonUndefined.Value },
                { "1", BsonUndefined.Value },
                { "2", BsonUndefined.Value },
                { "3", BsonUndefined.Value },
                { "4", BsonUndefined.Value }
            });

            Assert.AreEqual(resultWithClearedValues, BsonDiff.GetDifferences(arrayA, null).Difference);
            Assert.AreEqual(resultWithSameValues, BsonDiff.GetDifferences(null, arrayA).Difference);
            Assert.AreEqual(null, BsonDiff.GetDifferences((BsonArray)null, null).Difference);
        }
コード例 #16
0
 /// <summary>
 /// Whether there are any changes between the original and current values of the entity.
 /// </summary>
 /// <returns></returns>
 public static bool HasChanges <TEntity>(this DbEntityEntry <TEntity> entry)
 {
     return(BsonDiff.HasDifferences(entry.OriginalValues, entry.CurrentValues));
 }
コード例 #17
0
        public void GetDocumentDiffWithPartialDifferences()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true },
                { "Comment", string.Empty },
                { "Moderator", false },
                { "ModerationDate", null },
                { "Level", 5 },
                { "ItemIDList", new int[] { 5, 10, 17 } },
                {
                    "NestedDetails",
                    new Dictionary <string, object>
                    {
                        { "ID", 23 }
                    }
                }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 30 },
                { "Name", "Sam" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true },
                { "Comment", string.Empty },
                { "Moderator", true },
                { "ModerationDate", new DateTime(2017, 10, 5) },
                { "Level", 5 },
                { "ItemIDList", new int[] { 5, 10, 17, 22 } },
                {
                    "NestedDetails",
                    new Dictionary <string, object>
                    {
                        { "ID", 24 }
                    }
                }
            });

            var result = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 30 },
                { "Name", "Sam" },
                { "Moderator", true },
                { "ModerationDate", new DateTime(2017, 10, 5) },
                {
                    "ItemIDList",
                    new Dictionary <string, object>
                    {
                        { "3", 22 }
                    }
                },
                {
                    "NestedDetails",
                    new Dictionary <string, object>
                    {
                        { "ID", 24 }
                    }
                }
            });

            Assert.AreEqual(result, BsonDiff.GetDifferences(documentA, documentB).Difference);
        }