public void ProcessResults_Success()
        {
            // Arrange
            var dob = new DateTime(2001, 1, 1);
            var model = new PersonWithBindExclusion
            {
                DateOfBirth = dob
            };
            var containerMetadata = GetMetadataForType(model.GetType());

            var bindingContext = CreateContext(containerMetadata, model);
            var results = containerMetadata.Properties.ToDictionary(
                property => property,
                property => new ModelBindingResult(model: null, key: property.PropertyName, isModelSet: false));

            var firstNameProperty = containerMetadata.Properties[nameof(model.FirstName)];
            results[firstNameProperty] = new ModelBindingResult(
                model: "John",
                isModelSet: true,
                key: nameof(model.FirstName));

            var lastNameProperty = containerMetadata.Properties[nameof(model.LastName)];
            results[lastNameProperty] = new ModelBindingResult(
                model: "Doe",
                isModelSet: true,
                key: nameof(model.LastName));

            var modelValidationNode = new ModelValidationNode(string.Empty, containerMetadata, model);
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.ProcessResults(bindingContext, results, modelValidationNode);

            // Assert
            Assert.Equal("John", model.FirstName);
            Assert.Equal("Doe", model.LastName);
            Assert.Equal(dob, model.DateOfBirth);
            Assert.True(bindingContext.ModelState.IsValid);

            // Ensure that we add child nodes for all the nodes which have a result (irrespective of if they
            // are bound or not).
            Assert.Equal(5, modelValidationNode.ChildNodes.Count);

            Assert.Collection(modelValidationNode.ChildNodes,
                child =>
                {
                    Assert.Equal(nameof(model.DateOfBirth), child.Key);
                    Assert.Equal(null, child.Model);
                },
                child =>
                {
                    Assert.Equal(nameof(model.DateOfDeath), child.Key);
                    Assert.Equal(null, child.Model);
                },
                child =>
                {
                    Assert.Equal(nameof(model.FirstName), child.Key);
                    Assert.Equal("John", child.Model);
                },
                child =>
                {
                    Assert.Equal(nameof(model.LastName), child.Key);
                    Assert.Equal("Doe", child.Model);
                },
                child =>
                {
                    Assert.Equal(nameof(model.NonUpdateableProperty), child.Key);
                    Assert.Equal(null, child.Model);
                });
        }
        public void ProcessResults_Success()
        {
            // Arrange
            var dob = new DateTime(2001, 1, 1);
            var model = new PersonWithBindExclusion
            {
                DateOfBirth = dob
            };
            var containerMetadata = GetMetadataForType(model.GetType());

            var bindingContext = CreateContext(containerMetadata, model);
            var results = containerMetadata.Properties.ToDictionary(
                property => property,
                property => ModelBindingResult.Failed(property.PropertyName));

            var firstNameProperty = containerMetadata.Properties[nameof(model.FirstName)];
            results[firstNameProperty] = ModelBindingResult.Success(
                nameof(model.FirstName),
                "John");

            var lastNameProperty = containerMetadata.Properties[nameof(model.LastName)];
            results[lastNameProperty] = ModelBindingResult.Success(
                nameof(model.LastName),
                "Doe");

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.ProcessResults(bindingContext, results);

            // Assert
            Assert.Equal("John", model.FirstName);
            Assert.Equal("Doe", model.LastName);
            Assert.Equal(dob, model.DateOfBirth);
            Assert.True(bindingContext.ModelState.IsValid);
        }
        public void ProcessDto_Success()
        {
            // Arrange
            var dob = new DateTime(2001, 1, 1);
            var model = new PersonWithBindExclusion
            {
                DateOfBirth = dob
            };
            var containerMetadata = GetMetadataForType(model.GetType());

            var bindingContext = CreateContext(containerMetadata, model);
            var dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);

            var firstNameProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "FirstName");
            dto.Results[firstNameProperty] = new ModelBindingResult(
                "John",
                isModelSet: true,
                key: "");

            var lastNameProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "LastName");
            dto.Results[lastNameProperty] = new ModelBindingResult(
                "Doe",
                isModelSet: true,
                key: "");

            var dobProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "DateOfBirth");
            dto.Results[dobProperty] = null;
            var modelValidationNode = new ModelValidationNode(string.Empty, containerMetadata, model);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.ProcessDto(bindingContext, dto, modelValidationNode);

            // Assert
            Assert.Equal("John", model.FirstName);
            Assert.Equal("Doe", model.LastName);
            Assert.Equal(dob, model.DateOfBirth);
            Assert.True(bindingContext.ModelState.IsValid);

            // Ensure that we add child nodes for all the nodes which have a result (irrespective of if they
            // are bound or not).
            Assert.Equal(2, modelValidationNode.ChildNodes.Count());

            var validationNode = modelValidationNode.ChildNodes[0];
            Assert.Equal("", validationNode.Key);
            Assert.Equal("John", validationNode.Model);

            validationNode = modelValidationNode.ChildNodes[1];
            Assert.Equal("", validationNode.Key);
            Assert.Equal("Doe", validationNode.Model);
        }
Exemplo n.º 4
0
        public async Task BindModelAsync_Success()
        {
            // Arrange
            var dob = new DateTime(2001, 1, 1);
            var model = new PersonWithBindExclusion
            {
                DateOfBirth = dob
            };

            var containerMetadata = GetMetadataForType(model.GetType());

            var bindingContext = CreateContext(containerMetadata, model);

            var binder = CreateBinder(bindingContext.ModelMetadata);

            foreach (var property in containerMetadata.Properties)
            {
                binder.Results[property] = ModelBindingResult.Failed();
            }

            var firstNameProperty = containerMetadata.Properties[nameof(model.FirstName)];
            binder.Results[firstNameProperty] = ModelBindingResult.Success("John");

            var lastNameProperty = containerMetadata.Properties[nameof(model.LastName)];
            binder.Results[lastNameProperty] = ModelBindingResult.Success("Doe");

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Equal("John", model.FirstName);
            Assert.Equal("Doe", model.LastName);
            Assert.Equal(dob, model.DateOfBirth);
            Assert.True(bindingContext.ModelState.IsValid);
        }
        public void ProcessDto_Success()
        {
            // Arrange
            var dob = new DateTime(2001, 1, 1);
            var model = new PersonWithBindExclusion
            {
                DateOfBirth = dob
            };
            var containerMetadata = GetMetadataForType(model.GetType());

            var bindingContext = CreateContext(containerMetadata, model);
            var dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);

            var firstNameProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "FirstName");
            dto.Results[firstNameProperty] = new ModelBindingResult(
                "John",
                isModelSet: true,
                key: "");

            var lastNameProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "LastName");
            dto.Results[lastNameProperty] = new ModelBindingResult(
                "Doe",
                isModelSet: true,
                key: "");

            var dobProperty = dto.PropertyMetadata.Single(o => o.PropertyName == "DateOfBirth");
            dto.Results[dobProperty] = null;

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.ProcessDto(bindingContext, dto);

            // Assert
            Assert.Equal("John", model.FirstName);
            Assert.Equal("Doe", model.LastName);
            Assert.Equal(dob, model.DateOfBirth);
            Assert.True(bindingContext.ModelState.IsValid);
        }