Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void CanAddNavigationLink_For_DerivedNavigationProperty()
        {
            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 manufacturers = builder.AddEntitySet("manufacturers", manufacturer);
            var navProperty   = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.One);

            var vehicles = builder.AddEntitySet("vehicles", vehicle);
            var binding  = vehicles.AddBinding(navProperty, manufacturers);

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

            IEdmModel model             = builder.GetEdmModel();
            var       motorcycleEdmType = model.AssertHasEntityType(typeof(Motorcycle));
            var       edmNavProperty    = motorcycleEdmType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: false, multiplicity: EdmMultiplicity.One);
            var       vehiclesEdmSet    = model.EntityContainer.FindEntitySet("vehicles");

            Assert.NotNull(model.GetNavigationSourceLinkBuilder(vehiclesEdmSet));
            Assert.Equal(
                "http://works/",
                model.GetNavigationSourceLinkBuilder(vehiclesEdmSet).BuildNavigationLink(new ResourceContext(), edmNavProperty, ODataMetadataLevel.MinimalMetadata).AbsoluteUri);
        }
Exemplo n.º 3
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);
        }
        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());
        }
Exemplo n.º 5
0
        public void SettingBaseTypeToNull_AlsoUpdatesBaseTypeConfigured()
        {
            var builder    = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));
            var vehicle    = builder.AddEntityType(typeof(Vehicle));

            motorcycle.DerivesFromNothing();

            Assert.True(motorcycle.BaseTypeConfigured);
        }
Exemplo n.º 6
0
        public static IEdmModel GetEdmModel()
        {
            //ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var builder = new ODataModelBuilder {
                Namespace = typeof(Person).Namespace, ContainerName = "DefaultContainer"
            };

            ComplexTypeConfiguration <Range> rangeType = builder.ComplexType <Range>();

            //rangeType.Select();
            rangeType.Property(c => c.minValue);
            rangeType.Property(c => c.maxValue);

            var personConfig = builder.AddEntityType(typeof(Person));

            personConfig.HasKey(typeof(Person).GetProperty("ID"));
            personConfig.AddProperty(typeof(Person).GetProperty("Name"));
            personConfig.AddNavigationProperty(typeof(Person).GetProperty("Car"), EdmMultiplicity.ZeroOrOne);
            //personConfig.AddComplexProperty(typeof(Person).GetProperty("minValue"));
            //personConfig.AddComplexProperty(typeof(Person).GetProperty("maxValue"));
            personConfig.AddComplexProperty(typeof(Person).GetProperty("Ranges"));

            var carConfig = builder.AddEntityType(typeof(Car));

            carConfig.HasKey(typeof(Car).GetProperty("ID"));
            carConfig.AddProperty(typeof(Car).GetProperty("AmountMade"));
            carConfig.AddProperty(typeof(Car).GetProperty("APK"));
            carConfig.AddProperty(typeof(Car).GetProperty("Name"));
            carConfig.AddProperty(typeof(Car).GetProperty("TimeWhenAddedToDatabase"));
            carConfig.AddEnumProperty(typeof(Car).GetProperty("Brand"));
            carConfig.AddNavigationProperty(typeof(Car).GetProperty("People"), EdmMultiplicity.Many);

            var _Brands = builder.AddEnumType(typeof(_Brands));

            _Brands.AddMember(Models._Brands.Audi);
            _Brands.AddMember(Models._Brands.BMW);
            _Brands.AddMember(Models._Brands.Ferrari);
            _Brands.AddMember(Models._Brands.Ford);
            _Brands.AddMember(Models._Brands.Honda);
            _Brands.AddMember(Models._Brands.Mercedes);
            _Brands.AddMember(Models._Brands.Mini);
            _Brands.AddMember(Models._Brands.Nissan);
            _Brands.AddMember(Models._Brands.Porsche);
            _Brands.AddMember(Models._Brands.Tesla);
            _Brands.AddMember(Models._Brands.Toyota);
            _Brands.AddMember(Models._Brands.Volkswagen);

            builder.AddEntitySet("people", personConfig);
            builder.AddEntitySet("cars", carConfig);

            var edmModel = builder.GetEdmModel();

            //return builder.GetEdmModel();
            return(edmModel);
        }
        public void GetTargetNavigationSource_Returns_TargetSingleton()
        {
            // Arrange
            ODataModelBuilder               builder            = new ODataModelBuilder();
            EntityTypeConfiguration         companyType        = builder.AddEntityType(typeof(Company));
            EntityTypeConfiguration         employeeType       = builder.AddEntityType(typeof(Employee));
            NavigationPropertyConfiguration navigationProperty = companyType.AddNavigationProperty(typeof(Company).GetProperty("CEO"), EdmMultiplicity.One);
            SingletonConfiguration          gazes = builder.AddSingleton("Gazes", employeeType);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Same(gazes, targetNavigationSource);
        }
        public void GetTargetNavigationSource_Returns_TargetEntitySet()
        {
            // Arrange
            ODataModelBuilder               builder            = new ODataModelBuilder();
            EntityTypeConfiguration         motorcycle         = builder.AddEntityType(typeof(Motorcycle));
            EntityTypeConfiguration         manufacturer       = builder.AddEntityType(typeof(MotorcycleManufacturer));
            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);
            EntitySetConfiguration          manufacturers      = builder.AddEntitySet("manufacturers", manufacturer);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Same(manufacturers, targetNavigationSource);
        }
