예제 #1
0
        public void ParserTestDuplicateComplexTypes()
        {
            var csdls = ODataTestModelBuilder.InvalidCsdl.DuplicateComplexTypes;
            var model = this.GetParserResult(csdls);

            IEdmEntityContainer entityContainer = model.EntityContainer;

            Assert.AreEqual("DefaultContainer", entityContainer.Name, "Invalid entity container name");
            Assert.IsTrue(entityContainer.Elements.Count() == 0, "Entity container has invalid amount of elements");

            Assert.IsTrue(model.SchemaElements.Count() == 3, "Invalid schema element count");
            Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, model.SchemaElements.ElementAt(0).SchemaElementKind, "Invalid schema element kind");
            Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, model.SchemaElements.ElementAt(1).SchemaElementKind, "Invalid schema element kind");

            IEdmComplexType complexTypeElement1 = (IEdmComplexType)model.SchemaElements.ElementAt(0);

            Assert.AreEqual("TestModel.DuplicateComplexTypes", complexTypeElement1.FullName(), "Invalid complex type full name");
            Assert.AreEqual("DuplicateComplexTypes", complexTypeElement1.Name, "Invalid complex type name");

            IEdmComplexType complexTypeElement2 = (IEdmComplexType)model.SchemaElements.ElementAt(1);

            Assert.AreEqual("TestModel.DuplicateComplexTypes", complexTypeElement2.FullName(), "Invalid complex type full name");
            Assert.AreEqual("DuplicateComplexTypes", complexTypeElement2.Name, "Invalid complex type name");

            var expectedErrors = new EdmLibTestErrors()
            {
                { 7, 6, EdmErrorCode.AlreadyDefined }
            };

            IEnumerable <EdmError> actualErrors = null;

            model.Validate(out actualErrors);
            this.CompareErrors(actualErrors, expectedErrors);
        }
예제 #2
0
        public void CreateComplexTypeWith_OneToOneOrZero_NavigationProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Customer>().HasKey(c => c.CustomerId);

            ComplexTypeConfiguration <Order> order = builder.ComplexType <Order>();

            order.HasOptional(o => o.Customer);

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

            // Assert
            IEdmEntityType customerType = Assert.Single(model.SchemaElements.OfType <IEdmEntityType>());

            Assert.Equal("System.Web.OData.Builder.TestModels.Customer", customerType.FullName());
            Assert.Equal("CustomerId", customerType.DeclaredKey.Single().Name);
            Assert.Equal(1, customerType.DeclaredProperties.Count());
            Assert.Empty(customerType.NavigationProperties());

            IEdmComplexType orderType = Assert.Single(model.SchemaElements.OfType <IEdmComplexType>());

            Assert.Equal("System.Web.OData.Builder.TestModels.Order", orderType.FullName());

            IEdmNavigationProperty navProperty = Assert.Single(orderType.NavigationProperties());

            Assert.Equal(EdmMultiplicity.ZeroOrOne, navProperty.TargetMultiplicity());
            Assert.Equal("Customer", navProperty.Name);
            Assert.True(navProperty.Type.IsEntity());
            Assert.Same(customerType, navProperty.Type.Definition);
        }
