Esempio n. 1
0
        public void CanCreateDerivedComplexType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <BaseComplexType>().Abstract().Property(v => v.BaseProperty);
            builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>().Property(v => v.DerivedProperty);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmComplexType baseComplexType = model.AssertHasComplexType(typeof(BaseComplexType));

            Assert.Null(baseComplexType.BaseComplexType());
            Assert.Equal(1, baseComplexType.Properties().Count());
            baseComplexType.AssertHasPrimitiveProperty(model, "BaseProperty", EdmPrimitiveTypeKind.String, true);

            IEdmComplexType derivedComplexType = model.AssertHasComplexType(typeof(DerivedComplexType));

            Assert.Equal(baseComplexType, derivedComplexType.BaseComplexType());
            Assert.Equal(2, derivedComplexType.Properties().Count());
            derivedComplexType.AssertHasPrimitiveProperty(model, "BaseProperty", EdmPrimitiveTypeKind.String, true);
            derivedComplexType.AssertHasPrimitiveProperty(model, "DerivedProperty", EdmPrimitiveTypeKind.Int32, false);
        }
        public BindingPathConfiguration <TTargetType> HasManyPath <TTargetType>(
            Expression <Func <TStructuralType, IEnumerable <TTargetType> > > pathExpression,
            bool contained)
            where TTargetType : class
        {
            if (pathExpression == null)
            {
                throw Error.ArgumentNull("pathExpression");
            }

            PropertyInfo pathProperty = PropertySelectorVisitor.GetSelectedProperty(pathExpression);

            IList <MemberInfo> bindingPath = new List <MemberInfo>(_bindingPath);

            bindingPath.Add(pathProperty);

            StructuralTypeConfiguration <TTargetType> target;

            if (contained)
            {
                target = _modelBuilder.EntityType <TTargetType>();
                _structuralType.ContainsMany(pathExpression); // add a containment navigation property
            }
            else
            {
                target = _modelBuilder.ComplexType <TTargetType>();
                _structuralType.CollectionProperty(pathExpression); // add a collection complex property
            }

            return(new BindingPathConfiguration <TTargetType>(_modelBuilder, target, _navigationSource, bindingPath));
        }
Esempio n. 3
0
        public void AddProperty_Throws_WhenRedefineBaseTypeProperty_OnDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <BaseComplexType>().Property(v => v.BaseProperty);

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>().Property(v => v.BaseProperty),
                "propertyInfo",
                "Cannot redefine property 'BaseProperty' already defined on the base type 'System.Web.OData.Builder.BaseComplexType'.");
        }
Esempio n. 4
0
        public void AddProperty_Throws_WhenDefinePropertyOnBaseTypeAlreadyPresentInDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>().Property(m => m.BaseProperty);

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.ComplexType <BaseComplexType>().Property(v => v.BaseProperty),
                "propertyInfo",
                "Cannot define property 'BaseProperty' in the base type 'System.Web.OData.Builder.BaseComplexType' " +
                "as the derived type 'System.Web.OData.Builder.DerivedComplexType' already defines it.");
        }
Esempio n. 5
0
        public void DerivesFrom_Throws_WhenSettingTheBaseType_IfDuplicatePropertyInDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <BaseComplexType>().Property(v => v.BaseProperty);
            builder.ComplexType <SubDerivedComplexType>().DerivesFrom <DerivedComplexType>().Property(c => c.BaseProperty);

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>(),
                "propertyInfo",
                "Cannot define property 'BaseProperty' in the base type 'System.Web.OData.Builder.DerivedComplexType' as " +
                "the derived type 'System.Web.OData.Builder.SubDerivedComplexType' already defines it.");
        }
Esempio n. 6
0
        public void CreateArrayOfEnumTypeProperty()
        {
            // Arrange
            var builder = new ODataModelBuilder().Add_Color_EnumType();
            var complexTypeConfiguration = builder.ComplexType <ArrayEnumTypePropertyTestModel>();

            complexTypeConfiguration.CollectionProperty(c => c.Colors);
            complexTypeConfiguration.CollectionProperty(c => c.NullableColors);

            // Act
            var model       = builder.GetEdmModel();
            var complexType = model.SchemaElements.OfType <IEdmStructuredType>().Single();

            // Assert
            Assert.Equal(2, complexType.Properties().Count());
            var colors = complexType.Properties().SingleOrDefault(p => p.Name == "Colors");

            Assert.NotNull(colors);
            Assert.True(colors.Type.IsCollection());
            Assert.False(colors.Type.IsNullable);
            Assert.True(((IEdmCollectionTypeReference)colors.Type).ElementType().IsEnum());

            var nullablecolors = complexType.Properties().SingleOrDefault(p => p.Name == "NullableColors");

            Assert.NotNull(nullablecolors);
            Assert.True(nullablecolors.Type.IsCollection());
            Assert.True(nullablecolors.Type.IsNullable);
            Assert.True(((IEdmCollectionTypeReference)nullablecolors.Type).ElementType().IsEnum());
        }