Exemplo n.º 9
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
            ExceptionAssert.DoesNotThrow(() => myVehicle.AddBinding(navProperty, fordo));
        }
Exemplo n.º 10
0
        public void FunctionLink_PreservesFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder     builder       = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            FunctionConfiguration configuration = new FunctionConfiguration(builder, "IgnoreFunction");

            configuration.Returns <int>();
            Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>();

            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            Type entityType = typeof(object);

            bindingParameterTypeMock.Setup(o => o.ClrType).Returns(entityType);
            configuration.SetBindingParameter("IgnoreParameter", bindingParameterTypeMock.Object);
            configuration.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);
            builder.AddOperation(configuration);
            builder.AddEntityType(entityType);

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

            // Assert
            var function = Assert.Single(model.SchemaElements.OfType <IEdmFunction>());
            OperationLinkBuilder functionLinkBuilder = model.GetOperationLinkBuilder(function);

            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(value, functionLinkBuilder.FollowsConventions);
        }
Exemplo n.º 11
0
        public void BaseTypeConfigured_IsFalseByDefault()
        {
            var builder    = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));

            Assert.False(motorcycle.BaseTypeConfigured);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public void AddBinding_For_NavigationPropertyInHierarchy_DoesnotThrows()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicle       = builder.AddEntityType(typeof(Vehicle));
            var motorcycle    = builder.AddEntityType(typeof(Motorcycle));
            var manufacturer  = builder.AddEntityType(typeof(MotorcycleManufacturer));
            var manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

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

            var vehicles = builder.AddEntitySet("vehicles", vehicle);

            // Act & Assert
            ExceptionAssert.DoesNotThrow(() => vehicles.AddBinding(navProperty, manufacturers));
        }
Exemplo n.º 14
0
        public void HasConstraint_Throws_ReferentialConstraintPropertyTypeNotValid()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("MockPrincipal");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("MockDependent");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act & Assert
            ExceptionAssert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.ReferentialConstraintPropertyTypeNotValid, "Microsoft.AspNet.OData.Test.Common.MockType"));
        }
Exemplo n.º 15
0
        public void HasConstraint_Throws_DependentAndPrincipalTypeNotMatch()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey2");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act & Assert
            ExceptionAssert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.DependentAndPrincipalTypeNotMatch, "System.Int32", "System.String"));
        }
        public void RemoveStructuralType_RemovesEntityType()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Customer));

            Assert.NotEmpty(builder.StructuralTypes);

            builder.RemoveStructuralType(typeof(Customer));
            Assert.Empty(builder.StructuralTypes);
        }
Exemplo n.º 17
0
        public void HasConstraint_CanSetDependentAndPrincipalProperty()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey1");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act
            navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo);

            // Assert
            Assert.Same(principalPropertyInfo, Assert.Single(navigationProperty.PrincipalProperties));
            Assert.Same(dependentPropertyInfo, Assert.Single(navigationProperty.DependentProperties));
        }
Exemplo n.º 18
0
        public void RemoveStructuralType_RemovesEntityType()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.AddEntityType(typeof(Customer));

            Assert.NotEmpty(builder.StructuralTypes);

            builder.RemoveStructuralType(typeof(Customer));
            Assert.Empty(builder.StructuralTypes);
        }
