예제 #1
0
        public void AbleToAddObjectListItemThenApplyViaPatch()
        {
            var testObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(testObj);

            var updatedTestObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(updatedTestObj);

            updatedTestObj.ListOfObjectProperty.Add(new TestClass {
                StringProperty = "added"
            });

            var diff = ObjectDiffPatch.GenerateDiff(testObj, updatedTestObj);

            var updatePatch = JsonConvert.SerializeObject(diff.NewValues);

            var objToUpdate = GetSimpleTestObject();

            PopulateObjectListOnTestClass(objToUpdate);

            var updatedObj = ObjectDiffPatch.PatchObject(objToUpdate, updatePatch);

            Assert.AreEqual(updatedTestObj.ListOfObjectProperty.Count, updatedObj.ListOfObjectProperty.Count);

            var addedListItem = updatedObj.ListOfObjectProperty.SingleOrDefault(obj => obj != null && obj.StringProperty == "added");

            Assert.IsNotNull(addedListItem);
        }
예제 #2
0
        /// <summary>
        ///     Asserts that the current <see cref="JToken" /> is equivalent to the <paramref name="expected" /> element,
        ///     using its <see cref="JToken.DeepEquals(JToken, JToken)" /> implementation.
        /// </summary>
        /// <param name="expected">The expected element</param>
        /// <param name="because">
        ///     A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        ///     is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        ///     Zero or more objects to format using the placeholders in <see paramref="because" />.
        /// </param>
        public AndConstraint <JTokenAssertions> BeEquivalentTo(JToken expected, string because,
                                                               params object[] becauseArgs)
        {
            ObjectDiffPatchResult diff = ObjectDiffPatch.GenerateDiff(Subject, expected);
            JToken firstDifferingToken = diff.NewValues?.First ?? diff.OldValues?.First;

            Execute.Assertion
            .ForCondition(diff.AreEqual)
            .BecauseOf(because, becauseArgs)
            .FailWith("Expected JSON document {0} to be equivalent to {1}{reason}, but differs at {2}.",
                      Subject, expected, firstDifferingToken);

            return(new AndConstraint <JTokenAssertions>(this));
        }
예제 #3
0
        /// <summary>
        ///     Asserts that the current <see cref="JToken" /> is equivalent to the <paramref name="expected" /> element,
        ///     using its <see cref="JToken.DeepEquals(JToken, JToken)" /> implementation.
        /// </summary>
        /// <param name="expected">The expected element</param>
        /// <param name="because">
        ///     A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        ///     is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        ///     Zero or more objects to format using the placeholders in <see paramref="because" />.
        /// </param>
        public AndConstraint <JTokenAssertions> BeEquivalentTo(JToken expected, string because,
                                                               params object[] becauseArgs)
        {
            ObjectDiffPatchResult diff          = ObjectDiffPatch.GenerateDiff(Subject, expected);
            JToken          firstDifferingToken = diff.NewValues?.First ?? diff.OldValues?.First;
            JTokenFormatter formatter           = new JTokenFormatter();

            Execute.Assertion
            .ForCondition(diff.AreEqual)
            .BecauseOf(because, becauseArgs)
            .FailWith($"Expected JSON document {formatter.ToString(Subject, true)}" +
                      $" to be equivalent to {formatter.ToString(expected, true)}" +
                      $"{{reason}}, but differs at {firstDifferingToken}.");

            return(new AndConstraint <JTokenAssertions>(this));
        }
        public void Test_DiffField_handling_nulls()
        {
            var token = JToken.Parse("{\"field\":\"value\"}");

            var result = new ObjectDiffPatchResult();

            ObjectDiffPatch.DiffField("field", null, token, result);
            result.NewValues["field"].Should().Be(token);
            result.NewValues["field"].HasValues.Should().BeTrue();
            result.OldValues["field"].HasValues.Should().BeFalse();

            result = new ObjectDiffPatchResult();
            ObjectDiffPatch.DiffField("field", token, null, result);
            result.OldValues["field"].Should().Be(token);
            result.OldValues["field"].HasValues.Should().BeTrue();
            result.NewValues["field"].HasValues.Should().BeFalse();
        }
        private static string GetNotEquivalentMessage(JToken actual, JToken expected,
                                                      string reason = null, params object[] reasonArgs)
        {
            var diff = ObjectDiffPatch.GenerateDiff(actual, expected);
            var key  = diff.NewValues?.First ?? diff.OldValues?.First;

            var because = string.Empty;

            if (!string.IsNullOrWhiteSpace(reason))
            {
                because = " because " + string.Format(reason, reasonArgs);
            }

            var expectedMessage = $"Expected JSON document {_formatter.ToString(actual)}" +
                                  $" to be equivalent to {_formatter.ToString(expected)}" +
                                  $"{because}, but differs at {_formatter.ToString(key)}.";

            return(expectedMessage);
        }