예제 #3
0
        public void CreateComplexTypeWith_OneToMany_NavigationProperty()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Order>().HasKey(o => o.OrderId);

            ComplexTypeConfiguration <Customer> customer = builder.ComplexType <Customer>();

            customer.HasMany(c => c.Orders);

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

            // Assert
            IEdmEntityType orderType = Assert.Single(model.SchemaElements.OfType <IEdmEntityType>());

            Assert.Equal("Microsoft.AspNet.OData.Test.Builder.TestModels.Order", orderType.FullName());
            Assert.Equal("OrderId", orderType.DeclaredKey.Single().Name);
            Assert.Single(orderType.DeclaredProperties);
            Assert.Empty(orderType.NavigationProperties());

            IEdmComplexType customerType = Assert.Single(model.SchemaElements.OfType <IEdmComplexType>());

            Assert.Equal("Microsoft.AspNet.OData.Test.Builder.TestModels.Customer", customerType.FullName());

            IEdmNavigationProperty navProperty = Assert.Single(customerType.NavigationProperties());

            Assert.Equal(EdmMultiplicity.Many, navProperty.TargetMultiplicity());
            Assert.Equal("Orders", navProperty.Name);
            Assert.True(navProperty.Type.IsCollection());
            Assert.Same(orderType, navProperty.Type.AsCollection().ElementType().Definition);
        }
        internal static object CreateResource(IEdmComplexType edmComplexType, IEdmModel edmModel)
        {
            Type clrType = EdmLibHelpers.GetClrType(new EdmComplexTypeReference(edmComplexType, isNullable: true), edmModel);
            if (clrType == null)
            {
                throw Error.Argument("edmComplexType", SRResources.MappingDoesNotContainEntityType, edmComplexType.FullName());
            }

            return Activator.CreateInstance(clrType);
        }
        private void FillStockContentsForComplex(IEdmComplexType edmType, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockType = (EdmComplexType)stockModel.FindType(edmType.FullName());

            this.SetImmediateAnnotations(edmType, stockType, edmModel, stockModel);

            foreach (var edmProperty in edmType.DeclaredStructuralProperties())
            {
                ConvertToStockStructuralProperty((IEdmStructuralProperty)edmProperty, edmModel, stockModel);
            }
        }
        public void ConvertComplexType_Nested()
        {
            var taupoModel = new EntityModelSchema()
            {
                new ComplexType("NS1", "Complex1")
                {
                    new MemberProperty("p1", DataTypes.ComplexType.WithName("NS2", "NestedComplex")),
                },
                new ComplexType("NS2", "NestedComplex")
                {
                    new MemberProperty("p1", EdmDataTypes.Int16),
                },
            }
            .Resolve();

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(2, result.SchemaElements.Count());
            Assert.AreEqual(2, result.SchemaElements.OfType <IEdmComplexType>().Count());

            IEdmComplexType complex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(0);

            Assert.AreEqual("NS1.Complex1", complex.FullName());
            Assert.AreEqual(1, complex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p1", complex.DeclaredStructuralProperties().First().Name);

            IEdmTypeReference propertyType = complex.DeclaredStructuralProperties().First().Type;

            Assert.AreEqual("NS2.NestedComplex", propertyType.FullName());
            Assert.AreEqual(EdmTypeKind.Complex, propertyType.TypeKind());

            complex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(1);

            Assert.AreEqual("NS2.NestedComplex", complex.FullName());
            Assert.AreEqual(1, complex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p1", complex.DeclaredStructuralProperties().First().Name);
        }
        private EdmComplexType ConstructStockComplexTypeInModel(IEdmComplexType complexType, EdmModel stockModel, Dictionary <string, EdmComplexType> stockComplexTypes)
        {
            EdmComplexType stockType;
            string         fullName = complexType.FullName();

            if (!stockComplexTypes.TryGetValue(fullName, out stockType))
            {
                stockType = new EdmComplexType(
                    complexType.Namespace,
                    complexType.Name,
                    complexType.BaseType != null ? this.ConstructStockComplexTypeInModel((IEdmComplexType)complexType.BaseType, stockModel, stockComplexTypes) : null,
                    complexType.IsAbstract);

                // TODO: IsBad, Documentation
                stockModel.AddElement(stockType);
                stockComplexTypes.Add(fullName, stockType);
            }

            return(stockType);
        }
예제 #8
0
        public void ParserTestComplexTypeWithDuplicateProperties()
        {
            var csdls = ODataTestModelBuilder.InvalidCsdl.ComplexTypeWithDuplicateProperties;
            var model = this.GetParserResult(csdls);

            IEdmEntityContainer entityContainer = model.EntityContainer;

            Assert.AreEqual("DefaultContainer", entityContainer.Name, "Invalid entity container name");
            Assert.IsTrue(entityContainer.Elements.Count() == 0, "Entity container has invalid amount of elements");

            Assert.IsTrue(model.SchemaElements.Count() == 2, "Invalid schema element count");
            Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, model.SchemaElements.ElementAt(0).SchemaElementKind, "Invalid schema element kind");

            IEdmComplexType complexTypeElement = (IEdmComplexType)model.SchemaElements.ElementAt(0);

            Assert.AreEqual("TestModel.DuplicatePropertiesComplexType", complexTypeElement.FullName(), "Invalid complex type full name");
            Assert.AreEqual("DuplicatePropertiesComplexType", complexTypeElement.Name, "Invalid complex type name");

            Assert.IsTrue(complexTypeElement.DeclaredProperties.Count() == 2, "Invalid complex type property count");
            Assert.AreEqual(EdmPropertyKind.Structural, complexTypeElement.DeclaredProperties.ElementAt(0).PropertyKind, "Invalid property kind");
            Assert.AreEqual(EdmPropertyKind.Structural, complexTypeElement.DeclaredProperties.ElementAt(1).PropertyKind, "Invalid property kind");

            IEdmProperty complexProperty1 = (IEdmProperty)complexTypeElement.DeclaredProperties.ElementAt(0);

            Assert.AreEqual("Duplicate", complexProperty1.Name, "Invalid property name");

            IEdmProperty complexProperty2 = (IEdmProperty)complexTypeElement.DeclaredProperties.ElementAt(1);

            Assert.AreEqual("Duplicate", complexProperty2.Name, "Invalid property name");

            var expectedErrors = new EdmLibTestErrors()
            {
                { 6, 10, EdmErrorCode.AlreadyDefined }
            };

            IEnumerable <EdmError> actualErrors = null;

            model.Validate(out actualErrors);
            this.CompareErrors(actualErrors, expectedErrors);
        }
        public void ConvertComplexType_Inheritance()
        {
            var taupoModel = new EntityModelSchema()
            {
                new ComplexType("NS1", "BaseComplex")
                {
                    new MemberProperty("p1", EdmDataTypes.Int16),
                },
                new ComplexType("NS2", "DerivedComplex")
                {
                    BaseType   = "BaseComplex",
                    Properties = { new MemberProperty("p2", EdmDataTypes.Int16) },
                },
            }
            .Resolve();

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(2, result.SchemaElements.Count());
            Assert.AreEqual(2, result.SchemaElements.OfType <IEdmComplexType>().Count());

            IEdmComplexType baseComplex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(0);

            Assert.AreEqual("NS1.BaseComplex", baseComplex.FullName());
            Assert.AreEqual(1, baseComplex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p1", baseComplex.DeclaredStructuralProperties().First().Name);

            IEdmComplexType derivedComplex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(1);

            Assert.AreEqual("NS2.DerivedComplex", derivedComplex.FullName());
            Assert.AreEqual(1, derivedComplex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p2", derivedComplex.DeclaredStructuralProperties().First().Name);

            Assert.AreEqual(2, derivedComplex.StructuralProperties().Count());
            Assert.AreEqual(2, derivedComplex.Properties().Count());
            Assert.AreEqual(baseComplex, derivedComplex.BaseComplexType());
        }
        internal static object CreateResource(IEdmComplexType edmComplexType, IEdmModel edmModel)
        {
            Contract.Assert(edmComplexType != null);
            Contract.Assert(edmModel != null);

            Type clrType = EdmLibHelpers.GetClrType(new EdmComplexTypeReference(edmComplexType, isNullable: true), edmModel);
            if (clrType == null)
            {
                throw Error.InvalidOperation(SRResources.MappingDoesNotContainEntityType, edmComplexType.FullName());
            }

            return Activator.CreateInstance(clrType);
        }
예제 #11
0
        internal static object CreateResource(IEdmComplexType edmComplexType, IEdmModel edmModel)
        {
            Type clrType = EdmLibHelpers.GetClrType(new EdmComplexTypeReference(edmComplexType, isNullable: true), edmModel);

            if (clrType == null)
            {
                throw Error.Argument("edmComplexType", SRResources.MappingDoesNotContainEntityType, edmComplexType.FullName());
            }

            return(Activator.CreateInstance(clrType));
        }
        public void ConvertComplexType()
        {
            var taupoModel = new EntityModelSchema()
            {
                new ComplexType("NS1", "Complex1")
                {
                    new MemberProperty("p1", EdmDataTypes.Int32),
                    new MemberProperty("p2", EdmDataTypes.Int32.Nullable())
                    {
                        DefaultValue = 100,
                        Annotations  =
                        {
                            new ConcurrencyTokenAnnotation(),
                            new AttributeAnnotation()
                            {
                                Content = new XAttribute(this.annotationNamespace + "foo1", "bar1")
                            },
                        },
                    },
                    new AttributeAnnotation()
                    {
                        Content = new XAttribute(this.annotationNamespace + "foo2", "bar2")
                    },
                },
            };

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(1, result.SchemaElements.Count());
            Assert.AreEqual(1, result.SchemaElements.OfType <IEdmComplexType>().Count());

            IEdmComplexType complex = result.SchemaElements.OfType <IEdmComplexType>().Single();

            Assert.AreEqual("NS1.Complex1", complex.FullName());
            Assert.IsNull(complex.BaseComplexType());
            Assert.AreEqual(2, complex.DeclaredStructuralProperties().Count());
            Assert.AreEqual(2, complex.StructuralProperties().Count());

            IEdmStructuralProperty property     = complex.DeclaredStructuralProperties().ElementAt(0);
            IEdmTypeReference      propertyType = property.Type;

            Assert.AreEqual("p1", property.Name);
            Assert.AreEqual("Edm.Int32", propertyType.FullName());
            Assert.AreEqual(EdmTypeKind.Primitive, propertyType.TypeKind());
            Assert.IsFalse(propertyType.IsNullable);

            Assert.AreEqual(complex, property.DeclaringType);
            Assert.AreEqual(EdmConcurrencyMode.None, property.ConcurrencyMode);
            Assert.IsNull(property.DefaultValueString);

            Assert.AreEqual(0, result.DirectValueAnnotations(property).Count());

            property     = complex.DeclaredStructuralProperties().ElementAt(1);
            propertyType = property.Type;

            Assert.AreEqual("p2", property.Name);
            Assert.AreEqual("Edm.Int32", propertyType.FullName());
            Assert.AreEqual(EdmTypeKind.Primitive, propertyType.TypeKind());
            Assert.IsTrue(propertyType.IsNullable);

            Assert.AreEqual(complex, property.DeclaringType);
            Assert.AreEqual(EdmConcurrencyMode.Fixed, property.ConcurrencyMode);
            Assert.AreEqual("100", property.DefaultValueString);

            Assert.AreEqual(1, result.DirectValueAnnotations(complex).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(complex).First().NamespaceUri);
            Assert.AreEqual("foo2", result.DirectValueAnnotations(complex).First().Name);
            Assert.AreEqual("bar2", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(complex).First()).Value as IEdmStringValue).Value);

            Assert.AreEqual(1, result.DirectValueAnnotations(property).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(property).First().NamespaceUri);
            Assert.AreEqual("foo1", result.DirectValueAnnotations(property).First().Name);
            Assert.AreEqual("bar1", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(property).First()).Value as IEdmStringValue).Value);
        }