Exemplo n.º 19
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
            ExceptionAssert.ThrowsArgument(
                () => myVehicle.HasNavigationPropertyLink(navProperty, new NavigationLinkBuilder((ctxt, property) => new Uri("http://works/"), followsConventions: false)),
                "navigationProperty",
                "The declaring entity type 'Microsoft.AspNet.OData.Test.Builder.TestModels.Car' of the given navigation property is not a part of the " +
                "entity type 'Microsoft.AspNet.OData.Test.Builder.TestModels.Vehicle' hierarchy of the entity set or singleton 'MyVehicle'.");
        }
Exemplo n.º 20
0
        public void RemoveEnumKey_ThrowsArgumentNull()
        {
            // Arrange
            var builder    = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => motorcycle.RemoveKey(enumKeyProperty: null),
                "enumKeyProperty");
        }
Exemplo n.º 21
0
        public void AddNavigationLink_For_NavigationPropertyInHierarchy_Throws()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicle       = builder.AddEntityType(typeof(Vehicle));
            var motorcycle    = builder.AddEntityType(typeof(Motorcycle));
            var manufacturer  = builder.AddEntityType(typeof(MotorcycleManufacturer));
            var manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

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

            var vehicles = builder.AddEntitySet("vehicles", vehicle);

            ExceptionAssert.ThrowsArgument(
                () => vehicles.HasNavigationPropertyLink(navProperty, new NavigationLinkBuilder((ctxt, property) => new Uri("http://works/"), followsConventions: false)),
                "navigationProperty",
                "The declaring entity type 'Microsoft.AspNet.OData.Test.Builder.TestModels.Motorcycle' " +
                "of the given navigation property is not a part of the entity type " +
                "'Microsoft.AspNet.OData.Test.Builder.TestModels.Vehicle' hierarchy of the entity set or singleton 'vehicles'.");
        }
        public void AssociationSetDiscoveryConvention_AddsBindingForBaseAndDerivedNavigationProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            EntityTypeConfiguration vehicle = builder.AddEntityType(typeof(Vehicle));

            EntityTypeConfiguration         car = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration carNavigationProperty = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration         motorcycle = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration motorcycleNavigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration manufacturer           = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);
            EntityTypeConfiguration carManufacturer        = builder.AddEntityType(typeof(CarManufacturer)).DerivesFrom(manufacturer);

            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);


            Mock <EntitySetConfiguration> entitySet = new Mock <EntitySetConfiguration>(MockBehavior.Strict);

            entitySet.Setup(v => v.EntityType).Returns(vehicle);
            entitySet.Setup(v => v.AddBinding(motorcycleNavigationProperty, manufacturers)).Returns <NavigationPropertyConfiguration>(null);
            entitySet.Setup(v => v.AddBinding(carNavigationProperty, manufacturers)).Returns <NavigationPropertyConfiguration>(null);

            // Act
            _convention.Apply(entitySet.Object, builder);

            // Assert
            entitySet.VerifyAll();
        }
        public void AssociationSetDiscoveryConvention_AddsBindingForBaseAndDerivedNavigationProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            EntityTypeConfiguration vehicle = builder.AddEntityType(typeof(Vehicle));

            EntityTypeConfiguration car = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration carNavigationProperty = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration motorcycleNavigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);
            EntityTypeConfiguration carManufacturer = builder.AddEntityType(typeof(CarManufacturer)).DerivesFrom(manufacturer);

            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);


            Mock<EntitySetConfiguration> entitySet = new Mock<EntitySetConfiguration>(MockBehavior.Strict);
            entitySet.Setup(v => v.EntityType).Returns(vehicle);
            entitySet.Setup(v => v.AddBinding(motorcycleNavigationProperty, manufacturers)).Returns<NavigationPropertyConfiguration>(null);
            entitySet.Setup(v => v.AddBinding(carNavigationProperty, manufacturers)).Returns<NavigationPropertyConfiguration>(null);

            // Act
            _convention.Apply(entitySet.Object, builder);

            // Assert
            entitySet.VerifyAll();
        }
