public void ParseBadComputeWithMissingAlias() { Uri url = new Uri("http://host/Paintings?$compute=nonsense as"); ODataUriParser parser = new ODataUriParser(HardCodedTestModel.TestModel, ServiceRoot, url); Action action = () => parser.ParseCompute(); action.ShouldThrow <ArgumentNullException>().WithMessage("Value cannot be null or empty.\r\nParameter name: alias"); }
public void ParseBadComputeWithMissingAs() { Uri url = new Uri("http://host/Paintings?$compute=nonsense"); ODataUriParser parser = new ODataUriParser(HardCodedTestModel.TestModel, ServiceRoot, url); Action action = () => parser.ParseCompute(); action.ShouldThrow <ODataException>().WithMessage(ODataErrorStrings.UriQueryExpressionParser_AsExpected(8, "nonsense")); }
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); }
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); }