public string JsonPatchesWorks(string leftString, string rightString) { var left = JToken.Parse(leftString); var right = JToken.Parse(rightString); var patchDoc = new JsonDiffer().Diff(left, right); var patcher = new JsonPatcher(); patcher.Patch(ref left, patchDoc); Assert.True(JToken.DeepEquals(left, right)); //var patchedLeft = left.ToString(Formatting.None); //var expected = right.ToString(Formatting.None); //Assert.AreEqual(expected, patchedLeft); var patchJson = patchDoc.ToString(Formatting.None); return patchJson; }
public string JsonPatchesWorks(string leftString, string patchString) { var left = JToken.Parse(leftString); var patchDoc = PatchDocument.Parse(patchString); var patcher = new JsonPatcher(); patcher.Patch(ref left, patchDoc); var patchJson = left.ToString(Formatting.None); return patchJson; }
public void Add_an_non_existing_member_property() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/0/SBN"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "213324234343" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(pointer).Value<string>(); Assert.Equal("213324234343", result); }
public void Copy_array_element() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var frompointer = "/books/0"; var topointer = "/books/-"; patchDocument.AddOperation(new CopyOperation { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken("/books/2"); Assert.IsType(typeof(JObject), result); }
public void AddingItemToArrayWhenArrayDoesNotExists() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Add, Name = "blog_id", Value = new RavenJValue(1), } }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""blog_id"":[1]}", patchedDoc.ToString(Formatting.None)); }
public void AddingItemToArray() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Add, Name = "comments", Value = RavenJObject.Parse(@"{""author"":""oren"",""text"":""agreed""}") } }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""},{""author"":""oren"",""text"":""agreed""}]}", patchedDoc.ToString(Formatting.None)); }
public void Add_an_existing_member_property() // Why isn't this replace? { var sample = GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/0/title"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "Little Red Riding Hood" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(pointer).Value<string>(); Assert.Equal("Little Red Riding Hood", result); }
public void RemoveItemFromArrayByValue() { var patchedDoc = new JsonPatcher(JObject.Parse(@"{ name: ""Joe Doe"", roles: [""first/role"", ""second/role"", ""third/role""] }")).Apply( new[] { new PatchRequest { Type = PatchCommandType.Remove, Name = "roles", Value = "second/role" }, }); Assert.Equal(@"{""name"":""Joe Doe"",""roles"":[""first/role"",""third/role""]}", patchedDoc.ToString(Formatting.None)); }
public void Add_an_array_element() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/-"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") }) }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var list = sample["books"] as JArray; Assert.Equal(3, list.Count); }
public void PropertyRenameNonExistingProperty() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Rename, Name = "doesnotexist", Value = new RavenJValue("irrelevant") }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", patchedDoc.ToString(Formatting.None)); }
public void PropertyAddition() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "blog_id", Value = new RavenJValue(1) }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}],""blog_id"":1}", patchedDoc.ToString(Formatting.None)); }
public void PropertyAddition_WithConcurrenty_ExistingValueOn_Ok() { RavenJObject apply = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "body", Value = new RavenJValue("differnt markup"), PrevVal = new RavenJValue("html markup") }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""differnt markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", apply.ToString(Formatting.None)); }
public void Test_a_value() { var sample = GetSample2(); var patchDocument = new PatchDocument(); string pointer = "/books/0/author"; patchDocument.AddOperation(new TestOperation { Path = pointer, Value = new JValue("Billy Burton") }); Assert.Throws <InvalidOperationException>(() => { var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); }); }
public void RemoveItemFromArray() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Remove, Name = "comments", Position = 0 }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 2""}]}", patchedDoc.ToString(Formatting.None)); }
public void ExistingPropertySetToArray() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "title", Value = new RavenJArray() }, }); Assert.Equal(@"{""title"":[],""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", patchedDoc.ToString(Formatting.None)); }
public void Copy_property() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/0/ISBN"); var topointer = new JsonPointer("/books/1/ISBN"); patchDocument.AddOperation(new AddOperation() { Path = frompointer, Value = new JValue("21123123") }); patchDocument.AddOperation(new CopyOperation() { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = new JsonPointer("/books/1/ISBN").Find(sample); Assert.Equal("21123123", result); }
public void RemoveItemFromArray_WithConcurrency_Ok() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Remove, Name = "comments", Position = 0, PrevVal = RavenJArray.Parse(@"[{""author"":""ayende"",""text"":""good post 1""},{author: ""ayende"", text:""good post 2""}]") }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 2""}]}", patchedDoc.ToString(Formatting.None)); }
public void PropertyAddition_WithConcurrently_MissingProp() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "blog_id", Value = new RavenJValue(1), PrevVal = RavenJObject.Parse("{'a': undefined}")["a"] }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}],""blog_id"":1}", patchedDoc.ToString(Formatting.None)); }
public void PatchTokenCopy_DoesNotModifyArgument() { var input = JToken.Parse("{}"); JsonPatcher.PatchTokenCopy( input, new[] { new JsonPatchAddOperation { Path = "/var", Value = JToken.Parse("3") } }); input.ShouldBeJson("{}"); }
public void AddingEmptyObject() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "NewProp", Value = new RavenJObject() } }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""NewProp"":{}}", patchedDoc.ToString(Formatting.None)); }
public void Test_a_value() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/author"); patchDocument.AddOperation(new TestOperation() { Path = pointer, Value = new JValue("Billy Burton") }); Assert.Throws(typeof(InvalidOperationException), () => { var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); }); }
public void Move_property() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/0/author"); var topointer = new JsonPointer("/books/1/author"); patchDocument.AddOperation(new MoveOperation() { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)topointer.Find(sample); Assert.Equal("F. Scott Fitzgerald", result); }
public void Move_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/1"); var topointer = new JsonPointer("/books/0/child"); patchDocument.AddOperation(new MoveOperation() { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = topointer.Find(sample); Assert.IsType(typeof(JObject), result); }
public void InsertItemToArray() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = "insert", Name = "comments", Position = 1, Value = JObject.Parse(@"{""author"":""ayende"",""text"":""good post 1.5""}") }, }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 1.5""},{""author"":""ayende"",""text"":""good post 2""}]}", patchedDoc.ToString(Formatting.None)); }
public void AddingItemToArray_WithConcurrency_Ok() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = "add", Name = "comments", PrevVal = JArray.Parse(@"[{""author"":""ayende"",""text"":""good post 1""},{author: ""ayende"", text:""good post 2""}]"), Value = JObject.Parse(@"{""author"":""oren"",""text"":""agreed""}") } }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""},{""author"":""oren"",""text"":""agreed""}]}", patchedDoc.ToString(Formatting.None)); }
public void AddingItemToArrayWhenArrayDoesNotExists_WithConcurrency_Ok() { var patchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = "add", Name = "blog_id", Value = new JValue(1), PrevVal = JObject.Parse("{'a': undefined}").Property("a").Value } }); Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""blog_id"":[1]}", patchedDoc.ToString(Formatting.None)); }
public void Remove_an_array_element() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/0"; patchDocument.AddOperation(new RemoveOperation { Path = pointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); Assert.Null(sample.SelectPatchToken("/books/1")); }
public void Can_replace_existing_boolean() { var sample = JToken.FromObject(new MyConfigClass { RequiresConfiguration = true }); var patchDocument = new PatchDocument(); patchDocument.AddOperation(new ReplaceOperation { Path = "/RequiresConfiguration", Value = new JValue(false) }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); Assert.False(sample.ToObject <MyConfigClass>().RequiresConfiguration); }
public void Add_an_non_existing_member_property() // Why isn't this replace? { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/ISBN"); patchDocument.AddOperation(new AddOperation() { Path = pointer, Value = new JValue("213324234343") }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)pointer.Find(sample); Assert.Equal("213324234343", result); }
public void RemoveItemFromArrayByNonExistingValue() { var value = @"{""name"":""Joe Doe"",""roles"":[""first/role"",""second/role"",""third/role""]}"; var patchedDoc = new JsonPatcher(RavenJObject.Parse(value)); var result = patchedDoc.Apply( new[] { new PatchRequest { Type = PatchCommandType.Remove, Name = "roles", Value = "this/does/not/exist" }, }); Assert.Equal(value, result.ToString(Formatting.None)); }
public string JsonPatchesWorks(string leftString, string rightString) { var left = JToken.Parse(leftString); var right = JToken.Parse(rightString); var patchDoc = new JsonDiffer().Diff(left, right, false); var patcher = new JsonPatcher(); patcher.Patch(ref left, patchDoc); Assert.True(JToken.DeepEquals(left, right)); //Assert.AreEqual(expected, patchedLeft); var patchJson = patchDoc.ToString(Formatting.None); return(patchJson); }
public void Add_an_array_element_to_non_existent_property() { var sample = GetSample2(); var patchDocument = new PatchDocument(); string pointer = "/someobject/somearray/-"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") }) }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var list = sample["someobject"]["somearray"] as JArray; Assert.Single(list); }
public void Add_an_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/-"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") }) }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var list = sample["books"] as JArray; Assert.Equal(3, list.Count); }
public void Add_an_non_existing_member_property() // Why isn't this replace? { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/0/SBN"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "213324234343" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(pointer).Value <string>(); Assert.Equal("213324234343", result); }
public void Add_an_existing_member_property() // Why isn't this replace? { var sample = GetSample2(); var patchDocument = new PatchDocument(); var pointer = "/books/0/title"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "Little Red Riding Hood" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(pointer).Value <string>(); Assert.Equal("Little Red Riding Hood", result); }
public void Remove_an_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0"); patchDocument.AddOperation(new RemoveOperation() { Path = pointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); Assert.Throws(typeof(ArgumentException), () => { var x = pointer.Find("/books/1"); }); }
public void SetValueInNested() { var doc = new RavenJObject(); var addToPatchedDoc = new JsonPatcher(doc).Apply( new[] { new PatchRequest { Type = PatchCommandType.Modify, Name = "user", Nested = new[] { new PatchRequest { Type = PatchCommandType.Set, Name = "name", Value = new RavenJValue("rahien") } } } }); }
public void Move_array_element() { var sample = GetSample2(); var patchDocument = new PatchDocument(); string frompointer = "/books/1"; string topointer = "/books/0/child"; patchDocument.AddOperation(new MoveOperation { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(topointer); Assert.IsType <JObject>(result); }
public void ComplexExampleWithDeepArrayChange() { var leftPath = @".\samples\scene{0}a.json"; var rightPath = @".\samples\scene{0}b.json"; var i = 1; while(File.Exists(string.Format(leftPath, i))) { var scene1Text = File.ReadAllText(string.Format(leftPath, i)); var scene1 = JToken.Parse(scene1Text); var scene2Text = File.ReadAllText(string.Format(rightPath, i)); var scene2 = JToken.Parse(scene2Text); var patchDoc = new JsonDiffer().Diff(scene1, scene2, true); //Assert.AreEqual("[{\"op\":\"remove\",\"path\":\"/items/0/entities/1\"}]", var patcher = new JsonPatcher(); patcher.Patch(ref scene1, patchDoc); Assert.True(JToken.DeepEquals(scene1, scene2)); i++; } }
public void PatchObjectCopy_DoesNotModifyArgument() { var input = new Person { Name = "Andrey" }; JsonPatcher.PatchObjectCopy( input, new[] { new JsonPatchAddOperation { Path = "/Name", Value = "Ivan" } }); input.Name.ShouldBe("Andrey"); }
public void Move_property() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var frompointer = "/books/0/author"; var topointer = "/books/1/author"; patchDocument.AddOperation(new MoveOperation { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(topointer).Value <string>(); Assert.Equal("F. Scott Fitzgerald", result); }
public void Move_property() { var sample = GetSample2(); var patchDocument = new PatchDocument(); var frompointer = "/books/0/author"; var topointer = "/books/1/author"; patchDocument.AddOperation(new MoveOperation { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = sample.SelectPatchToken(topointer).Value<string>(); Assert.Equal("F. Scott Fitzgerald", result); }