Exemplo n.º 24
0
        public void HasConstraint_Throws_ReferentialConstraintAlreadyConfigured_Principal()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey1");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");
            PropertyInfo otherPrincipalPropertyInfo = typeof(Dependent).GetProperty("DependentKey2");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo);

            // Act & Assert
            ExceptionAssert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(otherPrincipalPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.ReferentialConstraintAlreadyConfigured,
                "principal", "PrincipalKey1", "dependent", "DependentKey1"));
        }
        public void GetTargetNavigationSource_Returns_Null_IfNoMatchingTargetNavigaitonSource()
        {
            // Arrange
            ODataModelBuilder               builder            = new ODataModelBuilder();
            EntityTypeConfiguration         motorcycle         = builder.AddEntityType(typeof(Motorcycle));
            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Null(targetNavigationSource);
        }
        public void GetTargetNavigationSource_Returns_Null_IfNoMatchingTargetNavigaitonSource()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle));
            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            // Act
           NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
           Assert.Null(targetNavigationSource);
        }
Exemplo n.º 27
0
        static void RegisterCustomODataServiceModels(ODataModelBuilder builder)
        {
            IServiceManager serviceManager = Provider.ServiceManager();

            Application[] apps = serviceManager.GetApplications();

            object[] models = null;

            if (apps != null)
            {
                foreach (var app in apps)
                {
                    if (app != null)
                    {
                        foreach (var service in app.Services)
                        {
                            try
                            {
                                //models = serviceManager.GetEntityModels(service, app);

                                if (service.ServiceType == ServiceType.Custom)
                                {
                                    models = serviceManager.GetEntityModels(service);
                                }

                                //models = serviceManager.GetEntityModels(service);
                            }
                            catch (Exception ex)
                            {
                                models = null;
                            }

                            if (models != null)
                            {
                                foreach (var model in models)
                                {
                                    if (model is IODataEntityModel <ODataModelBuilder> )
                                    {
                                        //(model as IODataEntityModel<ODataModelBuilder>).Register(builder);

                                        //builder.AddEntitySet(model.GetType().Name + "s", builder.AddEntityType(model.GetType()));

                                        builder.AddEntitySet(model.GetType().Name, builder.AddEntityType(model.GetType()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 28
0
        public void CanAddBinding_For_DerivedNavigationProperty()
        {
            // 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 manufacturers = builder.AddEntitySet("manufacturers", manufacturer);
            var navProperty   = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.One);

            var vehicles = builder.AddEntitySet("vehicles", vehicle);

            vehicles.AddBinding(navProperty, manufacturers);

            // 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);

            IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("vehicles");

            Assert.NotNull(entitySet);

            var target = entitySet.FindNavigationTarget(edmNavProperty);

            Assert.NotNull(target);
            Assert.Equal("manufacturers", target.Name);

            var binding = Assert.Single(entitySet.FindNavigationPropertyBindings(edmNavProperty));

            Assert.Same(target, binding.Target);
            Assert.Equal("Microsoft.AspNet.OData.Test.Builder.TestModels.Motorcycle/Manufacturer", binding.Path.Path);
        }
Exemplo n.º 29
0
        // TODO: customize the way the key is identified
        private void AddEntityType(ODataModelBuilder builder, Type type)
        {
            var properties = type.GetProperties();
            var key        = properties.Where(p => p.PropertyType == typeof(Guid)).FirstOrDefault();

            if (key == null)
            {
                key = properties.First();
            }

            var edmSetConfig  = builder.AddEntitySet(type.Name, new EntityTypeConfiguration(builder, type));
            var edmTypeConfig = builder.AddEntityType(type);

            edmTypeConfig.HasKey(key);
        }
        public void Apply_DoesnotRemove_ExplicitlyAddedProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            PropertyInfo propertyInfo = typeof(TestEntity).GetProperty("Property");
            EntityTypeConfiguration entity = builder.AddEntityType(typeof(TestEntity));
            PropertyConfiguration property = entity.AddProperty(propertyInfo);

            // Act
            new NotMappedAttributeConvention().Apply(property, entity);

            // Assert
            Assert.Contains(propertyInfo, entity.ExplicitProperties.Keys);
            Assert.DoesNotContain(propertyInfo, entity.RemovedProperties);
        }
Exemplo n.º 31
0
        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);
        }
Exemplo n.º 32
0
        public void RemoveEnumKey_Removes_EnumKeyProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            var enumEntityType        = builder.AddEntityType(typeof(EnumModel));

            enumEntityType.HasKey(typeof(EnumModel).GetProperty("Simple"));

            EnumPropertyConfiguration enumProperty =
                enumEntityType.AddEnumProperty(typeof(EnumModel).GetProperty("Simple"));

            Assert.Equal(new[] { enumProperty }, enumEntityType.EnumKeys); // Guard

            // Act
            enumEntityType.RemoveKey(enumProperty);

            // Assert
            Assert.Empty(enumEntityType.EnumKeys);
        }
Exemplo n.º 33
0
        public void HasNavigationPropertyLink_CanReplaceExistingLinks()
        {
            // Arrange
            var entity             = _builder.AddEntityType(typeof(Motorcycle));
            var navigationProperty = entity.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.One);
            var entityset          = _builder.AddEntitySet("vehicles", entity);
            Uri link1 = new Uri("http://link1");
            Uri link2 = new Uri("http://link2");

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

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

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

            Assert.False(navigationLink.FollowsConventions);
            Assert.Equal(link2, navigationLink.Factory(null, null));
        }
        static EntitySetConfiguration CreateEntitySet(XPClassInfo classInfo, ODataModelBuilder builder)
        {
            EntitySetConfiguration entitySetConfig = builder.EntitySets.FirstOrDefault(t => t.EntityType.ClrType == classInfo.ClassType);

            if (entitySetConfig != null)
            {
                return(entitySetConfig);
            }
            EntityTypeConfiguration entityTypeConfig = builder.AddEntityType(classInfo.ClassType);

            entitySetConfig = builder.AddEntitySet(classInfo.ClassType.Name, entityTypeConfig);
            if (classInfo.PersistentBaseClass != null)
            {
                EntitySetConfiguration baseClassEntitySetConfig = CreateEntitySet(classInfo.PersistentBaseClass, builder);
                entityTypeConfig.DerivesFrom(baseClassEntitySetConfig.EntityType);
            }
            else
            {
                if (classInfo.KeyProperty is ReflectionFieldInfo)
                {
                    foreach (XPMemberInfo mi in classInfo.Members)
                    {
                        if (mi.IsAliased)
                        {
                            string aliasedExpr     = ((PersistentAliasAttribute)mi.GetAttributeInfo(typeof(PersistentAliasAttribute))).AliasExpression;
                            var    aliasedCriteria = CriteriaOperator.Parse(aliasedExpr) as OperandProperty;
                            if (!ReferenceEquals(null, aliasedCriteria) && aliasedCriteria.PropertyName == classInfo.KeyProperty.Name)
                            {
                                entityTypeConfig.HasKey(classInfo.ClassType.GetProperty(mi.Name));
                                break;
                            }
                        }
                    }
                }
                else
                {
                    entityTypeConfig.HasKey(classInfo.ClassType.GetProperty(classInfo.KeyProperty.Name));
                }
            }
            return(entitySetConfig);
        }
        static EntitySetConfiguration CreateEntitySet(XPClassInfo classInfo, ODataModelBuilder builder)
        {
            EntitySetConfiguration entitySetConfig = builder.EntitySets.FirstOrDefault(t => t.EntityType.ClrType == classInfo.ClassType);

            if (entitySetConfig != null)
            {
                return(entitySetConfig);
            }
            EntityTypeConfiguration entityTypeConfig = builder.AddEntityType(classInfo.ClassType);

            entitySetConfig = builder.AddEntitySet(classInfo.ClassType.Name, entityTypeConfig);
            if (classInfo.PersistentBaseClass != null)
            {
                EntitySetConfiguration baseClassEntitySetConfig = CreateEntitySet(classInfo.PersistentBaseClass, builder);
                entityTypeConfig.DerivesFrom(baseClassEntitySetConfig.EntityType);
            }
            else
            {
                entityTypeConfig.HasKey(classInfo.ClassType.GetProperty(classInfo.KeyProperty.Name));
            }
            return(entitySetConfig);
        }
        public void Apply_AddsNavigationLinkFor_AllBaseDeclaredAndDerivedProperties()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            EntityTypeConfiguration vehicle = builder.AddEntityType(typeof(Vehicle));

            EntityTypeConfiguration car = builder.AddEntityType(typeof(Car)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration carNavigationProperty = car.AddNavigationProperty(typeof(Car).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle)).DerivesFrom(vehicle);
            NavigationPropertyConfiguration motorcycleNavigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);

            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);
            EntityTypeConfiguration carManufacturer = builder.AddEntityType(typeof(CarManufacturer)).DerivesFrom(manufacturer);

            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);


            Mock<EntitySetConfiguration> entitySet = new Mock<EntitySetConfiguration>(MockBehavior.Strict);
            entitySet.Setup(v => v.EntityType).Returns(vehicle);
            entitySet.Setup(v => v.GetNavigationPropertyLink(motorcycleNavigationProperty)).Returns<NavigationPropertyConfiguration>(null);
            entitySet.Setup(v => v.GetNavigationPropertyLink(carNavigationProperty)).Returns<NavigationPropertyConfiguration>(null);

            entitySet
                .Setup(v => v.HasNavigationPropertyLink(motorcycleNavigationProperty, It.IsAny<NavigationLinkBuilder>()))
                .Callback((NavigationPropertyConfiguration property, NavigationLinkBuilder navBuilder) => Assert.True(navBuilder.FollowsConventions))
                .Returns<EntitySetConfiguration>(null);
            entitySet
                .Setup(v => v.HasNavigationPropertyLink(carNavigationProperty, It.IsAny<NavigationLinkBuilder>()))
                .Returns<EntitySetConfiguration>(null);

            // Act
            _convention.Apply(entitySet.Object, builder);

            // Assert
            entitySet.VerifyAll();
        }
