public void PropertyAddition_WithConcurrenty_ExistingValueOn_Ok()
 {
     JObject apply = new JsonPatcher(doc).Apply(
         JArray.Parse(@"[{  ""type"":""set"",""name"": ""body"", ""value"": ""differnt markup"", ""prevVal"": ""html markup"" }]")
         );
     Assert.Equal(@"{""title"":""A Blog Post"",""body"":""differnt markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", apply.ToString(Formatting.None));
 }
        public void PropertyIncrement()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Set,
        				Name = "blog_id",
        				Value = new JValue(1)
        			},
        		});

            patchedDoc = new JsonPatcher(patchedDoc).Apply(
                new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Inc,
        				Name = "blog_id",
        				Value = new JValue(1)
        			},
        		});

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}],""blog_id"":2}",
                patchedDoc.ToString(Formatting.None));
        }
        public void PropertyRemovalPropertyDoesNotExists()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                JArray.Parse(@"[{ ""type"": ""unset"", ""name"": ""ip"" }]")
                );

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", patchedDoc.ToString(Formatting.None));
        }
Example #4
0
        public void AddingItemToArray_WithConcurrency_Ok()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                JArray.Parse(@"[{ ""type"": ""add"" , ""name"": ""comments"", ""value"": {""author"":""oren"",""text"":""agreed""}, ""prevVal"": [{""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 1""},{""author"":""ayende"",""text"":""good post 2""},{""author"":""oren"",""text"":""agreed""}]}",
                patchedDoc.ToString(Formatting.None));
        }
Example #5
0
        public void AddingItemToArrayWhenArrayDoesNotExists_WithConcurrency_Ok()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                JArray.Parse(@"[{ ""type"":""add"",  ""name"": ""blog_id"", ""value"": 1, ""prevVal"": undefined }]")
                );

            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 PropertyAddition_WithConcurrenty_MissingProp()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                JArray.Parse(@"[{  ""type"":""set"",""name"": ""blog_id"", ""value"": 1, ""prevVal"": undefined }]")
                );

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}],""blog_id"":1}",
                patchedDoc.ToString(Formatting.None));
        }
Example #7
0
        public void SetValueNestedInArray()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                JArray.Parse(
                    @"[{ ""type"": ""modify"",  ""name"": ""comments"", ""position"": 1, ""value"": [{ ""type"":""set"",""name"":""author"",""value"":""oren""} ]}]")
                );

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""oren"",""text"":""good post 2""}],""user"":{""name"":""ayende"",""id"":13}}",
                patchedDoc.ToString(Formatting.None));
        }
		public void PropertyCopyNonExistingProperty()
		{
			var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
				{
					new PatchRequest
					{
						Type = PatchCommandType.Copy,
						Name = "non-existing",
						Value = new RavenJValue("irrelevant")
					},
				});

			Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}",
				patchedDoc.ToString(Formatting.None));
		}
Example #9
0
        public void AddingItemToArrayWhenArrayDoesNotExists()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Add,
        				Name = "blog_id",
        				Value = new JValue(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 PropertyMove()
		{
			var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Rename,
        				Name = "comments",
        				Value = new JValue("cmts")
        			},
        		});

			Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""cmts"":[{""author"":""ayende"",""text"":""good post""}]}",
				patchedDoc.ToString(Formatting.None));
		}
        public void PropertyAddition_WithConcurrenty_ExistingValueOn_Ok()
        {
            JObject apply = new JsonPatcher(doc).Apply(
                new[]
                {
                    new PatchRequest
                    {
                        Type = "set",
                        Name = "body",
                        Value = new JValue("differnt markup"),
                        PrevVal = new JValue("html markup")
                    },
                });

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""differnt markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", apply.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));
        }
Example #13
0
		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));
		}
Example #14
0
        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));
        }