예제 #6
0
        public void AbleToDiffAndPatchSimpleObject()
        {
            var testObj = GetSimpleTestObject();

            var updatedTestObj = GetSimpleTestObject();

            updatedTestObj.StringProperty = "this is an updated string";
            updatedTestObj.IntProperty    = 5678;
            updatedTestObj.DoubleProperty = 123.456;

            var diff = ObjectDiffPatch.GenerateDiff(testObj, updatedTestObj);

            var revertPatch = JsonConvert.SerializeObject(diff.OldValues);

            var revertedObj = ObjectDiffPatch.PatchObject(updatedTestObj, revertPatch);

            Assert.AreEqual(testObj.StringProperty, revertedObj.StringProperty);
            Assert.AreEqual(testObj.IntProperty, revertedObj.IntProperty);
            Assert.AreEqual(testObj.DoubleProperty, revertedObj.DoubleProperty);
        }
        public void Should_Generate_Diffs_for_POCOs_and_JObjects()
        {
            var sut = ObjectDiffPatch.GenerateDiff <Dummy>(null, null);

            sut.AreEqual.Should().BeTrue();
            sut.OldValues.Should().BeNull();
            sut.NewValues.Should().BeNull();

            var a = new Dummy {
                Id = "foo"
            };
            var ja = JObject.FromObject(a);

            sut = ObjectDiffPatch.GenerateDiff(a, null);
            sut.AreEqual.Should().BeFalse();
            sut.OldValues.Should().Be(ja);
            sut.NewValues.Should().BeNull();

            sut = ObjectDiffPatch.GenerateDiff(null, a);
            sut.AreEqual.Should().BeFalse();
            sut.OldValues.Should().BeNull();
            sut.NewValues.Should().Be(ja);

            var b = new Dummy {
                Id = "bar"
            };
            var jb = JObject.FromObject(b);

            sut = ObjectDiffPatch.GenerateDiff(a, b);
            sut.AreEqual.Should().BeFalse();
            JToken.DeepEquals(sut.OldValues, ja).Should().BeTrue();
            JToken.DeepEquals(sut.NewValues, jb).Should().BeTrue();

            // now for JObjects
            sut = ObjectDiffPatch.GenerateDiff(ja, jb);
            sut.AreEqual.Should().BeFalse();
            JToken.DeepEquals(sut.OldValues, ja).Should().BeTrue();
            JToken.DeepEquals(sut.NewValues, jb).Should().BeTrue();
        }
예제 #8
0
        public void AbleToDeleteObjectListItemThenRevertViaPatch()
        {
            var testObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(testObj);

            var updatedTestObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(updatedTestObj);

            updatedTestObj.ListOfObjectProperty.RemoveAt(1);

            Assert.AreNotEqual(testObj.ListOfObjectProperty.Count, updatedTestObj.ListOfObjectProperty.Count);

            var diff = ObjectDiffPatch.GenerateDiff(testObj, updatedTestObj);

            var revertPatch = JsonConvert.SerializeObject(diff.OldValues);

            var revertedObj = ObjectDiffPatch.PatchObject(updatedTestObj, revertPatch);

            Assert.AreEqual(testObj.ListOfObjectProperty.Count, revertedObj.ListOfObjectProperty.Count);
        }
예제 #9
0
        public void AbleToDeleteStringListItemThenRevertViaPatch()
        {
            var testObj = GetSimpleTestObject();

            PopulateStringListOnTestClass(testObj);

            var updatedTestObj = GetSimpleTestObject();

            PopulateStringListOnTestClass(updatedTestObj);

            updatedTestObj.ListOfStringProperty.Remove("list");

            CollectionAssert.AreNotEqual(testObj.ListOfStringProperty, updatedTestObj.ListOfStringProperty);


            var diff = ObjectDiffPatch.GenerateDiff(testObj, updatedTestObj);

            var revertPatch = JsonConvert.SerializeObject(diff.OldValues);

            var revertedObj = ObjectDiffPatch.PatchObject(updatedTestObj, revertPatch);

            CollectionAssert.AreEqual(testObj.ListOfStringProperty, revertedObj.ListOfStringProperty);
        }
예제 #10
0
        public void AbleToEditObjectInListThenRevertViaPatch()
        {
            var testObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(testObj);

            var updatedTestObj = GetSimpleTestObject();

            PopulateObjectListOnTestClass(updatedTestObj);

            updatedTestObj.ListOfObjectProperty[2].IntProperty    = 30;
            updatedTestObj.ListOfObjectProperty[2].StringProperty = "this is an update to the last object in the list";
            updatedTestObj.ListOfObjectProperty[2].DoubleProperty = 33.333;

            var diff = ObjectDiffPatch.GenerateDiff(testObj, updatedTestObj);

            var revertPatch = JsonConvert.SerializeObject(diff.OldValues);

            var revertedObj = ObjectDiffPatch.PatchObject(updatedTestObj, revertPatch);

            Assert.AreEqual(testObj.ListOfObjectProperty[2].IntProperty, revertedObj.ListOfObjectProperty[2].IntProperty);
            Assert.AreEqual(testObj.ListOfObjectProperty[2].StringProperty, revertedObj.ListOfObjectProperty[2].StringProperty);
            Assert.AreEqual(testObj.ListOfObjectProperty[2].DoubleProperty, revertedObj.ListOfObjectProperty[2].DoubleProperty);
        }