public void PassNotEnumPropertyoStructuralTypeConfigurationAddEnumPropertyShouldThrowException() { // Arrange var builder = new ODataModelBuilder(); builder.ComplexType <EntityTypeWithEnumTypePropertyTestModel>(); StructuralTypeConfiguration structuralTypeConfiguration = builder.StructuralTypes.Single(); Expression <Func <EntityTypeWithEnumTypePropertyTestModel, int> > propertyExpression = e => e.ID; PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression); // Act & Assert Assert.ThrowsArgument( () => structuralTypeConfiguration.AddEnumProperty(propertyInfo), "propertyInfo", "The property 'ID' on type 'System.Web.OData.Builder.EntityTypeWithEnumTypePropertyTestModel' must be an Enum property."); }
public void PassNotExistingPropertyoStructuralTypeConfigurationAddEnumPropertyShouldThrowException() { // Arrange var builder = new ODataModelBuilder(); builder.ComplexType <ComplexTypeWithEnumTypePropertyTestModel>(); StructuralTypeConfiguration structuralTypeConfiguration = builder.StructuralTypes.Single(); Expression <Func <EntityTypeWithEnumTypePropertyTestModel, Color> > propertyExpression = e => e.RequiredColor; PropertyInfo propertyInfo = PropertySelectorVisitor.GetSelectedProperty(propertyExpression); // Act & Assert Assert.ThrowsArgument( () => structuralTypeConfiguration.AddEnumProperty(propertyInfo), "propertyInfo", "The property 'RequiredColor' does not belong to the type 'System.Web.OData.Builder.ComplexTypeWithEnumTypePropertyTestModel'."); }
private void MapStructuralProperty(StructuralTypeConfiguration type, PropertyInfo property, PropertyKind propertyKind, bool isCollection) { Contract.Assert(type != null); Contract.Assert(property != null); Contract.Assert(propertyKind == PropertyKind.Complex || propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Enum); bool addedExplicitly = type.Properties.Any(p => p.PropertyInfo.Name == property.Name); PropertyConfiguration addedEdmProperty; if (!isCollection) { if (propertyKind == PropertyKind.Primitive) { addedEdmProperty = type.AddProperty(property); } else if (propertyKind == PropertyKind.Enum) { AddEnumType(TypeHelper.GetUnderlyingTypeOrSelf(property.PropertyType)); addedEdmProperty = type.AddEnumProperty(property); } else { addedEdmProperty = type.AddComplexProperty(property); } } else { if (_isQueryCompositionMode) { Contract.Assert(propertyKind != PropertyKind.Complex, "we don't create complex types in query composition mode."); } if (property.PropertyType.IsGenericType) { Type elementType = property.PropertyType.GetGenericArguments().First(); Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType); if (elementUnderlyingTypeOrSelf.IsEnum) { AddEnumType(elementUnderlyingTypeOrSelf); } } else { Type elementType; if (property.PropertyType.IsCollection(out elementType)) { Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType); if (elementUnderlyingTypeOrSelf.IsEnum) { AddEnumType(elementUnderlyingTypeOrSelf); } } } addedEdmProperty = type.AddCollectionProperty(property); } addedEdmProperty.AddedExplicitly = addedExplicitly; }