Exemple #1
0
        public void ParseGroupByStringsWithAggregate(string query)
        {
            var model =
                Common.TestModelBuilder.CreateModel(new Type[] { typeof(Category), typeof(Product), typeof(Sales) });
            IEdmType edmType = model.FindDeclaredType(typeof(Sales).FullName);
            var      config  = new ODataUriParserConfiguration(model);
            var      result  = default(ApplyClause);

            "Given a message to parse {0}".Given(() => { });
            "When I try to parse".When(
                () =>
            {
                result = ApplyParser.ParseApplyImplementation(query, config, edmType, null);
            });
            "the transformation is groupby".Then(() => result.Transformations.First().Item2.Should().BeOfType <ApplyGroupbyClause>());
            "the transformation contains aggregate".Then(() => ((ApplyGroupbyClause)result.Transformations.First().Item2).Aggregate.Should().NotBeNull());
            "the transformation contains aggregate".Then(() => ((ApplyGroupbyClause)result.Transformations.First().Item2).Aggregate.Should().BeOfType <ApplyAggregateClause>());
            "The aggregation method is Sum".Then(
                () =>
                ((ApplyGroupbyClause)result.Transformations.First().Item2).Aggregate.AggregationMethod.Should()
                .Be("sum"));
            "The aggregation property is Amount".Then(
                () =>
                ((ApplyGroupbyClause)result.Transformations.First().Item2).Aggregate.AggregatableProperty.Should()
                .Be("Amount"));
            "The Alias is Total".Then(
                () =>
                ((ApplyGroupbyClause)result.Transformations.First().Item2).Aggregate.Alias.Should()
                .Be("Total"));
        }
        public override int VisitApply <TLeft, TOutput>(ApplyParser <TInput, TLeft, TOutput> parser, int start)
        {
            var leftLen = Search(parser.LeftParser, start);

            if (leftLen < 0)
            {
                return(leftLen);
            }

            while (true)
            {
                var rightLen = Search(parser.RightParser, start + leftLen);
                if (rightLen < 0)
                {
                    if (parser.ApplyKind == ApplyKind.One)
                    {
                        // failed to be applied once
                        return(-leftLen + rightLen);
                    }
                    else
                    {
                        return(leftLen);
                    }
                }
                else
                {
                    leftLen += rightLen;

                    if (parser.ApplyKind != ApplyKind.ZeroOrMore)
                    {
                        return(leftLen);
                    }
                }
            }
        }
Exemple #3
0
        public void ParsingStringThatDoesNotMatchModelThrowsODataException(string query)
        {
            var      model     = Common.TestModelBuilder.CreateModel(new Type[] { typeof(Category), typeof(Product), typeof(Sales) });
            IEdmType edmType   = model.FindDeclaredType(typeof(Sales).FullName);
            var      config    = new ODataUriParserConfiguration(model);
            var      exception = default(Exception);

            "Given a message to parse {0}".Given(() => { });
            "When I try to parse".When(
                () =>
            {
                exception = Record.Exception(() => ApplyParser.ParseApplyImplementation(query, config, edmType, null));
            });
            "Then an OData exception is thrown".Then(() => exception.Should().BeOfType <Microsoft.OData.Core.ODataException>());
        }
Exemple #4
0
            public override Parser <TInput> VisitApply <TLeft, TOutput>(ApplyParser <TInput, TLeft, TOutput> parser)
            {
                if (state.State == 0)
                {
                    state.State           = 1;
                    state.NextOutputStart = state.OutputStart;
                    return(parser.LeftParser);
                }
                else if (state.State == 1)
                {
                    if (state.LastResult < 0)
                    {
                        if (parser.ApplyKind == ApplyKind.One)
                        {
                            state.InputLength = -state.InputLength + state.LastResult;
                        }
                        else
                        {
                            state.InputLength = state.LastResult;
                        }

                        return(null);
                    }
                    else
                    {
                        state.InputLength     = state.LastResult;
                        state.State           = 2;
                        state.NextOutputStart = state.OutputStart;
                        return(parser.RightParser);
                    }
                }
                else
                {
                    if (state.LastResult > 0)
                    {
                        state.InputLength += state.LastResult;

                        if (parser.ApplyKind == ApplyKind.ZeroOrMore)
                        {
                            return(parser.RightParser);
                        }
                    }

                    return(null);
                }
            }
            public override void VisitApply <TLeft, TOutput>(ApplyParser <TInput, TLeft, TOutput> parser)
            {
                switch (parser.ApplyKind)
                {
                case ApplyKind.ZeroOrMore:
                    WriteSequence(new Parser <TInput>[] { parser.LeftParser, Parsers <TInput> .ZeroOrMore(parser.RightParser) });
                    break;

                case ApplyKind.ZeroOrOne:
                    WriteSequence(new Parser <TInput>[] { parser.LeftParser, Parsers <TInput> .ZeroOrOne(parser.RightParser) });
                    break;

                default:
                    WriteSequence(new Parser <TInput>[] { parser.LeftParser, parser.RightParser });
                    break;
                }
            }
