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));
        }
        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));
        }
        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));
        }
        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));
        }
        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));
        }
        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));
        }
        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));
        }
        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));
        }
 /// <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;
                 }
             }
         }
     }
 }
        public void DocumentHasNoDifferences()
        {
            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(documentA, documentB));
            Assert.IsFalse(BsonDiff.HasDifferences(documentB, documentA));
        }
 /// <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));
 }