示例#1
0
        public void CreateEdmModel_WithSingleton_CanAddNavigationLinkToDerivedNavigationProperty()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            var vehicle      = builder.AddEntityType(typeof(Vehicle));
            var car          = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            var manufacturer = builder.AddEntityType(typeof(CarManufacturer));
            var fordo        = builder.AddSingleton("Fordo", manufacturer);
            var navProperty  = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.One);

            var contoso = builder.AddSingleton("Contoso", vehicle);
            var binding = contoso.AddBinding(navProperty, fordo);

            contoso.HasNavigationPropertyLink(navProperty, new NavigationLinkBuilder((ctxt, property) => new Uri("http://works/"), followsConventions: false));

            // Act & assert
            IEdmModel model             = builder.GetEdmModel();
            var       motorcycleEdmType = model.AssertHasEntityType(typeof(Car));
            var       edmNavProperty    = motorcycleEdmType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: false, multiplicity: EdmMultiplicity.One);
            var       edmContoso        = model.EntityContainer.FindSingleton("Contoso");

            Assert.NotNull(model.GetNavigationSourceLinkBuilder(edmContoso));
            Assert.Equal(
                "http://works/",
                model.GetNavigationSourceLinkBuilder(edmContoso).BuildNavigationLink(new ResourceContext(), edmNavProperty, ODataMetadataLevel.MinimalMetadata).AbsoluteUri);
        }
示例#2
0
        public void CreateEdmModel_WithSingleton_CanAddBindingToDerivedNavigationProperty()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            var vehicle      = builder.AddEntityType(typeof(Vehicle));
            var motorcycle   = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            var manufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer));
            var yamaha       = builder.AddSingleton("Yamaha", manufacturer);
            var navProperty  = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.One);

            var r7 = builder.AddSingleton("Yamaha-R7", motorcycle);

            r7.AddBinding(navProperty, yamaha);

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

            // Assert
            var motorcycleEdmType = model.AssertHasEntityType(typeof(Motorcycle));
            var edmNavProperty    = motorcycleEdmType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: false, multiplicity: EdmMultiplicity.One);

            Assert.Equal(
                "Yamaha",
                model.EntityContainer.FindSingleton("Yamaha-R7").FindNavigationTarget(edmNavProperty).Name);
        }
示例#3
0
        public void AddSingleton_OnlyExistOne_IfAddTheSameSingletonTwiceWithSameType()
        {
            // Arrange
            var builder    = new ODataModelBuilder();
            var entityType = new Mock <EntityTypeConfiguration>().Object;
            var config1    = builder.AddSingleton("Singleton", entityType);

            // Act
            var config2 = builder.AddSingleton("Singleton", entityType);

            // Assert
            Assert.Same(config1, config2);
            Assert.Equal(1, builder.Singletons.Count());
        }
示例#4
0
        public void AddSingleton_ThrowException_IfAddTheSameSingletonTwiceWithDifferentType()
        {
            // Arrange
            var builder     = new ODataModelBuilder();
            var companyType = new Mock <EntityTypeConfiguration>();

            companyType.Setup(a => a.Name).Returns("Company");
            builder.AddSingleton("OsCorp", companyType.Object);
            var otherType = new Mock <EntityTypeConfiguration>().Object;

            // Act & Assert
            Assert.Throws <ArgumentException>(() => builder.AddSingleton("OsCorp", otherType),
                                              "The singleton 'OsCorp' was already configured with a different EntityType ('Company').\r\nParameter name: entityType");
        }
示例#5
0
        public void SingletonAddBinding_DoesnotThrows_IfBindingNavigationPropertyIsNotPartOfEntityType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicleType      = builder.AddEntityType(typeof(Vehicle));
            var carType          = builder.AddEntityType(typeof(Car));
            var manufacturerType = builder.AddEntityType(typeof(CarManufacturer));
            var fordo            = builder.AddSingleton("Fordo", manufacturerType);
            var navProperty      = carType.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.One);
            var myVehicle        = builder.AddSingleton("MyVehicle", vehicleType);

            // Act & Assert
            Assert.DoesNotThrow(() => myVehicle.AddBinding(navProperty, fordo));
        }
示例#6
0
        public void AddSingleton_ThrowException_IfEntityTypeNull()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => builder.AddSingleton("OsCorp", entityType: null),
                                      "entityType");
        }
