public void AddResultsShouldReplace() { // Arrange var doc = new SimpleDTO() { StringProperty = "A" }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<string>(o => o.StringProperty, "B"); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal("B", doc.StringProperty); }
public void AddToList() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 0); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList); }
public void AddResultsShouldReplaceWithSerialization() { // Arrange var doc = new SimpleDTO() { StringProperty = "A" }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<string>(o => o.StringProperty, "B"); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal("B", doc.StringProperty); }
public void AddToListWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 0); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList); }
public void MoveToEndOfList() { // Arrange var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<int>(o => o.IntegerValue, o => o.IntegerList); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(0, doc.IntegerValue); Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList); }
public void RemoveFromListInvalidPositionTooSmallWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList, -1); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act & Assert var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); }
public void AddToListInvalidPositionTooSmall_LogsError() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, -1); var logger = new TestErrorLogger<SimpleDTO>(); patchDoc.ApplyTo(doc, logger.LogErrorMessage); // Assert Assert.Equal( "For operation 'add' on array property at path '/integerlist/-1', the index is negative.", logger.ErrorMessage); }
public void ReplaceFullListFromEnumerable() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<IEnumerable<int>>(o => o.IntegerList, new List<int>() { 4, 5, 6 }); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList); }
public void SerializeAndReplaceGuidTest() { // Arrange var doc = new SimpleDTO() { GuidValue = Guid.NewGuid() }; var newGuid = Guid.NewGuid(); // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace(o => o.GuidValue, newGuid); // serialize & deserialize var serialized = JsonConvert.SerializeObject(patchDoc); var deserizalized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act deserizalized.ApplyTo(doc); // Assert Assert.Equal(newGuid, doc.GuidValue); }
public void Replace() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10 }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<string>(o => o.StringProperty, "B"); patchDoc.Replace(o => o.DecimalValue, 12); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal("B", doc.StringProperty); Assert.Equal(12, doc.DecimalValue); }
public void SerializationMustNotIncudeEnvelope() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch var 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); // Act var serialized = JsonConvert.SerializeObject(patchDoc); // Assert Assert.Equal(false, serialized.Contains("operations")); Assert.Equal(false, serialized.Contains("Operations")); }
public void MoveFomListToNonList() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<int>(o => o.IntegerList, 0, o => o.IntegerValue); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 2, 3 }, doc.IntegerList); Assert.Equal(1, doc.IntegerValue); }
public void RemoveFromListInvalidPositionTooSmall_LogsError() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList, -1); var logger = new TestErrorLogger<SimpleDTO>(); // Act patchDoc.ApplyTo(doc, logger.LogErrorMessage); // Assert Assert.Equal("Property does not exist at path '/integerlist/-1'.", logger.ErrorMessage); }
public void Copy() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", AnotherStringProperty = "B" }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<string>(o => o.StringProperty, o => o.AnotherStringProperty); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal("A", doc.AnotherStringProperty); }
public void MoveWithSerialization() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", AnotherStringProperty = "B" }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Move<string>(o => o.StringProperty, o => o.AnotherStringProperty); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal("A", doc.AnotherStringProperty); Assert.Equal(null, doc.StringProperty); }
public void AddToListInvalidPositionTooLarge() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, 4); // Act & Assert var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); Assert.Equal( "For operation 'add' on array property at path '/integerlist/4', the index is " + "larger than the array size.", exception.Message); }
public void CopyFromNonListToList() { // Arrange var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Copy<int>(o => o.IntegerValue, o => o.IntegerList, 0); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList); }
public void CopyFromListToNonListWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var 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); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal(1, doc.IntegerValue); }
public void RemoveFromEndOfList() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 1, 2 }, doc.IntegerList); }
public void MoveToEndOfListWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var 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); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal(0, doc.IntegerValue); Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList); }
public void ReplaceWithSerialization() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10 }; // create patch var 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); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal("B", doc.StringProperty); Assert.Equal(12, doc.DecimalValue); }
public void AddToListInvalidPositionTooSmall() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, -1); // Act & Assert var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { patchDoc.ApplyTo(doc); }); Assert.Equal("Property does not exist at path '/integerlist/-1'.", exception.Message); }
public void DeserializationMustWorkWithoutEnvelope() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch var 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); // Act var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Assert Assert.IsType<JsonPatchDocument<SimpleDTO>>(deserialized); }
public void Remove() { // Arrange var doc = new SimpleDTO() { StringProperty = "A" }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<string>(o => o.StringProperty); // Act patchDoc.ApplyTo(doc); // Assert Assert.Equal(null, doc.StringProperty); }
public void SerializationTests() { // Arrange var doc = new SimpleDTO() { StringProperty = "A", DecimalValue = 10, DoubleValue = 10, FloatValue = 10, IntegerValue = 10 }; // create patch var 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); // Act deserizalized.ApplyTo(doc); // Assert 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 RemoveFromListInvalidPositionTooLargeWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList, 3); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act & Assert var exception = Assert.Throws<JsonPatchException<SimpleDTO>>(() => { deserialized.ApplyTo(doc); }); Assert.Equal( "For operation 'remove' on array property at path '/integerlist/3', the index is " + "larger than the array size.", exception.Message); }
public void SerializeAndReplaceNestedObjectTest() { // Arrange var doc = new SimpleDTOWithNestedDTO() { SimpleDTO = new SimpleDTO() { IntegerValue = 5, IntegerList = new List<int>() { 1, 2, 3 } } }; var newDTO = new SimpleDTO() { DoubleValue = 1 }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>(); patchDoc.Replace(o => o.SimpleDTO, newDTO); // serialize & deserialize var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal(1, doc.SimpleDTO.DoubleValue); Assert.Equal(0, doc.SimpleDTO.IntegerValue); Assert.Equal(null, doc.SimpleDTO.IntegerList); }
public void RemoveFromListInvalidPositionTooLarge_LogsError() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Remove<int>(o => o.IntegerList, 3); var logger = new TestErrorLogger<SimpleDTO>(); // Act patchDoc.ApplyTo(doc, logger.LogErrorMessage); // Assert Assert.Equal( "For operation 'remove' on array property at path '/integerlist/3', the index is " + "larger than the array size.", logger.ErrorMessage); }
public void ReplaceFullListFromEnumerableWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Replace<IEnumerable<int>>(o => o.IntegerList, new List<int>() { 4, 5, 6 }); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act deserialized.ApplyTo(doc); // Assert Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList); }
public void AddToListInvalidPositionTooSmallWithSerialization() { // Arrange var doc = new SimpleDTO() { IntegerList = new List<int>() { 1, 2, 3 } }; // create patch var patchDoc = new JsonPatchDocument<SimpleDTO>(); patchDoc.Add<int>(o => o.IntegerList, 4, -1); var serialized = JsonConvert.SerializeObject(patchDoc); var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTO>>(serialized); // Act & Assert var exception = Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); }); Assert.Equal( "For operation 'add' on array property at path '/integerlist/-1', the index is negative.", exception.Message); }