Exemplo n.º 1
0
        public void TryTest_DoesNotThrowException_IfTestSuccessful()
        {
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };

            // Act
            var testStatus = adapter.TryTest(model, "Name", contractResolver, "Joana", out var errorMessage);

            // Assert
            Assert.Equal("Joana", model.Name);
            Assert.True(testStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
Exemplo n.º 2
0
        public void TryRemove_SetsPropertyToNull()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };

            // Act
            var removeStatus = adapter.TryRemove(model, "Name", contractResolver, out var errorMessage);

            // Assert
            Assert.Null(model.Name);
            Assert.True(removeStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
Exemplo n.º 3
0
        public void TryGet_ExistingProperty()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };

            // Act
            var getStatus = adapter.TryGet(model, "Name", contractResolver, out var value, out var errorMessage);

            // Assert
            Assert.Equal("Joana", value);
            Assert.True(getStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
Exemplo n.º 4
0
        public void TryAdd_ThrowsJsonPatchException_IfPropertyDoesNotExist()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };
            var expectedErrorMessage = "The target location specified by path segment 'LastName' was not found.";

            // Act
            var addStatus = adapter.TryAdd(model, "LastName", contractResolver, "Smith", out var errorMessage);

            // Assert
            Assert.False(addStatus);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }
Exemplo n.º 5
0
        public void TryTest_ThrowsJsonPatchException_IfTestFails()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };
            var expectedErrorMessage = "The current value 'Joana' at path 'Name' is not equal to the test value 'John'.";

            // Act
            var testStatus = adapter.TryTest(model, "Name", contractResolver, "John", out var errorMessage);

            // Assert
            Assert.False(testStatus);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }
Exemplo n.º 6
0
        public void TryReplace_OverwritesExistingValue()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Name = "Joana"
            };

            // Act
            var replaceStatus = adapter.TryReplace(model, "Name", contractResolver, "John", out var errorMessage);

            // Assert
            Assert.Equal("John", model.Name);
            Assert.True(replaceStatus);
            Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
        }
Exemplo n.º 7
0
        public void TryReplace_ThrowsJsonPatchException_IfNewValueIsInvalidType()
        {
            // Arrange
            var adapter          = new PocoAdapter();
            var contractResolver = new DefaultContractResolver();
            var model            = new Customer
            {
                Age = 25
            };

            var expectedErrorMessage = "The value 'TwentySix' is invalid for target location.";

            // Act
            var replaceStatus = adapter.TryReplace(model, "Age", contractResolver, "TwentySix", out var errorMessage);

            // Assert
            Assert.Equal(25, model.Age);
            Assert.False(replaceStatus);
            Assert.Equal(expectedErrorMessage, errorMessage);
        }