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 HasConstraint_ThrowsArgumentNull_ForNullPrincipalProperty()
        {
            // Arrange
            Mock <EntityTypeConfiguration>  mockDeclaringType  = new Mock <EntityTypeConfiguration>();
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(new MockPropertyInfo(), EdmMultiplicity.One,
                                                    mockDeclaringType.Object);

            // Act & Assert
            Assert.ThrowsArgumentNull(() => navigationProperty.HasConstraint(new MockPropertyInfo(),
                                                                             principalPropertyInfo: null),
                                      "principalPropertyInfo");
        }
        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));
        }
Пример #5
0
        public NavigationPropertyConfiguration HasRequired <TTargetEntity>(
            Expression <Func <TEntityType, TTargetEntity> > navigationPropertyExpression,
            Expression <Func <TEntityType, TTargetEntity, bool> > referentialConstraintExpression) where TTargetEntity : class
        {
            NavigationPropertyConfiguration navigation = GetOrCreateNavigationProperty(navigationPropertyExpression,
                                                                                       EdmMultiplicity.One);

            IDictionary <PropertyInfo, PropertyInfo> referentialConstraints =
                PropertyPairSelectorVisitor.GetSelectedProperty(referentialConstraintExpression);

            foreach (KeyValuePair <PropertyInfo, PropertyInfo> constraint in referentialConstraints)
            {
                navigation.HasConstraint(constraint);
            }

            return(navigation);
        }
        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 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 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_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 HasConstraint_ThrowsArgumentNull_ForNullPrincipalProperty()
        {
            // Arrange
            Mock<EntityTypeConfiguration> mockDeclaringType = new Mock<EntityTypeConfiguration>();
            NavigationPropertyConfiguration navigationProperty =
                new NavigationPropertyConfiguration(new MockPropertyInfo(), EdmMultiplicity.One,
                    mockDeclaringType.Object);

            // Act & Assert
            Assert.ThrowsArgumentNull(() => navigationProperty.HasConstraint(new MockPropertyInfo(),
                    principalPropertyInfo: null),
                "principalPropertyInfo");
        }
        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));
        }