示例#7
0
        public void AddSingleton_ThrowException_IfSingletonNameContainsADot()
        {
            // Arrange
            var builder    = new ODataModelBuilder();
            var entityType = new Mock <EntityTypeConfiguration>().Object;

            // Act & Assert
            Assert.Throws <NotSupportedException>(() => builder.AddSingleton("My.Singleton", entityType),
                                                  "'My.Singleton' is not a valid singleton name. The singleton name cannot contain '.'.");
        }
示例#8
0
        public void AddSingleton_ThrowException_IfSingletonNameNull()
        {
            // Arrange
            var builder    = new ODataModelBuilder();
            var entityType = new Mock <EntityTypeConfiguration>().Object;

            // Act & Assert
            Assert.Throws <ArgumentException>(() => builder.AddSingleton(null, entityType),
                                              "The argument 'name' is null or empty.\r\nParameter name: name");
        }
示例#9
0
        public void SingletonAddBinding_Throws_IfBindingNavigationPropertyIsNotPartOfEntityType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicleType      = builder.AddEntityType(typeof(Vehicle));
            var carType          = builder.AddEntityType(typeof(Car));
            var manufacturerType = builder.AddEntityType(typeof(CarManufacturer));
            var fordo            = builder.AddSingleton("Fordo", manufacturerType);
            var navProperty      = carType.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.One);
            var myVehicle        = builder.AddSingleton("MyVehicle", vehicleType);

            // Act & Assert
            Assert.ThrowsArgument(
                () => myVehicle.AddBinding(navProperty, fordo),
                "navigationConfiguration",
                "The declaring entity type 'System.Web.OData.Builder.TestModels.Car' of the given navigation property is not a part of " +
                "the entity type 'System.Web.OData.Builder.TestModels.Vehicle' hierarchy of the entity set or singleton 'MyVehicle'.");
        }
示例#10
0
        public void SingletonAddNavigationLink_Throws_IfNavigationPropertyInHierarchyIsNotPartOfEntityType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicleType      = builder.AddEntityType(typeof(Vehicle));
            var carType          = builder.AddEntityType(typeof(Car));
            var manufacturerType = builder.AddEntityType(typeof(CarManufacturer));
            var fordo            = builder.AddSingleton("Fordo", manufacturerType);

            var navProperty = carType.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.One);
            var myVehicle   = builder.AddSingleton("MyVehicle", vehicleType);

            // Act & Assert
            Assert.ThrowsArgument(
                () => myVehicle.HasNavigationPropertyLink(navProperty, new NavigationLinkBuilder((ctxt, property) => new Uri("http://works/"), followsConventions: false)),
                "navigationProperty",
                "The declaring entity type 'System.Web.OData.Builder.TestModels.Car' of the given navigation property is not a part of the " +
                "entity type 'System.Web.OData.Builder.TestModels.Vehicle' hierarchy of the entity set or singleton 'MyVehicle'.");
        }
