public void BuildPropertyValidator_with_buildFacetValidators_set_to_false_returns_null_for_a_scalar_property_with_facet_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
            .Setup <IEnumerable <IValidator> >(
                "BuildFacetValidators", ItExpr.IsAny <PropertyInfo>(),
                ItExpr.IsAny <EdmMember>(), ItExpr.IsAny <IEnumerable <Attribute> >())
            .Returns <PropertyInfo, EdmMember, IEnumerable <Attribute> >(
                (pi, e, a) => new[]
            {
                new ValidationAttributeValidator(new RequiredAttribute(), null)
            });

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validator = builder.Object.BuildPropertyValidatorBase(
                    entity.GetType().GetDeclaredProperty("ID"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ID").Single(),
                    false);

                Assert.Null(validator);
            }
        }
        public void BuildPropertyValidator_does_not_return_null_for_a_complex_property_with_attribute_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
            .Setup <IList <IValidator> >("BuildValidationAttributeValidators", ItExpr.IsAny <IEnumerable <Attribute> >())
            .Returns <IEnumerable <Attribute> >(
                a => new List <IValidator>
            {
                new ValidationAttributeValidator(new RequiredAttribute(), null)
            });

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validator = builder.Object.BuildPropertyValidatorBase(
                    entity.GetType().GetDeclaredProperty("ComplexProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                    true);

                Assert.NotNull(validator);
                Assert.IsType <ComplexPropertyValidator>(validator);
                Assert.Equal("ComplexProperty", validator.PropertyName);
                Assert.Equal(1, validator.PropertyAttributeValidators.Count());
                Assert.Null(((ComplexPropertyValidator)validator).ComplexTypeValidator);
            }
        }
        public void BuildValidatorsForProperties_calls_BuildPropertyValidator_for_a_scalar_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
            .Setup <PropertyValidator>("BuildPropertyValidator", ItExpr.IsAny <PropertyInfo>())
            .Throws <XunitException>();

            builder.Protected()
            .Setup <PropertyValidator>(
                "BuildPropertyValidator", ItExpr.IsAny <PropertyInfo>(), ItExpr.IsAny <EdmProperty>(), ItExpr.IsAny <bool>())
            .Returns <PropertyInfo, EdmProperty, bool>(
                (pi, e, f) => new PropertyValidator(
                    "ID", new[]
            {
                new ValidationAttributeValidator(new RequiredAttribute(), null)
            }));

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildValidatorsForPropertiesBase(
                    new[] { entity.GetType().GetDeclaredProperty("ID") },
                    new[] { ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ID").Single() },
                    new NavigationProperty[0]);

                Assert.Equal(1, validators.Count);
            }
        }
        public void BuildPropertyValidator_does_not_return_null_for_a_complex_property_with_type_level_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
            .Setup <ComplexTypeValidator>("BuildComplexTypeValidator", ItExpr.IsAny <Type>(), ItExpr.IsAny <ComplexType>())
            .Returns <Type, ComplexType>(
                (t, c) => new ComplexTypeValidator(new PropertyValidator[0], new ValidationAttributeValidator[0]));

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validator = builder.Object.BuildPropertyValidatorBase(
                    entity.GetType().GetDeclaredProperty("ComplexProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                    false);

                Assert.NotNull(validator);
                Assert.IsType <ComplexPropertyValidator>(validator);
                Assert.Equal("ComplexProperty", validator.PropertyName);
                Assert.Equal(0, validator.PropertyAttributeValidators.Count());
                Assert.NotNull(((ComplexPropertyValidator)validator).ComplexTypeValidator);
            }
        }
Beispiel #5
0
        public void BuildPropertyValidator_returns_null_for_a_complex_property_with_no_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                Assert.Null(
                    builder.Object.BuildPropertyValidatorBase(
                        entity.GetType().GetDeclaredProperty("ComplexProperty"),
                        ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                        true));
            }
        }
Beispiel #6
0
        public void BuildFacetValidators_returns_empty_for_required_property_with_ValidationAttribute()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetDeclaredProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new[] { new RequiredAttribute() });

                Assert.False(validators.Any());
            }
        }
Beispiel #7
0
        public void BuildValidatorsForProperties_returns_empty_if_no_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildValidatorsForPropertiesBase(
                    entity.GetType().GetRuntimeProperties().Where(p => p.IsPublic()),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties,
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties);

                Assert.Equal(0, validators.Count);
            }
        }
        public void BuildFacetValidators_returns_a_validator_for_required_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new Attribute[0]);

                Assert.Equal(1, validators.Count());
            }
        }
        public void BuildFacetValidators_returns_empty_for_required_value_type_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetProperty("NonNullableProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "NonNullableProperty").Single(),
                    new Attribute[0]);

                Assert.False(validators.Any());
            }
        }
