public void EdmModelAddEntityContainerTest() { var model = new EdmModel(); Assert.Equal( model.AddEntityContainer("NS", "Container"), model.FindEntityContainer("NS.Container")); }
/// <summary> /// Creates an EdmModel based on the source table /// </summary> /// <param name="sourceTable"></param> /// <returns></returns> public static Tuple <IEdmModel, IEdmType> BuildEdmModel(this DataTable sourceTable, String entityName = null, String entitySetName = null) { String Namespace = "Dynamic"; String TypeName = sourceTable.TableName; if (!String.IsNullOrWhiteSpace(entityName)) { TypeName = entityName; } EdmModel Output = new EdmModel(); EdmEntityType DataSourceModel = new EdmEntityType(Namespace, TypeName); List <IEdmStructuralProperty> KeyProperties = new List <IEdmStructuralProperty>(); foreach (DataColumn SourceColumn in sourceTable.Columns) { String ColumnName = SourceColumn.ColumnName; Type ColumnType = SourceColumn.DataType; EdmPrimitiveTypeKind?MappedType = ConvertType(ColumnType); if (!MappedType.HasValue) { continue; } IEdmStructuralProperty NewColumn = DataSourceModel.AddStructuralProperty(ColumnName, MappedType.Value); if (sourceTable.PrimaryKey != null && sourceTable.PrimaryKey.Any(x => String.Equals(x.ColumnName, ColumnName, StringComparison.InvariantCultureIgnoreCase))) { KeyProperties.Add(NewColumn); } } if (KeyProperties.Count > 0) { DataSourceModel.AddKeys(KeyProperties); } Output.AddElement(DataSourceModel); if (!String.IsNullOrWhiteSpace(entitySetName)) { EdmEntityContainer OutputContainer = Output.AddEntityContainer("Dynamic", "Test"); OutputContainer.AddEntitySet(entitySetName, DataSourceModel); } return(new Tuple <IEdmModel, IEdmType>(Output, DataSourceModel)); }
public void ShouldWriteOptionalParameters() { string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + "<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" + "<edmx:DataServices>" + "<Schema Namespace=\"test\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" + "<Function Name=\"TestFunction\">" + "<Parameter Name=\"requiredParam\" Type=\"Edm.String\" Nullable=\"false\" />" + "<Parameter Name=\"optionalParam\" Type=\"Edm.String\" Nullable=\"false\">" + "<Annotation Term=\"Org.OData.Core.V1.OptionalParameter\" />" + "</Parameter>" + "<Parameter Name=\"optionalParamWithDefault\" Type=\"Edm.String\" Nullable=\"false\">" + "<Annotation Term=\"Org.OData.Core.V1.OptionalParameter\">" + "<Record>" + "<PropertyValue Property=\"DefaultValue\" String=\"Smith\" />" + "</Record>" + "</Annotation>" + "</Parameter>" + "<ReturnType Type=\"Edm.String\" Nullable=\"false\" />" + "</Function>" + "<EntityContainer Name=\"Default\">" + "<FunctionImport Name=\"TestFunction\" Function=\"test.TestFunction\" />" + "</EntityContainer>" + "</Schema>" + "</edmx:DataServices>" + "</edmx:Edmx>"; var stringTypeReference = new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false); var model = new EdmModel(); var function = new EdmFunction("test", "TestFunction", stringTypeReference); var requiredParam = new EdmOperationParameter(function, "requiredParam", stringTypeReference); var optionalParam = new EdmOptionalParameter(function, "optionalParam", stringTypeReference, null); var optionalParamWithDefault = new EdmOptionalParameter(function, "optionalParamWithDefault", stringTypeReference, "Smith"); function.AddParameter(requiredParam); function.AddParameter(optionalParam); function.AddParameter(optionalParamWithDefault); model.AddElement(function); model.AddEntityContainer("test", "Default").AddFunctionImport("TestFunction", function); string csdlStr = GetCsdl(model, CsdlTarget.OData); Assert.Equal(expected, csdlStr); }
private IEdmModel CreateModel() { EdmModel model = new EdmModel(); string ns = "NS"; EdmEntityType typeA = model.AddEntityType(ns, "TypeA"); var idProperty = typeA.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String); typeA.AddKeys(idProperty); var container = model.AddEntityContainer(ns, "Container"); container.AddEntitySet("TypeAs", typeA); var term = model.AddTerm(ns, "MyTerm", EdmPrimitiveTypeKind.Boolean); var annotation = new EdmVocabularyAnnotation(typeA, term, new EdmBooleanConstant(true)); model.AddVocabularyAnnotation(annotation); return(model); }
public void ParseComputeAsExpandQueryOption() { // Create model EdmModel model = new EdmModel(); EdmEntityType elementType = model.AddEntityType("DevHarness", "Entity"); EdmEntityType targetType = model.AddEntityType("DevHarness", "Navigation"); EdmTypeReference typeReference = new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false); targetType.AddProperty(new EdmStructuralProperty(targetType, "Prop1", typeReference)); EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo(); propertyInfo.Name = "Nav1"; propertyInfo.Target = targetType; propertyInfo.TargetMultiplicity = EdmMultiplicity.One; EdmProperty navigation = EdmNavigationProperty.CreateNavigationProperty(elementType, propertyInfo); elementType.AddProperty(navigation); EdmEntityContainer container = model.AddEntityContainer("Default", "Container"); container.AddEntitySet("Entities", elementType); // Define queries and new up parser. Uri root = new Uri("http://host"); Uri url = new Uri("http://host/Entities?$expand=Nav1($compute=cast(Prop1, 'Edm.String') as NavProperty1AsString)"); ODataUriParser parser = new ODataUriParser(model, root, url); // parse and validate SelectExpandClause clause = parser.ParseSelectAndExpand(); List <SelectItem> items = clause.SelectedItems.ToList(); items.Count.Should().Be(1); ExpandedNavigationSelectItem expanded = items[0] as ExpandedNavigationSelectItem; List <ComputeExpression> computes = expanded.ComputeOption.ComputedItems.ToList(); computes.Count.Should().Be(1); computes[0].Alias.ShouldBeEquivalentTo("NavProperty1AsString"); computes[0].Expression.ShouldBeSingleValueFunctionCallQueryNode(); computes[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference); }
public void ParseComputeAsQueryOption() { // Create model EdmModel model = new EdmModel(); EdmEntityType elementType = model.AddEntityType("DevHarness", "Entity"); EdmTypeReference typeReference = new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false); elementType.AddProperty(new EdmStructuralProperty(elementType, "Prop1", typeReference)); EdmEntityContainer container = model.AddEntityContainer("Default", "Container"); container.AddEntitySet("Entities", elementType); // Define queries and new up parser. Uri root = new Uri("http://host"); Uri url = new Uri("http://host/Entities?$compute=cast(Prop1, 'Edm.String') as Property1AsString, tolower(Prop1) as Property1Lower"); ODataUriParser parser = new ODataUriParser(model, root, url); // parse and validate ComputeClause clause = parser.ParseCompute(); List <ComputeExpression> items = clause.ComputedItems.ToList(); items.Count().Should().Be(2); items[0].Alias.ShouldBeEquivalentTo("Property1AsString"); items[0].Expression.ShouldBeSingleValueFunctionCallQueryNode(); items[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference); items[1].Alias.ShouldBeEquivalentTo("Property1Lower"); items[1].Expression.ShouldBeSingleValueFunctionCallQueryNode(); items[1].Expression.TypeReference.FullName().ShouldBeEquivalentTo("Edm.String"); items[1].Expression.TypeReference.IsNullable.ShouldBeEquivalentTo(true); // tolower is built in function that allows nulls. ComputeExpression copy = new ComputeExpression(items[0].Expression, items[0].Alias, null); copy.Expression.Should().NotBeNull(); copy.TypeReference.Should().BeNull(); ComputeClause varied = new ComputeClause(null); }
private IEdmModel GetModel() { if (this.model == null) { var model = new EdmModel(); var customerType = new EdmEntityType("test", "customer", null, false, true); customerType.AddKeys(customerType.AddStructuralProperty("id", EdmPrimitiveTypeKind.String, false)); customerType.AddStructuralProperty("name", EdmPrimitiveTypeKind.String); customerType.AddStructuralProperty("age", EdmPrimitiveTypeKind.Int32, false); customerType.AddStructuralProperty("stream", EdmPrimitiveTypeKind.Stream); customerType.AddStructuralProperty("binaryAsStream", EdmPrimitiveTypeKind.Binary); customerType.AddStructuralProperty("textAsStream", EdmPrimitiveTypeKind.String); customerType.AddStructuralProperty("comments", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true)))); model.AddElement(customerType); var container = model.AddEntityContainer("test", "container"); this.customersEntitySet = container.AddEntitySet("customers", customerType); this.model = model; } return(this.model); }
public ODataJsonLightWriterComplexIntegrationTests() { // Initialize open EntityType: EntityType. EdmModel edmModel = new EdmModel(); EdmEntityType edmEntityType = new EdmEntityType("TestNamespace", "EntityType", baseType: null, isAbstract: false, isOpen: true); edmEntityType.AddStructuralProperty("DeclaredProperty", EdmPrimitiveTypeKind.Guid); edmEntityType.AddStructuralProperty("DeclaredGeometryProperty", EdmPrimitiveTypeKind.Geometry); edmEntityType.AddStructuralProperty("DeclaredSingleProperty", EdmPrimitiveTypeKind.Single); edmEntityType.AddStructuralProperty("DeclaredDoubleProperty", EdmPrimitiveTypeKind.Double); edmEntityType.AddStructuralProperty("TimeOfDayProperty", EdmPrimitiveTypeKind.TimeOfDay); edmEntityType.AddStructuralProperty("DateProperty", EdmPrimitiveTypeKind.Date); edmModel.AddElement(edmEntityType); this.model = TestUtils.WrapReferencedModelsToMainModel(edmModel); this.entityType = edmEntityType; // Initialize derived ComplexType: Address and HomeAddress this.addressType = new EdmComplexType("TestNamespace", "Address", baseType: null, isAbstract: false, isOpen: false); this.addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); this.derivedAddressType = new EdmComplexType("TestNamespace", "HomeAddress", baseType: this.addressType, isAbstract: false, isOpen: false); this.derivedAddressType.AddStructuralProperty("FamilyName", EdmPrimitiveTypeKind.String); // Initialize open ComplexType: OpenAddress. this.openAddressType = new EdmComplexType("TestNamespace", "OpenAddress", baseType: null, isAbstract: false, isOpen: true); this.openAddressType.AddStructuralProperty("CountryRegion", EdmPrimitiveTypeKind.String); edmModel.AddElement(this.addressType); edmModel.AddElement(this.derivedAddressType); edmModel.AddElement(this.openAddressType); edmModel.AddEntityContainer("TestNamespace", "Container").AddEntitySet("entitySet", this.entityType); this.address = new ODataResource() { TypeName = "TestNamespace.Address", Properties = new ODataProperty[] { new ODataProperty { Name = "City", Value = "Shanghai" } } }; this.homeAddress = new ODataResource { TypeName = "TestNamespace.HomeAddress", Properties = new ODataProperty[] { new ODataProperty { Name = "FamilyName", Value = "Green" }, new ODataProperty { Name = "City", Value = "Shanghai" } } }; this.addressWithInstanceAnnotation = new ODataResource() { TypeName = "TestNamespace.Address", Properties = new ODataProperty[] { new ODataProperty { Name = "City", Value = "Shanghai" } }, InstanceAnnotations = new Collection <ODataInstanceAnnotation> { new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(true)) } }; this.homeAddressWithInstanceAnnotations = new ODataResource() { TypeName = "TestNamespace.HomeAddress", Properties = new ODataProperty[] { new ODataProperty { Name = "FamilyName", Value = "Green", InstanceAnnotations = new Collection <ODataInstanceAnnotation> { new ODataInstanceAnnotation("FamilyName.annotation", new ODataPrimitiveValue(true)) } }, new ODataProperty { Name = "City", Value = "Shanghai", InstanceAnnotations = new Collection <ODataInstanceAnnotation> { new ODataInstanceAnnotation("City.annotation1", new ODataPrimitiveValue(true)), new ODataInstanceAnnotation("City.annotation2", new ODataPrimitiveValue(123)) } } }, InstanceAnnotations = new Collection <ODataInstanceAnnotation> { new ODataInstanceAnnotation("Is.AutoComputable", new ODataPrimitiveValue(true)), new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(false)) } }; }
public void EdmSingletonAdvancedContainedNavigationPropertyBindingTest() { // Creates model with: // -Singleton "s": // contained navigation properties "a1" and "a2" of type aType, // contained navigation proeprty b of type bType // non-contained navigation property c of type cType // -Type aType: // contained navigation property to "b" of bType // -Type bType: // non-contained navigation property to "c" of cType // -Type dType derives from bType: // contained navigation property to "a" of aType // Each c navigation property is bound to a different entity set // Validates that FindNavigationTarget identifies the correct entity set for each path EdmModel model = new EdmModel(); var sType = model.AddEntityType("TestNS", "sType"); var aType = model.AddEntityType("TestNS", "aType"); aType.AddKeys(aType.AddStructuralProperty("key", EdmPrimitiveTypeKind.Int32)); var bType = model.AddEntityType("TestNS", "bType"); bType.AddKeys(bType.AddStructuralProperty("key", EdmPrimitiveTypeKind.Int32)); var cType = model.AddEntityType("TestNS", "cType"); cType.AddKeys(cType.AddStructuralProperty("key", EdmPrimitiveTypeKind.Int32)); var dType = model.AddEntityType("TestNS", "dType", bType); dType.AddKeys(dType.AddStructuralProperty("key", EdmPrimitiveTypeKind.Int32)); var a1 = new EdmNavigationPropertyInfo() { Name = "a1", Target = aType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true }; var a2 = new EdmNavigationPropertyInfo() { Name = "a2", Target = aType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true }; var b = new EdmNavigationPropertyInfo() { Name = "b", Target = bType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = true }; var c = new EdmNavigationPropertyInfo() { Name = "c", Target = cType, TargetMultiplicity = EdmMultiplicity.Many, ContainsTarget = false }; var stoa1NavProp = sType.AddUnidirectionalNavigation(a1); var stoa2NavProp = sType.AddUnidirectionalNavigation(a2); var stobNavProp = sType.AddUnidirectionalNavigation(b); var stocNavProp = sType.AddUnidirectionalNavigation(c); var atobNavProp = aType.AddUnidirectionalNavigation(b); var btocNavProp = bType.AddUnidirectionalNavigation(c); var dtoa1NavProp = dType.AddUnidirectionalNavigation(a1); var container = model.AddEntityContainer("TestNS", "container"); var s = container.AddSingleton("s", sType); var sc = container.AddEntitySet("sc", cType); var sbc = container.AddEntitySet("sbc", cType); var sa1bc = container.AddEntitySet("sa1bc", cType); var sa2bc = container.AddEntitySet("sa2bc", cType); var sa2bda1bc = container.AddEntitySet("sa2bda1bc", cType); s.AddNavigationTarget(stocNavProp, sc, new EdmPathExpression("c")); s.AddNavigationTarget(btocNavProp, sbc, new EdmPathExpression("b/c")); s.AddNavigationTarget(btocNavProp, sa1bc, new EdmPathExpression("a1/b/c")); s.AddNavigationTarget(btocNavProp, sa2bc, new EdmPathExpression("a2/b/c")); s.AddNavigationTarget(btocNavProp, sa2bda1bc, new EdmPathExpression("a2/b/TestNS.dType/a1/b/c")); var foundSc = s.FindNavigationTarget(stocNavProp); Assert.Same(sc, foundSc); var foundSb = s.FindNavigationTarget(stobNavProp); Assert.True(foundSb is IEdmContainedEntitySet); var foundSbc = foundSb.FindNavigationTarget(btocNavProp); Assert.Same(sbc, foundSbc); var foundSa1 = s.FindNavigationTarget(stoa1NavProp); Assert.True(foundSa1 is IEdmContainedEntitySet); var foundSa1b = foundSa1.FindNavigationTarget(atobNavProp); Assert.True(foundSa1b is IEdmContainedEntitySet); var foundSa1bc = foundSa1b.FindNavigationTarget(btocNavProp); Assert.Same(sa1bc, foundSa1bc); var foundSa2 = s.FindNavigationTarget(stoa2NavProp); Assert.True(foundSa2 is IEdmContainedEntitySet); var foundSa2b = foundSa2.FindNavigationTarget(atobNavProp); Assert.True(foundSa2b is IEdmContainedEntitySet); var foundSa2bc = foundSa2b.FindNavigationTarget(btocNavProp); Assert.Same(sa2bc, foundSa2bc); var foundSa2bda1 = foundSa2b.FindNavigationTarget(dtoa1NavProp, new EdmPathExpression("TestNS.dType/a1")); Assert.True(foundSa2bda1 is IEdmContainedEntitySet); var foundSa2bda1b = foundSa2bda1.FindNavigationTarget(atobNavProp); Assert.True(foundSa2bda1b is IEdmContainedEntitySet); var foundSa2bda1bc = foundSa2bda1b.FindNavigationTarget(btocNavProp); Assert.Same(sa2bda1bc, foundSa2bda1bc); }
public void ParseComputeAsLevel2ExpandQueryOption() { // Create model EdmModel model = new EdmModel(); EdmEntityType elementType = model.AddEntityType("DevHarness", "Entity"); EdmEntityType targetType = model.AddEntityType("DevHarness", "Navigation"); EdmEntityType subTargetType = model.AddEntityType("DevHarness", "SubNavigation"); EdmTypeReference typeReference = new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false); elementType.AddProperty(new EdmStructuralProperty(elementType, "Prop1", typeReference)); targetType.AddProperty(new EdmStructuralProperty(targetType, "Prop1", typeReference)); subTargetType.AddProperty(new EdmStructuralProperty(subTargetType, "Prop1", typeReference)); EdmNavigationPropertyInfo propertyInfo = new EdmNavigationPropertyInfo(); propertyInfo.Name = "Nav1"; propertyInfo.Target = targetType; propertyInfo.TargetMultiplicity = EdmMultiplicity.One; EdmProperty navigation = EdmNavigationProperty.CreateNavigationProperty(elementType, propertyInfo); elementType.AddProperty(navigation); EdmNavigationPropertyInfo subPropertyInfo = new EdmNavigationPropertyInfo(); subPropertyInfo.Name = "SubNav1"; subPropertyInfo.Target = subTargetType; subPropertyInfo.TargetMultiplicity = EdmMultiplicity.One; EdmProperty subnavigation = EdmNavigationProperty.CreateNavigationProperty(targetType, subPropertyInfo); targetType.AddProperty(subnavigation); EdmEntityContainer container = model.AddEntityContainer("Default", "Container"); container.AddEntitySet("Entities", elementType); // Define queries and new up parser. string address = "http://host/Entities?$compute=cast(Prop1, 'Edm.String') as Property1AsString, tolower(Prop1) as Property1Lower&" + "$expand=Nav1($compute=cast(Prop1, 'Edm.String') as NavProperty1AsString;" + "$expand=SubNav1($compute=cast(Prop1, 'Edm.String') as SubNavProperty1AsString))"; Uri root = new Uri("http://host"); Uri url = new Uri(address); ODataUriParser parser = new ODataUriParser(model, root, url); // parse ComputeClause computeClause = parser.ParseCompute(); SelectExpandClause selectClause = parser.ParseSelectAndExpand(); // validate top compute List <ComputeExpression> items = computeClause.ComputedItems.ToList(); items.Count().Should().Be(2); items[0].Alias.ShouldBeEquivalentTo("Property1AsString"); items[0].Expression.ShouldBeSingleValueFunctionCallQueryNode(); items[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference); items[1].Alias.ShouldBeEquivalentTo("Property1Lower"); items[1].Expression.ShouldBeSingleValueFunctionCallQueryNode(); items[1].Expression.TypeReference.FullName().ShouldBeEquivalentTo("Edm.String"); items[1].Expression.TypeReference.IsNullable.ShouldBeEquivalentTo(true); // tolower is built in function that allows nulls. // validate level 1 expand compute List <SelectItem> selectItems = selectClause.SelectedItems.ToList(); selectItems.Count.Should().Be(1); ExpandedNavigationSelectItem expanded = selectItems[0] as ExpandedNavigationSelectItem; List <ComputeExpression> computes = expanded.ComputeOption.ComputedItems.ToList(); computes.Count.Should().Be(1); computes[0].Alias.ShouldBeEquivalentTo("NavProperty1AsString"); computes[0].Expression.ShouldBeSingleValueFunctionCallQueryNode(); computes[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference); // validate level 2 expand compute List <SelectItem> subSelectItems = expanded.SelectAndExpand.SelectedItems.ToList(); subSelectItems.Count.Should().Be(1); ExpandedNavigationSelectItem subExpanded = subSelectItems[0] as ExpandedNavigationSelectItem; List <ComputeExpression> subComputes = subExpanded.ComputeOption.ComputedItems.ToList(); subComputes.Count.Should().Be(1); subComputes[0].Alias.ShouldBeEquivalentTo("SubNavProperty1AsString"); subComputes[0].Expression.ShouldBeSingleValueFunctionCallQueryNode(); subComputes[0].Expression.TypeReference.ShouldBeEquivalentTo(typeReference); }