Exemplo n.º 37
0
        public void SettingBaseTypeToNull_AlsoUpdatesBaseTypeConfigured()
        {
            var builder = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));
            var vehicle = builder.AddEntityType(typeof(Vehicle));

            motorcycle.DerivesFromNothing();

            Assert.True(motorcycle.BaseTypeConfigured);
        }
Exemplo n.º 38
0
        public void RemoveKey_ThrowsArgumentNull()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => motorcycle.RemoveKey(keyProperty: null),
                "keyProperty");
        }
Exemplo n.º 39
0
        public void AddBinding_For_NavigationPropertyInHierarchy_Throws()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicle = builder.AddEntityType(typeof(Vehicle));
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));
            var manufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer));
            var manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

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

            var vehicles = builder.AddEntitySet("vehicles", vehicle);

            Assert.ThrowsArgument(
                () => vehicles.AddBinding(navProperty, manufacturers),
                "navigationConfiguration",
                "The declaring entity type 'System.Web.OData.Builder.TestModels.Motorcycle' 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 'vehicles'.");
        }
        public void GetTargetEntitySet_Returns_BaseTypeEntitySet_IfNoMatchingEntitysetForCurrentType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle));
            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(Manufacturer));
            EntityTypeConfiguration motorcycleManufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer)).DerivesFrom(manufacturer);

            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);
            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

            // Act
            EntitySetConfiguration targetEntitySet = AssociationSetDiscoveryConvention.GetTargetEntitySet(navigationProperty, builder);

            // Assert
            Assert.Equal(manufacturers, targetEntitySet);
        }
        public void HasConstraint_Throws_DependentAndPrincipalTypeNotMatch()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey2");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.DependentAndPrincipalTypeNotMatch, "System.Int32", "System.String"));
        }
        public void GetTargetNavigationSource_Returns_Null_IfMultipleMatchingTargetSingleton()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration companyType = builder.AddEntityType(typeof(Company));
            EntityTypeConfiguration employeeType = builder.AddEntityType(typeof(Employee));
            NavigationPropertyConfiguration navigationProperty = companyType.AddNavigationProperty(typeof(Company).GetProperty("CEO"), EdmMultiplicity.One);
            SingletonConfiguration gazes1 = builder.AddSingleton("Gazes1", employeeType);
            SingletonConfiguration gazes2 = builder.AddSingleton("Gazes2", employeeType);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Null(targetNavigationSource);
        }
        public void HasConstraint_Throws_ReferentialConstraintPropertyTypeNotValid()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("MockPrincipal");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("MockDependent");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.ReferentialConstraintPropertyTypeNotValid, "System.Web.OData.MockType"));
        }
 public NavigationSourceLinkBuilderAnnotationTest()
 {
     _modelBuilder = ODataModelBuilderMocks.GetModelBuilderMock<ODataModelBuilder>();
     _entitySet = _modelBuilder.AddEntitySet("Customers", _modelBuilder.AddEntityType(typeof(Customer)));
 }
        public void GetTargetEntitySet_Returns_Null_IfMultipleMatchingTargetEntitySet()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle));
            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer));
            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);
            EntitySetConfiguration manufacturers1 = builder.AddEntitySet("manufacturers1", manufacturer);
            EntitySetConfiguration manufacturers2 = builder.AddEntitySet("manufacturers2", manufacturer);

            // Act
            EntitySetConfiguration targetEntitySet = AssociationSetDiscoveryConvention.GetTargetEntitySet(navigationProperty, builder);

            // Assert
            Assert.Null(targetEntitySet);
        }