예제 #13
0
        private void FillStockContentsForComplex(IEdmComplexType edmType, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockType = (EdmComplexType)stockModel.FindType(edmType.FullName());
            this.SetImmediateAnnotations(edmType, stockType, edmModel, stockModel);

            foreach (var edmProperty in edmType.DeclaredStructuralProperties())
            {
                ConvertToStockStructuralProperty((IEdmStructuralProperty)edmProperty, edmModel, stockModel);
            }
        }
예제 #14
0
        private EdmComplexType ConstructStockComplexTypeInModel(IEdmComplexType complexType, EdmModel stockModel, Dictionary<string, EdmComplexType> stockComplexTypes)
        {
            EdmComplexType stockType;
            string fullName = complexType.FullName();
            if (!stockComplexTypes.TryGetValue(fullName, out stockType))
            {
                stockType = new EdmComplexType(
                    complexType.Namespace,
                    complexType.Name,
                    complexType.BaseType != null ? this.ConstructStockComplexTypeInModel((IEdmComplexType)complexType.BaseType, stockModel, stockComplexTypes) : null,
                    complexType.IsAbstract);

                // TODO: IsBad, Documentation
                stockModel.AddElement(stockType);
                stockComplexTypes.Add(fullName, stockType);
            }

            return stockType;
        }
예제 #15
0
        internal static object CreateResource(IEdmComplexType edmComplexType, IEdmModel edmModel)
        {
            Contract.Assert(edmComplexType != null);
            Contract.Assert(edmModel != null);

            Type clrType = EdmLibHelpers.GetClrType(new EdmComplexTypeReference(edmComplexType, isNullable: true), edmModel);

            if (clrType == null)
            {
                throw Error.InvalidOperation(SRResources.MappingDoesNotContainEntityType, edmComplexType.FullName());
            }

            return(Activator.CreateInstance(clrType));
        }