public void CreateModelUsingProgrammableApi() { var builder = new ODataModelBuilder(); var customerConfig = builder.AddEntity(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.AddEntity(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 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.False(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.EntityContainers().Single(); Assert.NotNull(entityContainer); var customers = entityContainer.FindEntitySet("Customers"); Assert.NotNull(customers); Assert.Equal(typeof(Customer).FullName, customers.ElementType.FullName()); var orders = entityContainer.FindEntitySet("Orders"); Assert.NotNull(orders); Assert.Equal(typeof(Order).FullName, orders.ElementType.FullName()); }
public void CanAddNavigationLink_For_DerivedNavigationProperty() { ODataModelBuilder builder = new ODataModelBuilder(); var vehicle = builder.AddEntity(typeof(Vehicle)); var motorcycle = builder.AddEntity(typeof(Motorcycle)).DerivesFrom(vehicle); var manufacturer = builder.AddEntity(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.EntityContainers().Single().FindEntitySet("vehicles"); Assert.NotNull(model.GetEntitySetLinkBuilder(vehiclesEdmSet)); Assert.Equal( "http://works/", model.GetEntitySetLinkBuilder(vehiclesEdmSet).BuildNavigationLink(new EntityInstanceContext(), edmNavProperty, ODataMetadataLevel.Default).AbsoluteUri); }
public void SettingBaseTypeToNull_AlsoUpdatesBaseTypeConfigured() { var builder = new ODataModelBuilder(); var motorcycle = builder.AddEntity(typeof(Motorcycle)); var vehicle = builder.AddEntity(typeof(Vehicle)); motorcycle.DerivesFromNothing(); Assert.True(motorcycle.BaseTypeConfigured); }
public void ActionLink_PreservesFollowsConventions(bool value) { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration configuration = new ActionConfiguration(builder, "IgnoreAction"); 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, alwaysBindable: false); configuration.HasActionLink((a) => { throw new NotImplementedException(); }, followsConventions: value); builder.AddProcedure(configuration); builder.AddEntity(entityType); // Act IEdmModel model = builder.GetEdmModel(); // Assert IEdmFunctionImport functionImport = model.EntityContainers().Single().Elements.OfType <IEdmFunctionImport>().Single(); ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(functionImport); Assert.Equal(value, actionLinkBuilder.FollowsConventions); }
public void BaseTypeConfigured_IsFalseByDefault() { var builder = new ODataModelBuilder(); var motorcycle = builder.AddEntity(typeof(Motorcycle)); Assert.False(motorcycle.BaseTypeConfigured); }
public void HasConstraint_DoesnotAddTwice_ForSameConstraint() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration entity = builder.AddEntity(typeof(Dependent)); NavigationPropertyConfiguration navigationProperty = new NavigationPropertyConfiguration(typeof(Dependent).GetProperty("Principal"), EdmMultiplicity.ZeroOrOne, entity); PropertyInfo principalProperty = typeof(Principal).GetProperty("PrincipalKey1"); PropertyInfo dependentProperty = typeof(Dependent).GetProperty("DependentKey1"); // Act navigationProperty.HasConstraint(dependentProperty, principalProperty); navigationProperty.HasConstraint(dependentProperty, principalProperty); // Assert PropertyInfo actualInfo = Assert.Single(navigationProperty.DependentProperties); Assert.Same(dependentProperty, actualInfo); PropertyInfo actualPrincipalInfo = Assert.Single(navigationProperty.PrincipalProperties); Assert.Same(principalProperty, actualPrincipalInfo); }
public void AddNavigationLink_For_NavigationPropertyInHierarchy_Throws() { ODataModelBuilder builder = new ODataModelBuilder(); var vehicle = builder.AddEntity(typeof(Vehicle)); var motorcycle = builder.AddEntity(typeof(Motorcycle)); var manufacturer = builder.AddEntity(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.Throws <InvalidOperationException>( () => vehicles.HasNavigationPropertyLink(navProperty, (ctxt, property) => new Uri("http://works/")), "The declaring entity type 'System.Web.Http.OData.Builder.TestModels.Motorcycle' of the given navigation property is not a part of the entity type 'System.Web.Http.OData.Builder.TestModels.Vehicle' hierarchy of the entity set 'vehicles'."); }
public void AddBinding_For_NavigationPropertyInHierarchy_Throws() { ODataModelBuilder builder = new ODataModelBuilder(); var vehicle = builder.AddEntity(typeof(Vehicle)); var motorcycle = builder.AddEntity(typeof(Motorcycle)); var manufacturer = builder.AddEntity(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.Http.OData.Builder.TestModels.Motorcycle' of the given navigation property is not a part of the entity type 'System.Web.Http.OData.Builder.TestModels.Vehicle' hierarchy of the entity set 'vehicles'."); }
public void HasConstraint_Throws_ReferentialConstraintPropertyTypeNotValid() { // Arrange PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("MockPrincipal"); PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("MockDependent"); ODataModelBuilder builder = new ODataModelBuilder(); builder.AddEntity(typeof(Principal)); EntityTypeConfiguration dependentEntity = builder.AddEntity(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.Http.OData.MockType")); }
public void HasConstraint_Throws_DependentAndPrincipalTypeNotMatch() { // Arrange PropertyInfo principalPropertyInfo = typeof(Principal).GetProperty("PrincipalKey2"); PropertyInfo dependentPropertyInfo = typeof(Dependent).GetProperty("DependentKey1"); ODataModelBuilder builder = new ODataModelBuilder(); builder.AddEntity(typeof(Principal)); EntityTypeConfiguration dependentEntity = builder.AddEntity(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 RemoveKey_ThrowsArgumentNull() { // Arrange var builder = new ODataModelBuilder(); var motorcycle = builder.AddEntity(typeof(Motorcycle)); // Act & Assert Assert.ThrowsArgumentNull( () => motorcycle.RemoveKey(keyProperty: null), "keyProperty"); }
public void RemoveStructuralType_RemovesEntityType() { ODataModelBuilder builder = new ODataModelBuilder(); builder.AddEntity(typeof(Customer)); Assert.NotEmpty(builder.StructuralTypes); builder.RemoveStructuralType(typeof(Customer)); Assert.Empty(builder.StructuralTypes); }
public void CanAddBinding_For_DerivedNavigationProperty() { ODataModelBuilder builder = new ODataModelBuilder(); var vehicle = builder.AddEntity(typeof(Vehicle)); var motorcycle = builder.AddEntity(typeof(Motorcycle)).DerivesFrom(vehicle); var manufacturer = builder.AddEntity(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); IEdmModel model = builder.GetEdmModel(); var motorcycleEdmType = model.AssertHasEntityType(typeof(Motorcycle)); var edmNavProperty = motorcycleEdmType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: false, multiplicity: EdmMultiplicity.One); Assert.Equal( "manufacturers", model.EntityContainers().Single().FindEntitySet("vehicles").FindNavigationTarget(edmNavProperty).Name); }
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.AddEntity(typeof(Principal)); EntityTypeConfiguration dependentEntity = builder.AddEntity(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")); }
public void HasConstraint_Throws_NotSupported_ForMany() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration entityType = builder.AddEntity(typeof(Dependent)); NavigationPropertyConfiguration navigationProperty = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(IList <int>), "Users"), EdmMultiplicity.Many, entityType); // Assert & Act Assert.Throws <NotSupportedException>( () => navigationProperty.HasConstraint(new MockPropertyInfo(), new MockPropertyInfo()), String.Format(SRResources.ReferentialConstraintOnManyNavigationPropertyNotSupported, navigationProperty.Name, navigationProperty.DeclaringEntityType.ClrType.FullName)); }
public void RemoveKey_Removes_KeyProperty() { // Arrange var builder = new ODataModelBuilder(); var motorcycle = builder.AddEntity(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); }
public void HasNavigationPropertyLink_CanReplaceExistingLinks() { // Arrange var entity = _builder.AddEntity(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)); }
public EntitySetLinkBuilderAnnotationTest() { _modelBuilder = new ODataModelBuilder(); _entitySet = _modelBuilder.AddEntitySet("Customers", _modelBuilder.AddEntity(typeof(Customer))); }