Exemplo n.º 46
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'.");
        }
Exemplo n.º 47
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'.");
        }
        public void HasConstraint_Throws_ReferentialConstraintAlreadyConfigured_Principal()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey1");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");
            PropertyInfo otherPrincipalPropertyInfo = typeof(Dependent).GetProperty("DependentKey2");

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => navigationProperty.HasConstraint(otherPrincipalPropertyInfo, principalPropertyInfo),
                String.Format(SRResources.ReferentialConstraintAlreadyConfigured,
                "principal", "PrincipalKey1", "dependent", "DependentKey1"));
        }
Exemplo n.º 49
0
        public void BaseTypeConfigured_IsFalseByDefault()
        {
            var builder = new ODataModelBuilder();
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));

            Assert.False(motorcycle.BaseTypeConfigured);
        }
        public void GetTargetNavigationSource_Returns_TargetEntitySet()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration motorcycle = builder.AddEntityType(typeof(Motorcycle));
            EntityTypeConfiguration manufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer));
            NavigationPropertyConfiguration navigationProperty = motorcycle.AddNavigationProperty(typeof(Motorcycle).GetProperty("Manufacturer"), EdmMultiplicity.ZeroOrOne);
            EntitySetConfiguration manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

            // Act
            NavigationSourceConfiguration targetNavigationSource = AssociationSetDiscoveryConvention.GetTargetNavigationSource(navigationProperty, builder);

            // Assert
            Assert.Same(manufacturers, targetNavigationSource);
        }
        public void HasConstraint_CanSetDependentAndPrincipalProperty_OnlyOnceForTheSameConstraint()
        {
            // Arrange
            PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey1");
            PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1");
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.AddEntityType(typeof(Principal));
            EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(Dependent));
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.One,
                    dependentEntity);

            // Act
            navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo);
            navigationProperty.HasConstraint(dependentPropertyInfo, principalPropertyInfo);
            navigationProperty.HasConstraint(new KeyValuePair<PropertyInfo, PropertyInfo>(dependentPropertyInfo,
                principalPropertyInfo));

            // Assert
            Assert.Single(navigationProperty.PrincipalProperties);
            Assert.Single(navigationProperty.DependentProperties);
        }