Example #15
0
		public void SetValueInNestedElement()
		{
			var patchedDoc = 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")},
						}
					},
				});

            Assert.Equal(RavenJToken.Parse(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""user"":{""name"":""rahien"",""id"":13}}"),
				patchedDoc,new RavenJTokenEqualityComparer());
		}
Example #16
0
        public void SetValueInNestedElement()
        {
        	var patchedDoc = new JsonPatcher(doc).Apply(
        		new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Modify,
        				Name = "user",
        				Nested = new[]
        				{
        					new PatchRequest {Type = PatchCommandType.Set, Name = "name", Value = new JValue("rahien")},
        				}
        			},
        		});

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""user"":{""name"":""rahien"",""id"":13}}",
                patchedDoc.ToString(Formatting.None));
        }
Example #17
0
        public void RemoveValueInNestedElement()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                new[]
                {
                    new PatchRequest
                    {
                        Type = PatchCommandType.Modify,
                        Name = "user",
                        PrevVal = RavenJObject.Parse(@"{ ""name"": ""ayende"", ""id"": 13}"),
                        Nested = new[]
                        {
                            new PatchRequest {Type = PatchCommandType.Unset, Name = "name" },
                        }
                    },
                });

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""user"":{""id"":13}}",
                patchedDoc.ToString(Formatting.None));
        }
Example #18
0
        public void RenameAllItemsInArray()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                new[]
                    {
                        new PatchRequest
                            {
                                Type = PatchCommandType.Modify,
                                Name = "comments",
                                AllPositions = true,
        				Nested = new[]
        				{
        					new PatchRequest {Type = PatchCommandType.Rename, Name = "author", Value = "authorname"},
        				}
        			},
        		});

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""text"":""good post 1"",""authorname"":""ayende""},{""text"":""good post 2"",""authorname"":""ayende""}],""user"":{""name"":""ayende"",""id"":13}}",
                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));
		}
Example #20
0
        public void SimplePatching()
        {
            using (var documentStore = this.NewDocumentStore())
            {
                #region patching1
                var comment = new BlogComment
                                {
                                    Title = "Foo",
                                    Content = "Bar"
                                };

                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Add,
                                    Name = "Comments",
                                    Value = RavenJObject.FromObject(comment)
                                }
                        });

                #endregion

                #region patching2
                // Setting a native type value
                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Set,
                                    Name = "Title",
                                    Value = RavenJObject.FromObject("New title")
                                }
                        });

                // Setting an object as a property value
                documentStore.DatabaseCommands.Patch(
                    "blogposts/4321",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Set,
                                    Name = "Author",
                                    Value = RavenJObject.FromObject(
                                        new BlogAuthor
                                            {
                                                Name = "Itamar",
                                                ImageUrl = "/author_images/itamar.jpg"
                                            })
                                }
                        });

                #endregion

                #region patching3
                // This is how you rename a property; copying works
                // exactly the same, but with Type = PatchCommandType.Copy
                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Rename,
                                    Name = "Comments",
                                    Value = new RavenJValue("cmts")
                                }
                        });

                #endregion

                #region patching4
                // Assuming we have a Views counter in our entity
                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Inc,
                                    Name = "Views",
                                    Value = new RavenJValue(1)
                                }
                        });

                #endregion

                #region patching_arrays1
                // Append a new comment; Insert operation is supported
                // as well, by using PatchCommandType.Add and
                // specifying a Position to insert at
                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Add,
                                    Name = "Comments",
                                    Value =
                                        RavenJObject.FromObject(new BlogComment
                                                                    {Content = "FooBar"})
                                }
                        });

                // Remove the first comment
                documentStore.DatabaseCommands.Patch(
                    "blogposts/1234",
                    new[]
                        {
                            new PatchRequest
                                {
                                    Type = PatchCommandType.Remove,
                                    Name = "Comments",
                                    Position = 0
                                }
                        });

                #endregion

                var doc = new RavenJObject();

                #region nested1
                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")},
                        }
                    },
                });

                #endregion

                #region nested2
                var removeFromPatchedDoc = new JsonPatcher(doc).Apply(
                new[]
                {
                    new PatchRequest
                    {
                        Type = PatchCommandType.Modify,
                        Name = "user",
                        PrevVal = RavenJObject.Parse(@"{ ""name"": ""ayende"", ""id"": 13}"),
                        Nested = new[]
                        {
                            new PatchRequest {Type = PatchCommandType.Unset, Name = "name" },
                        }
                    },
                });

                #endregion
            }
        }
