Exemple #1
0
        public void GetAttributesForTestProperty_ModelOverridesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(BaseViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == nameof(BaseModel.TestProperty));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            var rangeAttributes = attributes.Attributes.OfType <RangeAttribute>().ToArray();

            Assert.NotNull(rangeAttributes[0]);
            Assert.Equal(0, (int)rangeAttributes[0].Minimum);
            Assert.Equal(10, (int)rangeAttributes[0].Maximum);
            Assert.NotNull(rangeAttributes[1]);
            Assert.Equal(10, (int)rangeAttributes[1].Minimum);
            Assert.Equal(100, (int)rangeAttributes[1].Maximum);
            Assert.Single(attributes.Attributes.OfType <FromHeaderAttribute>());

            rangeAttributes = attributes.PropertyAttributes.OfType <RangeAttribute>().ToArray();
            Assert.NotNull(rangeAttributes[0]);
            Assert.Equal(0, (int)rangeAttributes[0].Minimum);
            Assert.Equal(10, (int)rangeAttributes[0].Maximum);
            Assert.NotNull(rangeAttributes[1]);
            Assert.Equal(10, (int)rangeAttributes[1].Minimum);
            Assert.Equal(100, (int)rangeAttributes[1].Maximum);
            Assert.Single(attributes.PropertyAttributes.OfType <FromHeaderAttribute>());
        }
Exemple #2
0
        public void GetAttributesForBasePropertyFromDerivedModel_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == nameof(BaseModel.BaseProperty));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.Attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.Attributes.OfType <StringLengthAttribute>());

            Assert.Single(attributes.PropertyAttributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.PropertyAttributes.OfType <StringLengthAttribute>());
        }
Exemple #3
0
        public void GetFromServiceAttributeFromBase_IncludesMetadataAttributes()
        {
            // Arrange
            var modelType = typeof(DerivedViewModel);
            var property  = modelType.GetRuntimeProperties().FirstOrDefault(p => p.Name == nameof(BaseModel.RouteValue));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(modelType, property);

            // Assert
            Assert.Single(attributes.Attributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.Attributes.OfType <FromRouteAttribute>());

            Assert.Single(attributes.PropertyAttributes.OfType <RequiredAttribute>());
            Assert.Single(attributes.PropertyAttributes.OfType <FromRouteAttribute>());
        }
Exemple #4
0
        public void GetAttributesForProperty_MergedAttributes()
        {
            // Arrange
            var property = typeof(MergedAttributes).GetRuntimeProperty(nameof(MergedAttributes.Property));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(typeof(MergedAttributes), property);

            // Assert
            Assert.Equal(3, attributes.Attributes.Count);
            Assert.IsType <RequiredAttribute>(attributes.Attributes[0]);
            Assert.IsType <RangeAttribute>(attributes.Attributes[1]);
            Assert.IsType <ClassValidator>(attributes.Attributes[2]);

            Assert.Equal(2, attributes.PropertyAttributes.Count);
            Assert.IsType <RequiredAttribute>(attributes.PropertyAttributes[0]);
            Assert.IsType <RangeAttribute>(attributes.PropertyAttributes[1]);

            var attribute = Assert.Single(attributes.TypeAttributes);

            Assert.IsType <ClassValidator>(attribute);
        }
Exemple #5
0
        public void GetAttributesForProperty_WithModelType_IncludesTypeAttributes()
        {
            // Arrange
            var property = typeof(MergedAttributes)
                           .GetProperty(nameof(MergedAttributes.BaseModel));

            // Act
            var attributes = ModelAttributes.GetAttributesForProperty(typeof(MergedAttributes), property, typeof(DerivedModelWithAttributes));

            // Assert
            Assert.Collection(
                attributes.Attributes,
                attribute => Assert.IsType <BindRequiredAttribute>(attribute),
                attribute => Assert.IsType <ModelBinderAttribute>(attribute),
                attribute => Assert.IsType <ClassValidator>(attribute));
            Assert.IsType <BindRequiredAttribute>(Assert.Single(attributes.PropertyAttributes));
            Assert.Null(attributes.ParameterAttributes);
            Assert.Collection(
                attributes.TypeAttributes,
                attribute => Assert.IsType <ModelBinderAttribute>(attribute),
                attribute => Assert.IsType <ClassValidator>(attribute));
        }