示例#1
0
        public void GetEdmModel_SetsNullableIfParameterTypeIsReferenceType()
        {
            // Arrange
            ODataModelBuilder builder             = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType;
            var actionBuilder = movie.Action("Watch");

            actionBuilder.Parameter <string>("string").OptionalParameter = false;
            actionBuilder.Parameter <string>("nullaleString");

            actionBuilder.Parameter <Address>("address").OptionalParameter = false;
            actionBuilder.Parameter <Address>("nullableAddress");

            actionBuilder.EntityParameter <Customer>("customer").OptionalParameter = false;
            actionBuilder.EntityParameter <Customer>("nullableCustomer");

            actionBuilder.CollectionParameter <Address>("addresses").OptionalParameter = false;
            actionBuilder.CollectionParameter <Address>("nullableAddresses");

            actionBuilder.CollectionEntityParameter <Customer>("customers").OptionalParameter = false;
            actionBuilder.CollectionEntityParameter <Customer>("nullableCustomers");

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

            //Assert
            IEdmOperation action = Assert.Single(model.SchemaElements.OfType <IEdmAction>());

            Assert.False(action.FindParameter("string").Type.IsNullable);
            Assert.True(action.FindParameter("nullaleString").Type.IsNullable);

            Assert.False(action.FindParameter("address").Type.IsNullable);
            Assert.True(action.FindParameter("nullableAddress").Type.IsNullable);

            Assert.False(action.FindParameter("customer").Type.IsNullable);
            Assert.True(action.FindParameter("nullableCustomer").Type.IsNullable);

            Assert.False(action.FindParameter("addresses").Type.IsNullable);
            Assert.True(action.FindParameter("nullableAddresses").Type.IsNullable);

            // Follow up: https://github.com/OData/odata.net/issues/98
            // Assert.False(action.FindParameter("customers").Type.IsNullable);
            Assert.True(action.FindParameter("nullableCustomers").Type.IsNullable);
        }
示例#2
0
        public void GetEdmModel_SetReturnTypeAsNullable()
        {
            // Arrange
            ODataModelBuilder builder             = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType;

            movie.Action("Watch1").Returns <Address>();
            movie.Action("Watch2").Returns <Address>().OptionalReturn = false;

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

            //Assert
            IEdmOperation action = model.SchemaElements.OfType <IEdmAction>().First(e => e.Name == "Watch1");

            Assert.True(action.ReturnType.IsNullable);

            action = model.SchemaElements.OfType <IEdmAction>().First(e => e.Name == "Watch2");
            Assert.False(action.ReturnType.IsNullable);
        }
示例#3
0
        public void GetEdmModel_SetsNullableIfParameterTypeIsNullable()
        {
            // Arrange
            ODataModelBuilder builder             = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType;
            var actionBuilder = movie.Action("Watch");

            actionBuilder.Parameter <int>("int");
            actionBuilder.Parameter <Nullable <int> >("nullableOfInt");
            actionBuilder.Parameter <string>("string");

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

            //Assert
            IEdmOperation action = Assert.Single(model.SchemaElements.OfType <IEdmAction>());

            Assert.False(action.FindParameter("int").Type.IsNullable);
            Assert.True(action.FindParameter("nullableOfInt").Type.IsNullable);
            Assert.True(action.FindParameter("string").Type.IsNullable);
        }
示例#4
0
        public void CanCreateEdmModel_WithBindableAction()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntityType <Customer>();

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

            // Act
            customer.Action("ActionName");
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmAction action = Assert.Single(model.SchemaElements.OfType <IEdmAction>());

            Assert.True(action.IsBound);
            Assert.Equal("ActionName", action.Name);
            Assert.Null(action.ReturnType);
            Assert.Single(action.Parameters);
            Assert.Equal(BindingParameterConfiguration.DefaultBindingParameterName, action.Parameters.Single().Name);
            Assert.Equal(typeof(Customer).FullName, action.Parameters.Single().Type.FullName());
        }
示例#5
0
        public void GetEdmModel_SetsDateTimeAsParameterType()
        {
            // Arrange
            ODataModelBuilder builder             = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType;
            var actionBuilder = movie.Action("DateTimeAction");

            actionBuilder.Parameter <DateTime>("dateTime");
            actionBuilder.Parameter <DateTime?>("nullableDateTime");
            actionBuilder.CollectionParameter <DateTime>("collectionDateTime");
            actionBuilder.CollectionParameter <DateTime?>("nullableCollectionDateTime");

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

            //Assert
            IEdmOperation action = Assert.Single(model.SchemaElements.OfType <IEdmAction>());

            Assert.Equal("DateTimeAction", action.Name);

            IEdmOperationParameter parameter = action.FindParameter("dateTime");

            Assert.Equal("Edm.DateTimeOffset", parameter.Type.FullName());
            Assert.False(parameter.Type.IsNullable);

            parameter = action.FindParameter("nullableDateTime");
            Assert.Equal("Edm.DateTimeOffset", parameter.Type.FullName());
            Assert.True(parameter.Type.IsNullable);

            parameter = action.FindParameter("collectionDateTime");
            Assert.Equal("Collection(Edm.DateTimeOffset)", parameter.Type.FullName());
            Assert.False(parameter.Type.IsNullable);

            parameter = action.FindParameter("nullableCollectionDateTime");
            Assert.Equal("Collection(Edm.DateTimeOffset)", parameter.Type.FullName());
            Assert.True(parameter.Type.IsNullable);
        }
示例#6
0
        public void GetEdmModel_ThrowsException_WhenBoundActionOverloaded()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntityType <Customer>();

            customer.HasKey(c => c.CustomerId);
            customer.Property(c => c.Name);
            customer.Action("ActionOnCustomer");
            customer.Action("ActionOnCustomer").Returns <string>();

            // Act & Assert
            InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => builder.GetEdmModel());

            Assert.Equal(exception.Message, "Found more than one action with name 'ActionOnCustomer' " +
                         "bound to the same type 'Microsoft.AspNetCore.OData.Models.Customer'. " +
                         "Each bound action must have a different binding type or name.");
        }
示例#7
0
        public void GetEdmModel_ThrowsException_WhenUnboundActionOverloaded()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.Action("ActionName").Parameter <int>("Param1");
            builder.Action("ActionName").Returns <string>();

            // Act & Assert
            InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => builder.GetEdmModel());

            Assert.Equal(exception.Message, "Found more than one unbound action with name 'ActionName'. " +
                         "Each unbound action must have an unique action name.");
        }