public void BuildComplexTypeValidator_does_not_return_null_for_a_complex_type_with_entity_level_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 complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; var validator = builder.Object.BuildComplexTypeValidatorBase(typeof(ComplexTypeWithNoValidation), complexType); Assert.NotNull(validator); Assert.Equal(0, validator.PropertyValidators.Count()); Assert.Equal(1, validator.TypeLevelValidators.Count()); } }
public void BuildEntityValidator_does_not_return_null_for_an_entity_with_property_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); builder.Protected() .Setup <IList <PropertyValidator> >( "BuildValidatorsForProperties", ItExpr.IsAny <IEnumerable <PropertyInfo> >(), ItExpr.IsAny <IEnumerable <EdmProperty> >(), ItExpr.IsAny <IEnumerable <NavigationProperty> >()) .Returns <IEnumerable <PropertyInfo>, IEnumerable <EdmProperty>, IEnumerable <NavigationProperty> >( (pi, e, n) => new List <PropertyValidator> { new PropertyValidator("foo", Enumerable.Empty <ValidationAttributeValidator>()) }); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { var validator = builder.Object.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry); Assert.NotNull(validator); Assert.Equal(1, validator.PropertyValidators.Count()); Assert.Equal(0, validator.TypeLevelValidators.Count()); } }
public void BuildComplexTypeValidator_does_not_return_null_for_a_complex_type_with_property_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); builder.Protected() .Setup <IList <PropertyValidator> >( "BuildValidatorsForProperties", ItExpr.IsAny <IEnumerable <PropertyInfo> >(), ItExpr.IsAny <IEnumerable <EdmProperty> >(), ItExpr.IsAny <IEnumerable <NavigationProperty> >()) .Returns <IEnumerable <PropertyInfo>, IEnumerable <EdmProperty>, IEnumerable <NavigationProperty> >( (pi, e, n) => new List <PropertyValidator> { new PropertyValidator("foo", Enumerable.Empty <IValidator>()) }); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { var complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; var validator = builder.Object.BuildComplexTypeValidatorBase(typeof(ComplexTypeWithNoValidation), complexType); Assert.NotNull(validator); Assert.Equal(1, validator.PropertyValidators.Count()); Assert.Equal(0, validator.TypeLevelValidators.Count()); } }
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 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().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_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 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); } }
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().GetProperty("ID") }, new EdmProperty[0], new NavigationProperty[0]); Assert.Equal(1, validators.Count); } }
public void BuildEntityValidator_returns_null_for_an_entity_with_no_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { Assert.Null(builder.Object.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry)); } }
public void BuildComplexTypeValidator_returns_null_for_a_complex_type_with_no_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { var complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; Assert.Null(builder.Object.BuildComplexTypeValidatorBase(typeof(ComplexTypeWithNoValidation), complexType)); } }
public void BuildEntityValidator_does_not_return_null_for_an_IValidatableObject() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); object entity = new ValidatableEntity(); using (var ctx = new SelfPopulatingContext(entity)) { var validator = builder.Object.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry); Assert.NotNull(validator); Assert.False(validator.PropertyValidators.Any()); Assert.True(validator.TypeLevelValidators.Any()); } }
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)); } }
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 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 BuildComplexTypeValidator_does_not_return_null_for_an_IValidatableObject() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); object entity = new ValidatableEntity(); using (var ctx = new SelfPopulatingContext(entity)) { var complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; var validator = builder.Object.BuildComplexTypeValidatorBase(typeof(ValidatableComplexType), complexType); Assert.NotNull(validator); Assert.False(validator.PropertyValidators.Any()); Assert.True(validator.TypeLevelValidators.Any()); } }
public void BuildEntityValidator_does_not_return_null_for_an_entity_with_entity_level_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.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry); Assert.NotNull(validator); Assert.Equal(0, validator.PropertyValidators.Count()); Assert.Equal(1, validator.TypeLevelValidators.Count()); } }
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 BuildComplexTypeValidator_does_not_return_null_for_a_complex_type_with_property_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); builder.Protected() .Setup<IList<PropertyValidator>>( "BuildValidatorsForProperties", ItExpr.IsAny<IEnumerable<PropertyInfo>>(), ItExpr.IsAny<IEnumerable<EdmProperty>>(), ItExpr.IsAny<IEnumerable<NavigationProperty>>()) .Returns<IEnumerable<PropertyInfo>, IEnumerable<EdmProperty>, IEnumerable<NavigationProperty>>( (pi, e, n) => new List<PropertyValidator> { new PropertyValidator("foo", Enumerable.Empty<IValidator>()) }); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { var complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; var validator = builder.Object.BuildComplexTypeValidatorBase(typeof(ComplexTypeWithNoValidation), complexType); Assert.NotNull(validator); Assert.Equal(1, validator.PropertyValidators.Count()); Assert.Equal(0, validator.TypeLevelValidators.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 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 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_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 BuildEntityValidator_does_not_return_null_for_an_entity_with_entity_level_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.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry); Assert.NotNull(validator); Assert.Equal(0, validator.PropertyValidators.Count()); Assert.Equal(1, validator.TypeLevelValidators.Count()); } }
public void BuildEntityValidator_does_not_return_null_for_an_entity_with_property_validation() { var builder = MockHelper.CreateMockEntityValidatorBuilder(); builder.Protected() .Setup<IList<PropertyValidator>>( "BuildValidatorsForProperties", ItExpr.IsAny<IEnumerable<PropertyInfo>>(), ItExpr.IsAny<IEnumerable<EdmProperty>>(), ItExpr.IsAny<IEnumerable<NavigationProperty>>()) .Returns<IEnumerable<PropertyInfo>, IEnumerable<EdmProperty>, IEnumerable<NavigationProperty>>( (pi, e, n) => new List<PropertyValidator> { new PropertyValidator("foo", Enumerable.Empty<ValidationAttributeValidator>()) }); object entity = new EntityWithComplexType(); using (var ctx = new SelfPopulatingContext(entity)) { var validator = builder.Object.BuildEntityValidatorBase(ctx.Entry(entity).InternalEntry); Assert.NotNull(validator); Assert.Equal(1, validator.PropertyValidators.Count()); Assert.Equal(0, validator.TypeLevelValidators.Count()); } }
public void BuildComplexTypeValidator_does_not_return_null_for_a_complex_type_with_entity_level_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 complexType = (ComplexType)ctx.Entry(entity).InternalEntry.EdmEntityType.Properties .Where(p => p.Name == "ComplexProperty").Single().TypeUsage.EdmType; var validator = builder.Object.BuildComplexTypeValidatorBase(typeof(ComplexTypeWithNoValidation), complexType); Assert.NotNull(validator); Assert.Equal(0, validator.PropertyValidators.Count()); Assert.Equal(1, validator.TypeLevelValidators.Count()); } }
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); } }
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); } }