Beispiel #10
0
        public void BuildFacetValidators_returns_empty_for_optional_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();

            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetDeclaredProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new Attribute[0]);

                Assert.False(validators.Any());
            }
        }
        public void BuildFacetValidators_returns_empty_for_optional_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new Attribute[0]);

                Assert.False(validators.Any());
            }
        }
        public void BuildFacetValidators_returns_empty_for_required_value_type_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetDeclaredProperty("NonNullableProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "NonNullableProperty").Single(),
                    new Attribute[0]);

                Assert.False(validators.Any());
            }
        }
        public void BuildValidatorsForProperties_returns_empty_if_no_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildValidatorsForPropertiesBase(
                    entity.GetType().GetProperties(),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties,
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties);

                Assert.Equal(0, validators.Count);
            }
        }
        public void BuildValidatorsForProperties_calls_BuildPropertyValidator_for_a_scalar_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();
            builder.Protected()
                .Setup<PropertyValidator>("BuildPropertyValidator", ItExpr.IsAny<PropertyInfo>())
                .Throws<AssertException>();

            builder.Protected()
                .Setup<PropertyValidator>(
                    "BuildPropertyValidator", ItExpr.IsAny<PropertyInfo>(), ItExpr.IsAny<EdmProperty>(), ItExpr.IsAny<bool>())
                .Returns<PropertyInfo, EdmProperty, bool>(
                    (pi, e, f) => new PropertyValidator(
                                      "ID", new[]
                                                {
                                                    new ValidationAttributeValidator(new RequiredAttribute(), null)
                                                }));

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildValidatorsForPropertiesBase(
                    new[] { entity.GetType().GetProperty("ID") },
                    new[] { ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ID").Single() },
                    new NavigationProperty[0]);

                Assert.Equal(1, validators.Count);
            }
        }
        public void BuildPropertyValidator_returns_null_for_a_complex_property_with_no_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                Assert.Null(
                    builder.Object.BuildPropertyValidatorBase(
                        entity.GetType().GetProperty("ComplexProperty"),
                        ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                        true));
            }
        }
        public void BuildFacetValidators_returns_a_validator_for_required_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetDeclaredProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new Attribute[0]);

                Assert.Equal(1, validators.Count());
            }
        }
        public void BuildPropertyValidator_does_not_return_null_for_a_complex_property_with_type_level_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
                .Setup<ComplexTypeValidator>("BuildComplexTypeValidator", ItExpr.IsAny<Type>(), ItExpr.IsAny<ComplexType>())
                .Returns<Type, ComplexType>(
                    (t, c) => new ComplexTypeValidator(new PropertyValidator[0], new ValidationAttributeValidator[0]));

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validator = builder.Object.BuildPropertyValidatorBase(
                    entity.GetType().GetProperty("ComplexProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                    false);

                Assert.NotNull(validator);
                Assert.IsType<ComplexPropertyValidator>(validator);
                Assert.Equal("ComplexProperty", validator.PropertyName);
                Assert.Equal(0, validator.PropertyAttributeValidators.Count());
                Assert.NotNull(((ComplexPropertyValidator)validator).ComplexTypeValidator);
            }
        }
        public void BuildPropertyValidator_does_not_return_null_for_a_complex_property_with_attribute_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            builder.Protected()
                .Setup<IList<IValidator>>("BuildValidationAttributeValidators", ItExpr.IsAny<IEnumerable<Attribute>>())
                .Returns<IEnumerable<Attribute>>(
                    a => new List<IValidator>
                             {
                                 new ValidationAttributeValidator(new RequiredAttribute(), null)
                             });

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validator = builder.Object.BuildPropertyValidatorBase(
                    entity.GetType().GetProperty("ComplexProperty"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                    true);

                Assert.NotNull(validator);
                Assert.IsType<ComplexPropertyValidator>(validator);
                Assert.Equal("ComplexProperty", validator.PropertyName);
                Assert.Equal(1, validator.PropertyAttributeValidators.Count());
                Assert.Null(((ComplexPropertyValidator)validator).ComplexTypeValidator);
            }
        }
        public void BuildPropertyValidator_returns_null_for_a_complex_property_with_facet_validation()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();
            builder.Protected()
                .Setup<IEnumerable<IValidator>>(
                    "BuildFacetValidators", ItExpr.IsAny<PropertyInfo>(), ItExpr.IsAny<EdmMember>(), ItExpr.IsAny<IEnumerable<Attribute>>())
                .Returns<PropertyInfo, EdmMember, IEnumerable<Attribute>>(
                    (pi, e, a) => new[]
                                      {
                                          new ValidationAttributeValidator(new RequiredAttribute(), null)
                                      });

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                Assert.Null(
                    builder.Object.BuildPropertyValidatorBase(
                        entity.GetType().GetProperty("ComplexProperty"),
                        ctx.Entry(entity).InternalEntry.EdmEntityType.Properties.Where(p => p.Name == "ComplexProperty").Single(),
                        true));
            }
        }
        public void BuildFacetValidators_returns_empty_for_required_property_with_ValidationAttribute()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();

            object entity = new EntityWithComplexType();
            using (var ctx = new ConfigurationOverridesContext(entity))
            {
                var validators = builder.Object.BuildFacetValidatorsBase(
                    entity.GetType().GetProperty("Self"),
                    ctx.Entry(entity).InternalEntry.EdmEntityType.NavigationProperties.Where(p => p.Name == "Self").Single(),
                    new[] { new RequiredAttribute() });

                Assert.False(validators.Any());
            }
        }
        public void BuildValidatorsForProperties_calls_BuildPropertyValidator_for_a_transient_property()
        {
            var builder = MockHelper.CreateMockEntityValidatorBuilder();
            builder.Protected()
                .Setup<PropertyValidator>("BuildPropertyValidator", ItExpr.IsAny<PropertyInfo>())
                .Returns<PropertyInfo>(
                    pi => new PropertyValidator(
                              "ID", new[]
                                        {
                                            new ValidationAttributeValidator(new RequiredAttribute(), null)
                                        }));

            builder.Protected()
                .Setup<PropertyValidator>(
                    "BuildPropertyValidator", ItExpr.IsAny<PropertyInfo>(), ItExpr.IsAny<EdmProperty>(), ItExpr.IsAny<bool>())
                .Throws<AssertException>();

            object entity = new EntityWithComplexType();
            using (var ctx = new SelfPopulatingContext(entity))
            {
                var validators = builder.Object.BuildValidatorsForPropertiesBase(
                    new[] { entity.GetType().GetDeclaredProperty("ID") },
                    new EdmProperty[0],
                    new NavigationProperty[0]);

                Assert.Equal(1, validators.Count);
            }
        }