Beispiel #1
0
        public void CanCreateFunctionWithNonbindingParameters()
        {
            // Arrange
            // Act
            ODataModelBuilder     builder  = new ODataModelBuilder();
            FunctionConfiguration function = builder.Function("MyFunction");

            function.Parameter <string>("p0");
            function.Parameter <int>("p1");
            function.Parameter <Address>("p2");
            function.CollectionParameter <string>("p3");
            function.CollectionParameter <int>("p4");
            function.CollectionParameter <ZipCode>("p5");
            ParameterConfiguration[]   parameters   = function.Parameters.ToArray();
            ComplexTypeConfiguration[] complexTypes = builder.StructuralTypes.OfType <ComplexTypeConfiguration>().ToArray();

            // Assert
            Assert.Equal(2, complexTypes.Length);
            Assert.Equal(typeof(Address).FullName, complexTypes[0].FullName);
            Assert.Equal(typeof(ZipCode).FullName, complexTypes[1].FullName);
            Assert.Equal(6, parameters.Length);
            Assert.Equal("p0", parameters[0].Name);
            Assert.Equal("Edm.String", parameters[0].TypeConfiguration.FullName);
            Assert.Equal("p1", parameters[1].Name);
            Assert.Equal("Edm.Int32", parameters[1].TypeConfiguration.FullName);
            Assert.Equal("p2", parameters[2].Name);
            Assert.Equal(typeof(Address).FullName, parameters[2].TypeConfiguration.FullName);
            Assert.Equal("p3", parameters[3].Name);
            Assert.Equal("Collection(Edm.String)", parameters[3].TypeConfiguration.FullName);
            Assert.Equal("p4", parameters[4].Name);
            Assert.Equal("Collection(Edm.Int32)", parameters[4].TypeConfiguration.FullName);
            Assert.Equal("p5", parameters[5].Name);
            Assert.Equal(string.Format("Collection({0})", typeof(ZipCode).FullName), parameters[5].TypeConfiguration.FullName);
        }
Beispiel #2
0
        public void CanCreateFunctionWithNonbindingParametersAsNullable()
        {
            // Arrange & Act
            ODataModelBuilder     builder  = new ODataModelBuilder();
            FunctionConfiguration function = builder.Function("MyFunction");

            function.Parameter <string>("p0");
            function.Parameter <string>("p1").OptionalParameter = false;
            function.Parameter <int>("p2").OptionalParameter    = true;
            function.Parameter <int>("p3");
            function.Parameter <Address>("p4");
            function.Parameter <Address>("p5").OptionalParameter = false;

            function.CollectionParameter <ZipCode>("p6");
            function.CollectionParameter <ZipCode>("p7").OptionalParameter = false;

            Dictionary <string, ParameterConfiguration> parameters = function.Parameters.ToDictionary(e => e.Name, e => e);

            // Assert
            Assert.True(parameters["p0"].OptionalParameter);
            Assert.False(parameters["p1"].OptionalParameter);

            Assert.True(parameters["p2"].OptionalParameter);
            Assert.False(parameters["p3"].OptionalParameter);

            Assert.True(parameters["p4"].OptionalParameter);
            Assert.False(parameters["p5"].OptionalParameter);

            Assert.True(parameters["p6"].OptionalParameter);
            Assert.False(parameters["p7"].OptionalParameter);
        }
Beispiel #3
0
        public void BoundFunction_ForEnumTypeInODataConventionModelBuilder()
        {
            // Arrange
            ODataConventionModelBuilder         builder = new ODataConventionModelBuilder();
            EntityTypeConfiguration <EnumModel> entityTypeConfiguration = builder.EntityType <EnumModel>();
            FunctionConfiguration functionConfiguration = entityTypeConfiguration.Function("BoundFunction");

            functionConfiguration.CollectionParameter <Color?>("Colors");
            functionConfiguration.Returns <Color>();

            // Act & Assert
            IEdmModel    model    = builder.GetEdmModel();
            IEdmFunction function = model.FindDeclaredOperations("Default.BoundFunction").Single() as IEdmFunction;

            IEdmTypeReference colors     = function.Parameters.Single(p => p.Name == "Colors").Type;
            IEdmTypeReference returnType = function.ReturnType;
            IEdmEnumType      colorType  = model.SchemaElements.OfType <IEdmEnumType>().Single(e => e.Name == "Color");

            Assert.True(colors.IsCollection());
            Assert.True(colors.AsCollection().ElementType().IsNullable);
            Assert.Same(colorType, colors.AsCollection().ElementType().Definition);
            Assert.Same(colorType, returnType.Definition);
        }
