public void Property_NotFilterableAndNonFilterableAreEqual( bool? notFilterable, bool? nonFilterable, bool expectedResult) { // Arrange StructuralTypeConfiguration structuralType = Mock.Of<StructuralTypeConfiguration>(); Mock<PropertyInfo> propertyInfo = new Mock<PropertyInfo>(); propertyInfo.SetupGet(p => p.PropertyType).Returns(typeof(int)); PropertyConfiguration property = new PrimitivePropertyConfiguration(propertyInfo.Object, structuralType); // Act if (notFilterable.HasValue) { property.NotFilterable = notFilterable.Value; } if (nonFilterable.HasValue) { property.NonFilterable = nonFilterable.Value; } // Assert Assert.Equal(expectedResult, property.NotFilterable); Assert.Equal(expectedResult, property.NonFilterable); }
public void Keys_Returns_DeclaredKeys_IfNoBaseType() { // Arrange PrimitivePropertyConfiguration[] keys = new PrimitivePropertyConfiguration[0]; Mock<EntityTypeConfiguration> entity = new Mock<EntityTypeConfiguration>(); entity.Setup(e => e.BaseType).Returns<EntityTypeConfiguration>(null); entity.Setup(e => e.Keys).Returns(keys); // Act & Assert Assert.ReferenceEquals(keys, entity.Object.Keys()); }
public void Property_HasDefaultValueFalse_NotFilterableAndNonFilterable() { // Arrange StructuralTypeConfiguration structuralType = Mock.Of<StructuralTypeConfiguration>(); Mock<PropertyInfo> propertyInfo = new Mock<PropertyInfo>(); propertyInfo.SetupGet(p => p.PropertyType).Returns(typeof(int)); // Act PropertyConfiguration property = new PrimitivePropertyConfiguration(propertyInfo.Object, structuralType); // Assert Assert.False(property.NotFilterable); Assert.False(property.NonFilterable); }
public void AsDate_ThrowsArgument(Type propertyType) { // Arrange MockType type = new MockType().Property(propertyType, "Birthday"); PropertyInfo property = type.GetProperty("Birthday"); _structuralType.Setup(t => t.ClrType).Returns(type); // Act PrimitivePropertyConfiguration propertyConfig = new PrimitivePropertyConfiguration(property, _structuralType.Object); // Assert Assert.ThrowsArgument(() => propertyConfig.AsDate(), "property", "The property 'Birthday' on type 'NS.Customer' must be a System.DateTime property"); }
public void AsDate_Works() { // Arrange MockType type = new MockType().Property(typeof(DateTime), "Birthday"); PropertyInfo property = type.GetProperty("Birthday"); _structuralType.Setup(t => t.ClrType).Returns(type); // Act PrimitivePropertyConfiguration propertyConfig = new PrimitivePropertyConfiguration(property, _structuralType.Object); EdmPrimitiveTypeKind? typeKind = propertyConfig.AsDate().TargetEdmTypeKind; // Assert Assert.NotNull(typeKind); Assert.Equal(EdmPrimitiveTypeKind.Date, typeKind); }
public void Property_IsNotNavigable_SetsUnsortableAndNonFilterable() { // Arrange StructuralTypeConfiguration structuralType = Mock.Of<StructuralTypeConfiguration>(); Mock<PropertyInfo> propertyInfo = new Mock<PropertyInfo>(); propertyInfo.SetupGet(p => p.PropertyType).Returns(typeof(int)); PropertyConfiguration property = new PrimitivePropertyConfiguration(propertyInfo.Object, structuralType); // Act property.IsNotNavigable(); // Assert Assert.True(property.NonFilterable); Assert.True(property.Unsortable); }
/// <summary> /// If this primitive property is <see cref="System.TimeSpan"/>, this method will make the target /// Edm type kind as <see cref="TimeOfDay"/> /// </summary> /// <param name="property">Reference to the calling primitive property configuration.</param> /// <returns>Returns itself so that multiple calls can be chained.</returns> public static PrimitivePropertyConfiguration AsTimeOfDay(this PrimitivePropertyConfiguration property) { if (property == null) { throw Error.ArgumentNull("property"); } if (!TypeHelper.IsTimeSpan(property.RelatedClrType)) { throw Error.Argument("property", SRResources.MustBeTimeSpanProperty, property.PropertyInfo.Name, property.DeclaringType.FullName); } property.TargetEdmTypeKind = EdmPrimitiveTypeKind.TimeOfDay; return(property); }
public void Property_IsNotNavigable_SetsNotSortableAndNotFilterable() { // Arrange StructuralTypeConfiguration structuralType = Mock.Of <StructuralTypeConfiguration>(); Mock <PropertyInfo> propertyInfo = new Mock <PropertyInfo>(); propertyInfo.SetupGet(p => p.PropertyType).Returns(typeof(int)); PropertyConfiguration property = new PrimitivePropertyConfiguration(propertyInfo.Object, structuralType); // Act property.IsNotNavigable(); // Assert Assert.True(property.NotFilterable); Assert.True(property.NotSortable); }
public void AsDate_Works() { // Arrange MockType type = new MockType().Property(typeof(DateTime), "Birthday"); PropertyInfo property = type.GetProperty("Birthday"); _structuralType.Setup(t => t.ClrType).Returns(type); // Act PrimitivePropertyConfiguration propertyConfig = new PrimitivePropertyConfiguration(property, _structuralType.Object); EdmPrimitiveTypeKind? typeKind = propertyConfig.AsDate().TargetEdmTypeKind; // Assert Assert.NotNull(typeKind); Assert.Equal(EdmPrimitiveTypeKind.Date, typeKind); }
public void AsTimeOfDay_Works() { // Arrange MockType type = new MockType().Property(typeof(TimeSpan), "CreatedTime"); PropertyInfo property = type.GetProperty("CreatedTime"); _structuralType.Setup(t => t.ClrType).Returns(type); // Act PrimitivePropertyConfiguration propertyConfig = new PrimitivePropertyConfiguration(property, _structuralType.Object); EdmPrimitiveTypeKind? typeKind = propertyConfig.AsTimeOfDay().TargetEdmTypeKind; // Assert Assert.NotNull(typeKind); Assert.Equal(EdmPrimitiveTypeKind.TimeOfDay, typeKind); }
public void RemoveKey_Removes_KeyProperty() { // Arrange var builder = new ODataModelBuilder(); var motorcycle = builder.AddEntityType(typeof(Motorcycle)); PrimitivePropertyConfiguration modelProperty = motorcycle.AddProperty(typeof(Motorcycle).GetProperty("Model")); motorcycle.HasKey(typeof(Motorcycle).GetProperty("Model")); Assert.Equal(new[] { modelProperty }, motorcycle.Keys); // Act motorcycle.RemoveKey(modelProperty); // Assert Assert.Empty(motorcycle.Keys); }
/// <summary> /// Adds a primitive property to this edm type. /// </summary> /// <param name="propertyInfo">The property being added.</param> /// <returns>The <see cref="PrimitivePropertyConfiguration"/> so that the property can be configured further.</returns> public virtual PrimitivePropertyConfiguration AddProperty(PropertyInfo propertyInfo) { if (propertyInfo == null) { throw Error.ArgumentNull("propertyInfo"); } if (!propertyInfo.ReflectedType.IsAssignableFrom(ClrType)) { throw Error.Argument("propertyInfo", SRResources.PropertyDoesNotBelongToType, propertyInfo.Name, ClrType.FullName); } if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)) { throw Error.Argument("propertyInfo", SRResources.DateTimeTypePropertyNotSupported, propertyInfo.PropertyType.FullName, propertyInfo.Name, propertyInfo.DeclaringType.FullName, typeof(DateTimeOffset).FullName, typeof(ODataModelBuilder).FullName); } ValidatePropertyNotAlreadyDefinedInBaseTypes(propertyInfo); ValidatePropertyNotAlreadyDefinedInDerivedTypes(propertyInfo); // Remove from the ignored properties if (RemovedProperties.Contains(propertyInfo)) { RemovedProperties.Remove(propertyInfo); } PrimitivePropertyConfiguration propertyConfiguration = null; if (ExplicitProperties.ContainsKey(propertyInfo)) { propertyConfiguration = ExplicitProperties[propertyInfo] as PrimitivePropertyConfiguration; if (propertyConfiguration == null) { throw Error.Argument("propertyInfo", SRResources.MustBePrimitiveProperty, propertyInfo.Name, ClrType.FullName); } } else { propertyConfiguration = new PrimitivePropertyConfiguration(propertyInfo, this); ExplicitProperties[propertyInfo] = propertyConfiguration; } return propertyConfiguration; }
public void CanAddPrimitiveProperty_ForTargetEntityType() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); // Act builder.EntityType <ForeignEntity>().HasRequired(c => c.Principal, (c, r) => c.ForeignKey1 == r.PrincipalKey1); // Assert EntityTypeConfiguration principalEntityType = Assert.Single( builder.StructuralTypes.OfType <EntityTypeConfiguration>().Where(e => e.Name == "ForeignPrincipal")); PropertyConfiguration propertyConfig = Assert.Single(principalEntityType.Properties); PrimitivePropertyConfiguration primitiveConfig = Assert.IsType <PrimitivePropertyConfiguration>(propertyConfig); Assert.Equal("PrincipalKey1", primitiveConfig.Name); Assert.Equal("System.Int32", primitiveConfig.RelatedClrType.FullName); }
/// <summary> /// Configures the key property(s) for this entity type. /// </summary> /// <param name="keyProperty">The property to be added to the key properties of this entity type.</param> /// <returns>Returns itself so that multiple calls can be chained.</returns> public virtual EntityTypeConfiguration HasKey(PropertyInfo keyProperty) { if (BaseType != null && BaseType.Keys().Any()) { throw Error.InvalidOperation(SRResources.CannotDefineKeysOnDerivedTypes, FullName, BaseType.FullName); } PrimitivePropertyConfiguration propertyConfig = AddProperty(keyProperty); // keys are always required propertyConfig.IsRequired(); if (!_keys.Contains(propertyConfig)) { _keys.Add(propertyConfig); } return(this); }
public void CanAddPrimitiveProperty_ForDependentEntityType() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); // Act builder.EntityType <ForeignEntity>().HasRequired(c => c.Principal, (c, r) => c.ForeignKey1 == r.PrincipalKey1); // Assert EntityTypeConfiguration dependentEntityType = builder.StructuralTypes.OfType <EntityTypeConfiguration>().FirstOrDefault(e => e.Name == "ForeignEntity"); Assert.NotNull(dependentEntityType); PrimitivePropertyConfiguration primitiveConfig = Assert.Single(dependentEntityType.Properties.OfType <PrimitivePropertyConfiguration>()); Assert.Equal("ForeignKey1", primitiveConfig.Name); Assert.Equal("System.Int32", primitiveConfig.RelatedClrType.FullName); }
public void CreateTimeOfDayPrimitiveProperty() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <File> file = builder.EntityType <File>(); PrimitivePropertyConfiguration timeOfDay = file.Property(f => f.TimeOfDayProperty); // Act IEdmModel model = builder.GetServiceModel(); // Assert Assert.Equal(PropertyKind.Primitive, timeOfDay.Kind); IEdmEntityType fileType = Assert.Single(model.SchemaElements.OfType <IEdmEntityType>()); IEdmProperty property = Assert.Single(fileType.DeclaredProperties.Where(p => p.Name == "TimeOfDayProperty")); Assert.NotNull(property); Assert.Equal("Edm.TimeOfDay", property.Type.FullName()); }
public void Keys_Returns_DerivedKeys_IfBaseTypePresent() { // Arrange PrimitivePropertyConfiguration[] keys = new PrimitivePropertyConfiguration[0]; Mock <EntityTypeConfiguration> baseBaseEntity = new Mock <EntityTypeConfiguration>(); baseBaseEntity.Setup(e => e.Keys).Returns(keys); baseBaseEntity.Setup(e => e.BaseType).Returns <EntityTypeConfiguration>(null); Mock <EntityTypeConfiguration> baseEntity = new Mock <EntityTypeConfiguration>(); baseEntity.Setup(e => e.BaseType).Returns(baseBaseEntity.Object); Mock <EntityTypeConfiguration> entity = new Mock <EntityTypeConfiguration>(); baseEntity.Setup(e => e.BaseType).Returns(baseEntity.Object); // Act & Assert Assert.ReferenceEquals(keys, entity.Object.Keys()); }
/// <summary> /// Configures the key property(s) for this entity type. /// </summary> /// <param name="keyProperty">The property to be added to the key properties of this entity type.</param> /// <returns>Returns itself so that multiple calls can be chained.</returns> public virtual EntityTypeConfiguration HasKey(PropertyInfo keyProperty) { if (BaseType != null && BaseType.Keys().Any()) { throw Error.InvalidOperation(SRResources.CannotDefineKeysOnDerivedTypes, FullName, BaseType.FullName); } // Add the enum key if the property type is enum if (keyProperty.PropertyType.IsEnum) { ModelBuilder.AddEnumType(keyProperty.PropertyType); EnumPropertyConfiguration enumConfig = AddEnumProperty(keyProperty); // keys are always required enumConfig.IsRequired(); if (!_enumKeys.Contains(enumConfig)) { _enumKeys.Add(enumConfig); } } else { PrimitivePropertyConfiguration propertyConfig = AddProperty(keyProperty); // keys are always required propertyConfig.IsRequired(); if (!_keys.Contains(propertyConfig)) { _keys.Add(propertyConfig); } } return(this); }
/// <summary> /// Adds a primitive property to this edm type. /// </summary> /// <param name="propertyInfo">The property being added.</param> /// <returns>The <see cref="PrimitivePropertyConfiguration"/> so that the property can be configured further.</returns> public virtual PrimitivePropertyConfiguration AddProperty(PropertyInfo propertyInfo) { if (propertyInfo == null) { throw Error.ArgumentNull("propertyInfo"); } if (!propertyInfo.ReflectedType.IsAssignableFrom(ClrType)) { throw Error.Argument("propertyInfo", SRResources.PropertyDoesNotBelongToType, propertyInfo.Name, ClrType.FullName); } // Remove from the ignored properties if (RemovedProperties.Contains(propertyInfo)) { RemovedProperties.Remove(propertyInfo); } PrimitivePropertyConfiguration propertyConfiguration = null; if (ExplicitProperties.ContainsKey(propertyInfo)) { propertyConfiguration = ExplicitProperties[propertyInfo] as PrimitivePropertyConfiguration; if (propertyConfiguration == null) { throw Error.Argument("propertyInfo", SRResources.MustBePrimitiveProperty, propertyInfo.Name, ClrType.FullName); } } else { propertyConfiguration = new PrimitivePropertyConfiguration(propertyInfo, this); ExplicitProperties[propertyInfo] = propertyConfiguration; } return(propertyConfiguration); }
public void AsTimeOfDay_Works() { // Arrange MockType type = new MockType().Property(typeof(TimeSpan), "CreatedTime"); PropertyInfo property = type.GetProperty("CreatedTime"); _structuralType.Setup(t => t.ClrType).Returns(type); // Act PrimitivePropertyConfiguration propertyConfig = new PrimitivePropertyConfiguration(property, _structuralType.Object); EdmPrimitiveTypeKind? typeKind = propertyConfig.AsTimeOfDay().TargetEdmTypeKind; // Assert Assert.NotNull(typeKind); Assert.Equal(EdmPrimitiveTypeKind.TimeOfDay, typeKind); }
/// <summary> /// Removes the property from the entity keys collection. /// </summary> /// <param name="keyProperty">The key to be removed.</param> /// <remarks>This method just disable the property to be not a key anymore. It does not remove the property all together. /// To remove the property completely, use the method <see cref="RemoveProperty"/></remarks> public virtual void RemoveKey(PrimitivePropertyConfiguration keyProperty) { if (keyProperty == null) { throw Error.ArgumentNull("keyProperty"); } _keys.Remove(keyProperty); }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration; EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); // Set concurrency token if is entity type, and concurrency token is true EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None; if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken) { concurrencyMode = EdmConcurrencyMode.Fixed; } edmProperty = type.AddStructuralProperty( primitiveProperty.Name, primitiveTypeReference, defaultValue: null, concurrencyMode: concurrencyMode); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property); break; case PropertyKind.Enum: edmProperty = CreateStructuralTypeEnumPropertyBody(type, config, (EnumPropertyConfiguration)property); break; default: break; } if (edmProperty != null) { if (property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } if (property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } } } }
/// <summary> /// Adds a primitive property to this edm type. /// </summary> /// <param name="propertyInfo">The property being added.</param> /// <returns>The <see cref="PrimitivePropertyConfiguration"/> so that the property can be configured further.</returns> public virtual PrimitivePropertyConfiguration AddProperty(PropertyInfo propertyInfo) { if (propertyInfo == null) { throw Error.ArgumentNull("propertyInfo"); } if (!propertyInfo.ReflectedType.IsAssignableFrom(ClrType)) { throw Error.Argument("propertyInfo", SRResources.PropertyDoesNotBelongToType, propertyInfo.Name, ClrType.FullName); } // Remove from the ignored properties if (RemovedProperties.Contains(propertyInfo)) { RemovedProperties.Remove(propertyInfo); } PrimitivePropertyConfiguration propertyConfiguration = null; if (ExplicitProperties.ContainsKey(propertyInfo)) { propertyConfiguration = ExplicitProperties[propertyInfo] as PrimitivePropertyConfiguration; if (propertyConfiguration == null) { throw Error.Argument("propertyInfo", SRResources.MustBePrimitiveProperty, propertyInfo.Name, ClrType.FullName); } } else { propertyConfiguration = new PrimitivePropertyConfiguration(propertyInfo, this); ExplicitProperties[propertyInfo] = propertyConfiguration; } return propertyConfiguration; }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property; EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ?? GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); if (typeKind == EdmPrimitiveTypeKind.Decimal) { DecimalPropertyConfiguration decimalProperty = primitiveProperty as DecimalPropertyConfiguration; if (decimalProperty.Precision.HasValue || decimalProperty.Scale.HasValue) { primitiveTypeReference = new EdmDecimalTypeReference( (IEdmPrimitiveType)primitiveTypeReference.Definition, primitiveTypeReference.IsNullable, decimalProperty.Precision, decimalProperty.Scale.HasValue ? decimalProperty.Scale : 0); } } else if (EdmLibHelpers.HasPrecision(typeKind)) { PrecisionPropertyConfiguration precisionProperty = primitiveProperty as PrecisionPropertyConfiguration; primitiveTypeReference = AddPrecisionConfigInPrimitiveTypeReference( precisionProperty, primitiveTypeReference); } else if (EdmLibHelpers.HasLength(typeKind)) { LengthPropertyConfiguration lengthProperty = primitiveProperty as LengthPropertyConfiguration; primitiveTypeReference = AddLengthConfigInPrimitiveTypeReference( lengthProperty, primitiveTypeReference); } edmProperty = type.AddStructuralProperty( primitiveProperty.Name, primitiveTypeReference, defaultValue: null); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property); break; case PropertyKind.Enum: edmProperty = CreateStructuralTypeEnumPropertyBody(type, (EnumPropertyConfiguration)property); break; default: break; } if (edmProperty != null) { if (property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } if (property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } if (property.QueryConfiguration.ModelBoundQuerySettings != null) { _propertiesQuerySettings.Add(edmProperty, property.QueryConfiguration.ModelBoundQuerySettings); } } } }