Example #21
0
        public void AddingItemToArrayWhenArrayDoesNotExists_WithConcurrency_Ok()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Add,
        				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 1""},{""author"":""ayende"",""text"":""good post 2""}],""blog_id"":[1]}",
                patchedDoc.ToString(Formatting.None));
        }
Example #22
0
        public void InsertItemToArray_WithConcurrency_Ok()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Insert,
        				Name = "comments",
						Position = 1,
						Value = RavenJObject.Parse(@"{""author"":""ayende"",""text"":""good post 1.5""}"),
						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 1""},{""author"":""ayende"",""text"":""good post 1.5""},{""author"":""ayende"",""text"":""good post 2""}]}",
                patchedDoc.ToString(Formatting.None));
        }
Example #23
0
        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));
        }
Example #24
0
        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));
        }
Example #25
0
        public void RemoveItemFromArrayByValue()
        {
            var patchedDoc = new JsonPatcher(RavenJObject.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));

        }
Example #26
0
        public void CanAddServeralItemsToSeveralDifferentPartsAtTheSameTime()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
        		{
        			new PatchRequest
        			{
        				Type = PatchCommandType.Add,
        				Name = "blog_id",
        				Value = new RavenJValue(1)
        			},
					new PatchRequest
        			{
        				Type = PatchCommandType.Add,
        				Name = "blog_id",
        				Value = new RavenJValue(2)
        			},
					new PatchRequest
        			{
        				Type = PatchCommandType.Set,
        				Name = "title",
        				Value = new RavenJValue("abc")
        			},
        		});

            Assert.Equal(@"{""title"":""abc"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""blog_id"":[1,2]}",
                patchedDoc.ToString(Formatting.None));
        }
Example #27
0
 public void RemoveValueFromNested()
 {
     var doc = new RavenJObject();
     var removeFromPatchedDoc = new JsonPatcher(doc).Apply(
         new[]
             {
                 new PatchRequest
                     {
                         Type = PatchCommandType.Modify,
                         Name = "user",
                         PrevVal = RavenJObject.Parse(@"{ ""name"": ""ayende"", ""id"": 13}"),
                         Nested = new[]
                             {
                                 new PatchRequest {Type = PatchCommandType.Unset, Name = "name"}
                             }
                     }
             });
 }
		public void NewPropertySetToObject()
		{
			var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
				{
					new PatchRequest
					{
						Type = PatchCommandType.Set,
						Name = "blog_id",
						Value = new RavenJObject
						{
							{"a", "b"}
						}
					},
				});

			Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}],""blog_id"":{""a"":""b""}}",
				patchedDoc.ToString(Formatting.None));
		}
		public void PropertyRemovalPropertyDoesNotExists()
		{
			var patchedDoc = new JsonPatcher(doc).Apply(
				new[]
				{
					new PatchRequest
					{
						Type = PatchCommandType.Unset,
						Name = "ip",
					},
				});

			Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", patchedDoc.ToString(Formatting.None));
		}
		public void PropertyRemoval_WithConcurrency_Ok()
		{
			var patchedDoc = new JsonPatcher(doc).Apply(
				 new[]
				{
					new PatchRequest
					{
						Type = PatchCommandType.Unset,
						Name = "body",
						PrevVal = new RavenJValue("html markup")
					},
				});

			Assert.Equal(@"{""title"":""A Blog Post"",""comments"":[{""author"":""ayende"",""text"":""good post""}]}", patchedDoc.ToString(Formatting.None));
		}