Beispiel #4
0
        public void UnboundFunction_ForEnumTypeInODataModelBuilder()
        {
            // Arrange
            ODataModelBuilder     builder = new ODataModelBuilder();
            FunctionConfiguration functionConfiguration = builder.Function("UnboundFunction");

            functionConfiguration.CollectionParameter <Color>("Colors");
            functionConfiguration.ReturnsCollection <Color>();
            builder.Add_Color_EnumType();

            // Act & Assert
            IEdmModel          model          = builder.GetEdmModel();
            IEdmFunctionImport functionImport = model.EntityContainer.OperationImports().Single(o => o.Name == "UnboundFunction") as IEdmFunctionImport;

            IEdmTypeReference colors     = functionImport.Function.Parameters.Single(p => p.Name == "Colors").Type;
            IEdmTypeReference returnType = functionImport.Function.ReturnType;
            IEdmEnumType      colorType  = model.SchemaElements.OfType <IEdmEnumType>().Single(e => e.Name == "Color");

            Assert.True(colors.IsCollection());
            Assert.Same(colorType, colors.AsCollection().ElementType().Definition);
            Assert.True(returnType.IsCollection());
            Assert.False(returnType.AsCollection().ElementType().IsNullable);
            Assert.Same(colorType, returnType.AsCollection().ElementType().Definition);
        }
Beispiel #5
0
        public void CanCreateEdmModel_ForBindableFunction_WithSupportedParameterType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntityType <Customer>();

            customer.HasKey(c => c.CustomerId);
            customer.Property(c => c.Name);

            // Act
            FunctionConfiguration functionBuilder = customer.Function("FunctionName");

            functionBuilder.Parameter <int>("primitive");
            functionBuilder.CollectionParameter <int>("collectionPrimitive");

            functionBuilder.Parameter <bool?>("nullablePrimitive");
            functionBuilder.CollectionParameter <bool?>("nullableCollectionPrimitive");

            functionBuilder.Parameter <Color>("enum");
            functionBuilder.CollectionParameter <Color>("collectionEnum");

            functionBuilder.Parameter <Color?>("nullableEnum");
            functionBuilder.CollectionParameter <Color?>("nullableCollectionEnum");

            functionBuilder.Parameter <Address>("complex");
            functionBuilder.CollectionParameter <Address>("collectionComplex");

            functionBuilder.EntityParameter <Customer>("entity");
            functionBuilder.CollectionEntityParameter <Customer>("collectionEntity");

            functionBuilder.Returns <bool>();

            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.Equal(1, model.SchemaElements.OfType <IEdmFunction>().Count());
            IEdmFunction function = Assert.Single(model.SchemaElements.OfType <IEdmFunction>());

            Assert.False(function.IsComposable);
            Assert.True(function.IsBound);
            Assert.Equal("FunctionName", function.Name);
            Assert.NotNull(function.ReturnType);

            Assert.Equal(13, function.Parameters.Count());

            function.AssertHasParameter(model, BindingParameterConfiguration.DefaultBindingParameterName, typeof(Customer), true);

            function.AssertHasParameter(model, parameterName: "primitive", parameterType: typeof(int), isNullable: false);
            function.AssertHasParameter(model, parameterName: "collectionPrimitive", parameterType: typeof(IList <int>), isNullable: false);

            function.AssertHasParameter(model, parameterName: "nullablePrimitive", parameterType: typeof(bool?), isNullable: true);
            function.AssertHasParameter(model, parameterName: "nullableCollectionPrimitive", parameterType: typeof(IList <bool?>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "enum", parameterType: typeof(Color), isNullable: false);
            function.AssertHasParameter(model, parameterName: "collectionEnum", parameterType: typeof(IList <Color>), isNullable: false);

            function.AssertHasParameter(model, parameterName: "nullableEnum", parameterType: typeof(Color?), isNullable: true);
            function.AssertHasParameter(model, parameterName: "nullableCollectionEnum", parameterType: typeof(IList <Color?>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "complex", parameterType: typeof(Address), isNullable: true);
            function.AssertHasParameter(model, parameterName: "collectionComplex", parameterType: typeof(IList <Address>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "entity", parameterType: typeof(Customer), isNullable: true);
            function.AssertHasParameter(model, parameterName: "collectionEntity", parameterType: typeof(IList <Customer>), isNullable: true);
        }