Exemple #6
0
        public void CastingDoesNotRequireSpace(string query)
        {
            var model =
                Common.TestModelBuilder.CreateModel(new Type[] { typeof(Category), typeof(Product), typeof(Sales) });
            IEdmType edmType = model.FindDeclaredType(typeof(Sales).FullName);
            var      config  = new ODataUriParserConfiguration(model);
            var      result  = default(ApplyClause);

            "Given a query to parse {0}".Given(() => { });
            "When I Try to parse".When(
                () =>
            {
                result = ApplyParser.ParseApplyImplementation(query, config, edmType, null);
            });
            "Then we should have one transformation".Then(() => result.Transformations.Count.Should().Be(1));
            "And the transformation is filter".And(() => result.Transformations.First().Item2.Should().BeOfType <ApplyFilterClause>());
        }
Exemple #7
0
        public void GroupByStringsWithWrongNumberOfParenthesisPass2(string query)
        {
            var model =
                Common.TestModelBuilder.CreateModel(new Type[] { typeof(Category), typeof(Product), typeof(Sales) });
            IEdmType edmType = model.FindDeclaredType(typeof(Sales).FullName);
            var      config  = new ODataUriParserConfiguration(model);
            var      result  = default(ApplyClause);

            "Given a message to parse {0}".Given(() => { });
            "When I try to parse".When(
                () =>
            {
                result = ApplyParser.ParseApplyImplementation(query, config, edmType, null);
            });
            "the transformation is groupby".Then(
                () => result.Transformations.First().Item2.Should().BeOfType <ApplyGroupbyClause>());
        }
Exemple #8
0
        public void ParseMultipleTransformations(string query)
        {
            var model =
                Common.TestModelBuilder.CreateModel(new Type[] { typeof(Category), typeof(Product), typeof(Sales) });
            IEdmType edmType = model.FindDeclaredType(typeof(Sales).FullName);
            var      config  = new ODataUriParserConfiguration(model);
            var      result  = default(ApplyClause);

            "Given a message to parse {0}".Given(() => { });
            "When I try to parse".When(
                () =>
            {
                result = ApplyParser.ParseApplyImplementation(query, config, edmType, null);
            });
            "we should find 4 transformations".Then(() => result.Transformations.Count.Should().Be(4));
            "the first transformation is aggregate".Then(() => result.Transformations.First().Item2.Should().BeOfType <ApplyAggregateClause>());
            "the last transformation is groupby".Then(() => result.Transformations.Last().Item2.Should().BeOfType <ApplyGroupbyClause>());
        }
        /// <summary>
        /// Parses a apply clause on the given full Uri, binding
        /// the text into semantic nodes using the constructed mode.
        /// </summary>
        /// <returns>A <see cref="ApplyClause"/> representing the aggregation query.</returns>
        public ApplyClause ParseApply()
        {
            if (this.applyClause != null)
            {
                return(this.applyClause);
            }

            string applyQuery;

            if (!queryOptions.TryGetValue(UriQueryConstants.ApplyQueryOption, out applyQuery) ||
                string.IsNullOrEmpty(applyQuery) ||
                this.targetEdmType == null)
            {
                return(null);
            }

            this.applyClause = ApplyParser.ParseApplyImplementation(applyQuery, this.Configuration, this.targetEdmType, this.targetNavigationSource);
            return(this.applyClause);
        }