Esempio n. 7
0
        public ComplexTypeConfiguration <TComplexType> DerivesFrom <TBaseType>() where TBaseType : class
        {
            ComplexTypeConfiguration <TBaseType> baseEntityType = _modelBuilder.ComplexType <TBaseType>();

            _configuration.DerivesFrom(baseEntityType._configuration);
            return(this);
        }
Esempio n. 8
0
        public void NonbindingParameterConfigurationSupportsParameterTypeAs(Type type, bool isNullable)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Customer>();
            builder.ComplexType <Address>();
            builder.EnumType <Color>();

            // Act
            Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(type);
            IEdmTypeConfiguration edmTypeConfiguration = builder.GetTypeConfigurationOrNull(type);

            if (underlyingType.IsEnum)
            {
                edmTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType);
                if (edmTypeConfiguration != null && isNullable)
                {
                    edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration();
                }
            }
            NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name",
                                                                                              edmTypeConfiguration);

            // Assert
            Assert.Equal(isNullable, parameter.OptionalParameter);
        }
Esempio n. 9
0
        public void CreateComplexTypeWith_OneToMany_NavigationProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Order>().HasKey(o => o.OrderId);

            ComplexTypeConfiguration <Customer> customer = builder.ComplexType <Customer>();

            customer.HasMany(c => c.Orders);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmEntityType orderType = Assert.Single(model.SchemaElements.OfType <IEdmEntityType>());

            Assert.Equal("System.Web.OData.Builder.TestModels.Order", orderType.FullName());
            Assert.Equal("OrderId", orderType.DeclaredKey.Single().Name);
            Assert.Equal(1, orderType.DeclaredProperties.Count());
            Assert.Empty(orderType.NavigationProperties());

            IEdmComplexType customerType = Assert.Single(model.SchemaElements.OfType <IEdmComplexType>());

            Assert.Equal("System.Web.OData.Builder.TestModels.Customer", customerType.FullName());

            IEdmNavigationProperty navProperty = Assert.Single(customerType.NavigationProperties());

            Assert.Equal(EdmMultiplicity.Many, navProperty.TargetMultiplicity());
            Assert.Equal("Orders", navProperty.Name);
            Assert.True(navProperty.Type.IsCollection());
            Assert.Same(orderType, navProperty.Type.AsCollection().ElementType().Definition);
        }
Esempio n. 10
0
        public void DerivesFrom_Throws_WhenSettingTheBaseType_IfDuplicatePropertyInBaseType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <BaseComplexType>().Property(v => v.BaseProperty);
            ComplexTypeConfiguration <DerivedComplexType> derivedComplex = builder.ComplexType <DerivedComplexType>();

            derivedComplex.Property(m => m.BaseProperty);

            // Act & Assert
            Assert.ThrowsArgument(
                () => derivedComplex.DerivesFrom <BaseComplexType>(),
                "propertyInfo",
                "Cannot redefine property 'BaseProperty' already defined on the base type 'System.Web.OData.Builder.BaseComplexType'.");
        }
Esempio n. 11
0
        public void DerivesFrom_Throws_IfDerivedTypeDoesnotDeriveFromBaseType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.ComplexType <string>().DerivesFrom <BaseComplexType>(),
                "baseType",
                "'System.String' does not inherit from 'System.Web.OData.Builder.BaseComplexType'.");
        }
Esempio n. 12
0
        public void CanDeriveFromNull()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            ComplexTypeConfiguration <DerivedComplexType> derivedComplex = builder.ComplexType <DerivedComplexType>();

            // Act
            derivedComplex.DerivesFromNothing();

            // Assert
            Assert.Null(derivedComplex.BaseType);
        }
Esempio n. 13
0
        public void CreateInfiniteRecursiveComplexTypeDefinitionFails()
        {
            var builder = new ODataModelBuilder()
                          .Add_RecursiveZipCode_ComplexType();

            var zipCode = builder.ComplexType <RecursiveZipCode>();

            Assert.ThrowsArgument(
                () => zipCode.ComplexProperty(z => z.Recursive),
                "propertyInfo",
                "The complex type 'System.Web.OData.Builder.TestModels.RecursiveZipCode' has a reference to itself through the property 'Recursive'. A recursive loop of complex types is not allowed.");
        }
