public void AddToList() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 0); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList); }
public void AddToGenericList() { var doc = new SimpleDTO { IntegerGenericList = new List<int> { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add(o => o.IntegerGenericList, 4, 0); patchDoc.ApplyTo(doc); Assert.Equal(new List<int> { 4, 1, 2, 3 }, doc.IntegerGenericList); }
public void AddResultsShouldReplace() { var doc = new SimpleDTO() { StringProperty = "A" }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<string>(o => o.StringProperty, "B"); patchDoc.ApplyTo(doc); var s = new Operations.Operation(); Assert.Equal("B", doc.StringProperty); }
public void AddResultsShouldReplaceWithSerialization() { var doc = new SimpleDTO() { StringProperty = "A" }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<string>(o => o.StringProperty, "B"); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal("B", doc.StringProperty); }
public void CopyFromListToEndOfList() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<int>(o => o.IntegerList, 0, o => o.IntegerList); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.IntegerList); }
public void ReplaceWithSerialization() { var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10 }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<string>(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal("B", doc.StringProperty); Assert.Equal(12, doc.DecimalValue); }
public void SerializationNoEnvelope() { var doc = new SimpleDTO() { StringProperty = "A" }; // create patch var patch = "[{ \"op\": \"replace\", \"path\": \"/stringproperty\", \"value\": \"B\" }]"; var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(patch); deserialized.ApplyTo(doc); Assert.Equal("B", doc.StringProperty); }
public void AddToListInvalidPositionTooLarge() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 4); Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); }); }
public void Replace() { var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10 }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<string>(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); patchDoc.ApplyTo(doc); Assert.Equal("B", doc.StringProperty); Assert.Equal(12, doc.DecimalValue); }
public void SerializationTests() { var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); patchDoc.Replace(o => o.DoubleValue, 12); patchDoc.Replace(o => o.FloatValue, 12); patchDoc.Replace(o => o.IntegerValue, 12); // serialize & deserialize var serialized = JsonConvert.SerializeObject(patchDoc); var deserizalized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserizalized.ApplyTo(doc); Assert.Equal("B", doc.StringProperty); Assert.Equal(12, doc.DecimalValue); Assert.Equal(12, doc.DoubleValue); Assert.Equal(12, doc.FloatValue); Assert.Equal(12, doc.IntegerValue); }
public void MoveInGenericList() { var doc = new SimpleDTO { IntegerGenericList = new List<int> { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move(o => o.IntegerGenericList, 0, o => o.IntegerGenericList, 1); patchDoc.ApplyTo(doc); Assert.Equal(new List<int> { 2, 1, 3 }, doc.IntegerGenericList); }
public void RemoveWithSerialization() { var doc = new SimpleDTO() { StringProperty = "A" }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<string>(o => o.StringProperty); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(null, doc.StringProperty); }
public void SerializeAndReplaceNestedObjectTest() { var doc = new SimpleDTOWithNestedDTO() { SimpleDTO = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } } }; var newDTO = new SimpleDTO() { DoubleValue = 1 }; // create patch JsonPatchDocument<SimpleDTOWithNestedDTO> patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>(); patchDoc.Replace(o => o.SimpleDTO, newDTO); // serialize & deserialize var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(1, doc.SimpleDTO.DoubleValue); Assert.Equal(0, doc.SimpleDTO.IntegerValue); Assert.Equal(null, doc.SimpleDTO.IntegerList); }
public void CopyFromListToNonListWithSerialization() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<int>(o => o.IntegerList, 0, o => o.IntegerValue); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(1, doc.IntegerValue); }
public void Remove() { var doc = new SimpleDTO() { StringProperty = "A" }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<string>(o => o.StringProperty); patchDoc.ApplyTo(doc); Assert.Equal(null, doc.StringProperty); }
public void MoveToEndOfListWithSerialization() { var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<int>(o => o.IntegerValue, o => o.IntegerList); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(0, doc.IntegerValue); Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList); }
public void MoveToEndOfList() { var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<int>(o => o.IntegerValue, o => o.IntegerList); patchDoc.ApplyTo(doc); Assert.Equal(0, doc.IntegerValue); Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList); }
public void MoveFomListToNonList() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<int>(o => o.IntegerList, 0, o => o.IntegerValue); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() { 2, 3 }, doc.IntegerList); Assert.Equal(1, doc.IntegerValue); }
public void SerializationMustNotIncudeEnvelope() { var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); patchDoc.Replace(o => o.DoubleValue, 12); patchDoc.Replace(o => o.FloatValue, 12); patchDoc.Replace(o => o.IntegerValue, 12); var serialized = JsonConvert.SerializeObject(patchDoc); Assert.Equal(false, serialized.Contains("operations")); Assert.Equal(false, serialized.Contains("Operations")); }
public void ReplaceFullListWithCollection() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<IEnumerable<int>>(o => o.IntegerList, new Collection<int>() { 4, 5, 6 }); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList); }
public void DeserializationMustWorkWithoutEnvelope() { var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); patchDoc.Replace(o => o.DoubleValue, 12); patchDoc.Replace(o => o.FloatValue, 12); patchDoc.Replace(o => o.IntegerValue, 12); // default: no envelope var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); Assert.IsType<JsonPatchDocument<SimpleDTO>>(deserialized); }
public void ReplaceFullListWithCollectionWithSerialization() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<IEnumerable<int>>(o => o.IntegerList, new Collection<int>() { 4, 5, 6 }); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList); }
public void SerializeAndReplaceGuidTest() { var doc = new SimpleDTO() { GuidValue = Guid.NewGuid() }; var newGuid = Guid.NewGuid(); // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace(o => o.GuidValue, newGuid); // serialize & deserialize var serialized = JsonConvert.SerializeObject(patchDoc); var deserizalized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserizalized.ApplyTo(doc); Assert.Equal(newGuid, doc.GuidValue); }
public void CopyFromNonListToList() { var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<int>(o => o.IntegerValue, o => o.IntegerList, 0); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() {5, 1, 2, 3} , doc.IntegerList); }
public void RemoveFromGenericList() { var doc = new SimpleDTO { IntegerGenericList = new List<int> { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove(o => o.IntegerGenericList, 2); patchDoc.ApplyTo(doc); Assert.Equal(new List<int> { 1, 2 }, doc.IntegerGenericList); }
public void RemoveFromListInvalidPositionTooSmall() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList,-1); Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); }); }
public void AddToListWithSerialization() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 0); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList); }
public void RemoveFromListInvalidPositionTooSmallWithSerialization() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList, -1); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); }); }
public void ReplaceAtEndOfList() { var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<int>(o => o.IntegerList, 5); patchDoc.ApplyTo(doc); Assert.Equal(new List<int>() { 1, 2, 5 }, doc.IntegerList); }
public void CopyWithSerialization() { var doc = new SimpleDTO() { StringProperty = "A", AnotherStringProperty = "B" }; // create patch JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<string>(o => o.StringProperty, o => o.AnotherStringProperty); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); deserialized.ApplyTo(doc); Assert.Equal("A", doc.AnotherStringProperty); }