Exemplo n.º 52
0
        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);
        }
Exemplo n.º 53
0
        public void RemoveEnumKey_Removes_EnumKeyProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            var enumEntityType = builder.AddEntityType(typeof(EnumModel));
            enumEntityType.HasKey(typeof(EnumModel).GetProperty("Simple"));

            EnumPropertyConfiguration enumProperty =
                enumEntityType.AddEnumProperty(typeof(EnumModel).GetProperty("Simple"));

            Assert.Equal(new[] { enumProperty }, enumEntityType.EnumKeys); // Guard

            // Act
            enumEntityType.RemoveKey(enumProperty);

            // Assert
            Assert.Empty(enumEntityType.EnumKeys);
        }
Exemplo n.º 54
0
        public void AddNavigationLink_For_NavigationPropertyInHierarchy_Throws()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            var vehicle = builder.AddEntityType(typeof(Vehicle));
            var motorcycle = builder.AddEntityType(typeof(Motorcycle));
            var manufacturer = builder.AddEntityType(typeof(MotorcycleManufacturer));
            var manufacturers = builder.AddEntitySet("manufacturers", manufacturer);

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

            var vehicles = builder.AddEntitySet("vehicles", vehicle);

            Assert.ThrowsArgument(
                () => vehicles.HasNavigationPropertyLink(navProperty, new NavigationLinkBuilder((ctxt, property) => new Uri("http://works/"), followsConventions: false)),
                "navigationProperty",
                "The declaring entity type 'System.Web.OData.Builder.TestModels.Motorcycle' " +
                "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 'vehicles'.");
        }