Esempio n. 14
0
        public void DerivesFrom_SetsBaseType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            ComplexTypeConfiguration <DerivedComplexType> derivedComplex = builder.ComplexType <DerivedComplexType>();

            // Act
            derivedComplex.DerivesFrom <BaseComplexType>();

            // Assert
            Assert.NotNull(derivedComplex.BaseType);
            Assert.Equal(typeof(BaseComplexType), derivedComplex.BaseType.ClrType);
        }
Esempio n. 15
0
        public void ODataModelBuilder_Throws_AddCollectionOfEnumPropertyWithoutEnumType()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var complexTypeConfiguration = builder.ComplexType <ComplexTypeWithEnumTypePropertyTestModel>();

            complexTypeConfiguration.CollectionProperty(c => c.Colors);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => builder.GetServiceModel(),
                "The enum type 'Color' does not exist.");
        }
Esempio n. 16
0
        public void PassNullToStructuralTypeConfigurationAddEnumPropertyShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            builder.ComplexType <ComplexTypeWithEnumTypePropertyTestModel>();
            StructuralTypeConfiguration structuralTypeConfiguration = builder.StructuralTypes.Single();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => structuralTypeConfiguration.AddEnumProperty(null),
                "propertyInfo");
        }
Esempio n. 17
0
        public void CanCreateDerivedComplexTypeInReverseOrder()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>();

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            model.AssertHasComplexType(typeof(BaseComplexType));
            model.AssertHasComplexType(typeof(DerivedComplexType), typeof(BaseComplexType));
        }
Esempio n. 18
0
        public void DynamicDictionaryProperty_Works_ToSetOpenComplexType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            // Act
            ComplexTypeConfiguration <SimpleOpenComplexType> complexType = builder.ComplexType <SimpleOpenComplexType>();

            complexType.Property(c => c.IntProperty);
            complexType.HasDynamicProperties(c => c.DynamicProperties);

            // Act & Assert
            Assert.True(complexType.IsOpen);
        }
Esempio n. 19
0
        public void CanCreateAbstractComplexType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <BaseComplexType>().Abstract();

            // Arrange
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmComplexType baseComplexType = Assert.Single(model.SchemaElements.OfType <IEdmComplexType>());

            Assert.True(baseComplexType.IsAbstract);
        }
Esempio n. 20
0
        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 BindingParameterConfigurationThrowsWhenParameterTypeIsNotEntity()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <Address>();

            // Act & Assert
            ArgumentException exception = Assert.Throws <ArgumentException>(() =>
            {
                BindingParameterConfiguration configuration = new BindingParameterConfiguration("name", builder.GetTypeConfigurationOrNull(typeof(Address)), true);
            });

            Assert.True(exception.Message.Contains(string.Format("'{0}'", typeof(Address).FullName)));
            Assert.Equal("parameterType", exception.ParamName);
        }
Esempio n. 22
0
        public void NonNullablePropertiesAreNotOptional()
        {
            // NOTE: Converting this to be a data driven test is painful as we need to mock C# overload resolution algorithm.
            var builder     = new ODataModelBuilder();
            var complexType = builder.ComplexType <ComplexTypeTestModel>();

            Assert.False(complexType.Property(t => t.BoolProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.ByteProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.DateTimeOffsetProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.DoubleProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.GuidProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.IntProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.LongProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.ShortProperty).OptionalProperty);
            Assert.False(complexType.Property(t => t.TimeSpanProperty).OptionalProperty);
        }
Esempio n. 23
0
        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'.");
        }
Esempio n. 24
0
        public void PamameterOfEnumPropertyIsNotEnumShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var color   = builder.EnumType <Color>();

            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);
            var entityTypeConfiguration = builder.ComplexType <EntityTypeWithEnumTypePropertyTestModel>();

            // Act & Assert
            Assert.ThrowsArgument(
                () => entityTypeConfiguration.EnumProperty(e => e.ID),
                "propertyInfo",
                "The property 'ID' on type 'System.Web.OData.Builder.EntityTypeWithEnumTypePropertyTestModel' must be an Enum property.");
        }