示例#11
0
        public void CreateEdmModel_WithSingleton_CanAddBindingPath_ToNavigationProperty_WithComplex()
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            var motorcycle = builder.AddEntityType(typeof(Motorcycle));
            var myMotor    = builder.AddSingleton("MyMotor", motorcycle);

            var manufacturer = builder.AddComplexType(typeof(MotorcycleManufacturer));
            var address      = builder.AddEntityType(typeof(ManufacturerAddress));

            motorcycle.AddComplexProperty(typeof(Motorcycle).GetProperty("Manufacturer"));
            var navProperty = manufacturer.AddNavigationProperty(typeof(Manufacturer).GetProperty("Address"), EdmMultiplicity.One);

            var addresses = builder.AddEntitySet("Addresses", address);

            myMotor.AddBinding(navProperty, addresses, new List <MemberInfo>
            {
                typeof(Motorcycle).GetProperty("Manufacturer"),
                typeof(Manufacturer).GetProperty("Address")
            });

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

            // Assert
            var motorcycleEdmType = model.AssertHasEntityType(typeof(Motorcycle));

            Assert.Empty(motorcycleEdmType.NavigationProperties());

            var manufacturerEdmType = model.AssertHasComplexType(typeof(MotorcycleManufacturer));

            var edmNavProperty = manufacturerEdmType.AssertHasNavigationProperty(model, "Address",
                                                                                 typeof(ManufacturerAddress), isNullable: false, multiplicity: EdmMultiplicity.One);

            var myMotorSingleton = model.EntityContainer.FindSingleton("MyMotor");

            Assert.NotNull(myMotorSingleton);

            var bindings = myMotorSingleton.FindNavigationPropertyBindings(edmNavProperty);
            var binding  = Assert.Single(bindings);

            Assert.Equal("Address", binding.NavigationProperty.Name);
            Assert.Equal("Addresses", binding.Target.Name);
            Assert.Equal("Manufacturer/Address", binding.Path.Path);
        }
        public void HasNavigationPropertyLink_CanReplaceExistingLinks()
        {
            // Arrange
            var entity             = _builder.AddEntityType(typeof(Motorcycle));
            var navigationProperty = entity.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.One);
            var singleton          = _builder.AddSingleton("MyVehicle", entity);
            Uri link1 = new Uri("http://link1");
            Uri link2 = new Uri("http://link2");

            singleton.HasNavigationPropertyLink(navigationProperty, new NavigationLinkBuilder((entityContext, property) => link1, followsConventions: true));

            // Act
            singleton.HasNavigationPropertyLink(navigationProperty, new NavigationLinkBuilder((entityContext, property) => link2, followsConventions: false));

            // Assert
            var navigationLink = singleton.GetNavigationPropertyLink(navigationProperty);

            Assert.False(navigationLink.FollowsConventions);
            Assert.Equal(link2, navigationLink.Factory(null, null));
        }
        public void CreateModelUsingProgrammableApi()
        {
            var builder        = new ODataModelBuilder();
            var customerConfig = builder.AddEntityType(typeof(Customer));

            customerConfig.HasKey(typeof(Customer).GetProperty("CustomerId"));
            customerConfig.AddProperty(typeof(Customer).GetProperty("Name"));
            var ordersPropertyConfig = customerConfig.AddNavigationProperty(typeof(Customer).GetProperty("Orders"), EdmMultiplicity.Many);

            var orderConfig = builder.AddEntityType(typeof(Order));

            orderConfig.HasKey(typeof(Order).GetProperty("OrderId"));
            orderConfig.AddProperty(typeof(Order).GetProperty("Cost"));

            var customersSetConfig = builder.AddEntitySet("Customers", customerConfig);
            var ordersSetConfig    = builder.AddEntitySet("Orders", orderConfig);

            customersSetConfig.AddBinding(ordersPropertyConfig, ordersSetConfig);

            var meConfig = builder.AddSingleton("Me", customerConfig);

            var model        = builder.GetServiceModel();
            var customerType = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Customer");

            Assert.NotNull(customerType);
            Assert.Equal(typeof(Customer).Namespace, customerType.Namespace);
            Assert.Equal(3, customerType.DeclaredProperties.Count());

            var key = customerType.DeclaredKey.SingleOrDefault();

            Assert.NotNull(key);
            Assert.Equal("CustomerId", key.Name);
            Assert.True(key.Type.IsInt32());
            Assert.False(key.Type.IsNullable);

            var nameProperty = customerType.DeclaredProperties.SingleOrDefault(dp => dp.Name == "Name");

            Assert.NotNull(nameProperty);
            Assert.True(nameProperty.Type.IsString());
            Assert.True(nameProperty.Type.IsNullable);

            Assert.Equal(1, customerType.NavigationProperties().Count());
            var ordersProperty = customerType.NavigationProperties().Single();

            Assert.Equal("Orders", ordersProperty.Name);
            Assert.Equal(EdmTypeKind.Collection, ordersProperty.Type.Definition.TypeKind);
            Assert.Equal(typeof(Order).FullName, (ordersProperty.Type.Definition as IEdmCollectionType).ElementType.FullName());

            var entityContainer = model.EntityContainer;

            Assert.NotNull(entityContainer);

            var customers = entityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers);
            Assert.Equal(typeof(Customer).FullName, customers.EntityType().FullName());

            var orders = entityContainer.FindEntitySet("Orders");

            Assert.NotNull(orders);

            Assert.Equal(typeof(Order).FullName, orders.EntityType().FullName());

            var me = entityContainer.FindSingleton("Me");

            Assert.NotNull(me);
            Assert.Equal(typeof(Customer).FullName, me.EntityType().FullName());
        }