Esempio n. 25
0
        public void NullablePropertiesAreOptional()
        {
            // NOTE: Converting this to be a data driven test is painful as we need to mock C# overload resolution algorithm.
            var builder     = new ODataModelBuilder();
            var complexType = builder.ComplexType <ComplexTypeTestModel>();

            Assert.True(complexType.Property(t => t.NullableBoolProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableByteProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableDateTimeOffsetProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableDoubleProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableGuidProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableIntProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableLongProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableShortProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.NullableTimeSpanProperty).OptionalProperty);

            // Assert.True(complexType.Property(t => t.StreamProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.StringProperty).OptionalProperty);
            Assert.True(complexType.Property(t => t.ByteArrayProperty).OptionalProperty);
        }
Esempio n. 26
0
        public void CreateComplexTypePropertyInComplexType()
        {
            var builder = new ODataModelBuilder()
                          .Add_Address_ComplexType();

            var address = builder.ComplexType <Address>();

            address.ComplexProperty(a => a.ZipCode);

            var model       = builder.GetServiceModel();
            var addressType = model.SchemaElements.OfType <IEdmComplexType>().SingleOrDefault(se => se.Name == "Address");
            var zipCodeType = model.SchemaElements.OfType <IEdmComplexType>().SingleOrDefault(se => se.Name == "ZipCode");

            Assert.NotNull(addressType);
            Assert.NotNull(zipCodeType);
            var zipCodeProperty = addressType.FindProperty("ZipCode");

            Assert.NotNull(zipCodeProperty);
            Assert.Equal(zipCodeType.FullName(), zipCodeProperty.Type.AsComplex().ComplexDefinition().FullName());
        }
Esempio n. 27
0
        public void CreateNullableEnumType()
        {
            // Arrange
            var builder = new ODataModelBuilder().Add_Color_EnumType();
            var complexTypeConfiguration = builder.ComplexType <ComplexTypeWithEnumTypePropertyTestModel>();

            complexTypeConfiguration.EnumProperty(c => c.NullableColor);

            // Act
            var model       = builder.GetEdmModel();
            var complexType = model.SchemaElements.OfType <IEdmStructuredType>().Single();

            // Assert
            Assert.Equal(1, complexType.Properties().Count());
            var nullableColor = complexType.Properties().SingleOrDefault(p => p.Name == "NullableColor");

            Assert.NotNull(nullableColor);
            Assert.True(nullableColor.Type.IsEnum());
            Assert.True(nullableColor.Type.IsNullable);
        }
Esempio n. 28
0
        public void GetEdmModel_WorksOnModelBuilder_ForOpenComplexType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            ComplexTypeConfiguration <SimpleOpenComplexType> complex = builder.ComplexType <SimpleOpenComplexType>();

            complex.Property(c => c.IntProperty);
            complex.HasDynamicProperties(c => c.DynamicProperties);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.NotNull(model);
            IEdmComplexType complexType = Assert.Single(model.SchemaElements.OfType <IEdmComplexType>());

            Assert.True(complexType.IsOpen);
            IEdmProperty edmProperty = Assert.Single(complexType.Properties());

            Assert.Equal("IntProperty", edmProperty.Name);
        }
Esempio n. 29
0
        public void CanDefinePropertyOnDerivedType_NotPresentInBaseType_ButPresentInDerivedType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.ComplexType <DerivedComplexType>().DerivesFrom <BaseComplexType>().Property(m => m.BaseProperty);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmComplexType baseComplex = model.AssertHasComplexType(typeof(BaseComplexType));

            Assert.Null(baseComplex.BaseComplexType());
            Assert.Equal(0, baseComplex.Properties().Count());

            IEdmComplexType derivedComplex = model.AssertHasComplexType(typeof(DerivedComplexType));

            Assert.Equal(baseComplex, derivedComplex.BaseComplexType());
            Assert.Equal(1, derivedComplex.Properties().Count());
            derivedComplex.AssertHasPrimitiveProperty(model, "BaseProperty", EdmPrimitiveTypeKind.String, true);
        }
Esempio n. 30
0
        public void NonbindingParameterConfigurationSupportsParameterCollectionTypeAs(Type type, bool isNullable)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Customer>();
            builder.ComplexType <Address>();
            builder.EnumType <Color>();

            Type elementType;

            Assert.True(type.IsCollection(out elementType));

            // Act
            Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(elementType);
            IEdmTypeConfiguration       elementTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType);
            CollectionTypeConfiguration collectionType           = new CollectionTypeConfiguration(elementTypeConfiguration,
                                                                                                   typeof(IEnumerable <>).MakeGenericType(elementType));

            NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", collectionType);

            // Assert
            Assert.Equal(isNullable, parameter.OptionalParameter);
        }