示例#1
0
        public void FormatExpressionTest()
        {
            string[] values = new string[] { "json", "xml", "foo", "bar" };

            var testCases = values.Select(value =>
                                          new
            {
                Value = value,
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                (testCase) =>
            {
                SyntacticTree actual =
                    QueryTokenUtils.ParseQuery("Root", format: testCase.Value);

                SyntacticTree expected = new SyntacticTree(
                    null,
                    new[] { "Root" },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    testCase.Value,
                    null);

                QueryTokenUtils.VerifySyntaxTreesAreEqual(expected, actual, this.Assert);
            });
        }
示例#2
0
        public void CountExpressionTest()
        {
            object[] values = new object[] { null, 0, 1, 2, 5, 11111, -2, 12.3, "Foo", -12.4m, 'c', (long)int.MaxValue + 1, (long)int.MinValue - 1, 2.3f, "(1)", "2 + 3", "int.MaxValue", "false", "true", "False", "True", "FaLSE", "trUE" };

            string           errorMessageTemplate = "Invalid value '{0}' for $count query option found. Valid values are '{1}'.";
            HashSet <string> correctValues        = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
            {
                "true", "false"
            };
            string correctValuesStr = string.Join(", ", correctValues.ToArray());
            var    testCases        = values.Select(value =>
                                                    new
            {
                Value = value == null ? null : value.ToString(),
                ExpectedErrorMessage = value == null || correctValues.Contains(value.ToString())
                        ? null
                        : string.Format(CultureInfo.InvariantCulture, errorMessageTemplate, value, correctValuesStr).Replace('+', ' ')    // URI all + will be replace by space
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                (testCase) =>
            {
                this.Assert.ExpectedException <ODataException>(
                    () =>
                {
                    SyntacticTree actual =
                        QueryTokenUtils.ParseQuery("Root", count: testCase.Value);

                    bool?countQuery = null;

                    if (testCase.Value != null)
                    {
                        bool countValue;
                        bool.TryParse(testCase.Value, out countValue);

                        countQuery = countValue;
                    }

                    SyntacticTree expected = new SyntacticTree(
                        null,
                        new[] { "Root" },
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        countQuery,
                        null,
                        null);

                    QueryTokenUtils.VerifySyntaxTreesAreEqual(expected, actual, this.Assert);
                },
                    testCase.ExpectedErrorMessage,
                    null);
            });
        }
示例#3
0
        public void ExpandExpressionTest()
        {
            IEnumerable <ExpressionTestCase> expressionTestCases = ExpressionTestCase.PrimitiveLiteralTestCases()
                                                                   .Concat(ExpressionTestCase.BinaryOperatorTestCases())
                                                                   .Concat(ExpressionTestCase.UnaryOperatorTestCases())
                                                                   .Concat(ExpressionTestCase.PropertyAccessTestCases(ExpressionTestCase.PropertyAccessNames))
                                                                   .Concat(ExpressionTestCase.ParenthesisTestCases())
                                                                   .Concat(ExpressionTestCase.FunctionCallTestCases());

            var testCases = expressionTestCases.Select(tc => new ExpandTestCase()
            {
                Expand = tc.Expression,
                ExpectedExpandToken = new ExpandToken(new QueryToken[] { tc.ExpectedToken })
            });

            // All expressions
            testCases.Concat(new ExpandTestCase[] { new ExpandTestCase()
                                                    {
                                                        Expand = string.Join(", ", expressionTestCases.Select(tc => tc.Expression).ToArray()),
                                                        ExpectedExpandToken = new ExpandToken(expressionTestCases.Select(tc => tc.ExpectedToken))
                                                    } });

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                (testCase) =>
            {
                SyntacticTree actual   = QueryTokenUtils.ParseQuery("Root", expand: testCase.Expand);
                SyntacticTree expected = new SyntacticTree(
                    new SegmentToken("Root", null, null),
                    null,
                    null,
                    null,
                    testCase.ExpectedExpandToken,
                    null,
                    null,
                    null,
                    null,
                    null);

                QueryTokenUtils.VerifySyntaxTreesAreEqual(
                    expected,
                    actual,
                    this.Assert);
            });
        }
示例#4
0
        public void SelectExpressionTest()
        {
            IEnumerable <SelectExpressionTestCase> expressionTestCases = SelectExpressionTestCase.SelectPropertyAccessTestCases(ExpressionTestCase.PropertyAccessNames);

            var testCases = expressionTestCases.Select(tc => new SelectTestCase()
            {
                Select = tc.Expression,
                ExpectedSelectToken = new SelectToken(new PathSegmentToken[] { tc.ExpectedToken })
            });

            // All expressions
            testCases.Concat(new SelectTestCase[] { new SelectTestCase()
                                                    {
                                                        Select = string.Join(", ", expressionTestCases.Select(tc => tc.Expression).ToArray()),
                                                        ExpectedSelectToken = new SelectToken(expressionTestCases.Select(tc => tc.ExpectedToken))
                                                    } });

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                (testCase) =>
            {
                SyntacticTree actual   = QueryTokenUtils.ParseQuery("Root", select: testCase.Select);
                SyntacticTree expected = new SyntacticTree(
                    new StartPathToken("Root", null, null),
                    null,
                    null,
                    testCase.ExpectedSelectToken,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null);

                QueryTokenUtils.VerifySyntaxTreesAreEqual(
                    expected,
                    actual,
                    this.Assert);
            });
        }
示例#5
0
        public void OrderByExpressionTest()
        {
            IEnumerable <ExpressionTestCase> expressionTestCases = ExpressionTestCase.PrimitiveLiteralTestCases()
                                                                   .Concat(ExpressionTestCase.BinaryOperatorTestCases())
                                                                   .Concat(ExpressionTestCase.UnaryOperatorTestCases())
                                                                   .Concat(ExpressionTestCase.PropertyAccessTestCases(ExpressionTestCase.PropertyAccessNames))
                                                                   .Concat(ExpressionTestCase.ParenthesisTestCases())
                                                                   .Concat(ExpressionTestCase.FunctionCallTestCases());

            // Use filter expressions first without anything
            var filterWithNoDirection = expressionTestCases.Select(tc => new OrderByTestCase()
            {
                OrderBy = tc.Expression,
                ExpectedOrderByTokens = new OrderByToken[] { new OrderByToken(tc.ExpectedToken, OrderByDirection.Ascending) }
            });

            // Use filter expressions with asc
            var filterWithAscending = expressionTestCases.Select(tc => new OrderByTestCase()
            {
                OrderBy = tc.Expression + " asc",
                ExpectedOrderByTokens = new OrderByToken[] { new OrderByToken(tc.ExpectedToken, OrderByDirection.Ascending) }
            });

            // Use filter expressions with desc
            var filterWithDescending = expressionTestCases.Select(tc => new OrderByTestCase()
            {
                OrderBy = tc.Expression + " desc",
                ExpectedOrderByTokens = new OrderByToken[] { new OrderByToken(tc.ExpectedToken, OrderByDirection.Descending) }
            });

            // And now some order by specific cases with multiple orderbys
            var orderByTestCases = ExpressionTestCase.VariousExpressions().ToList().Variations(3).Select(tc =>
                                                                                                         new OrderByTestCase
            {
                OrderBy = string.Join(",", tc.Select((t, index) => t.Expression + ((index % 3 == 0) ? "" : ((index % 3 == 1) ? " asc" : " desc"))).ToArray()),
                ExpectedOrderByTokens = tc.Select((t, index) => new OrderByToken(
                                                      t.ExpectedToken,
                                                      (index % 3 == 2) ? OrderByDirection.Descending : OrderByDirection.Ascending)).ToArray()
            });

            CombinatorialEngineProvider.RunCombinations(
                filterWithNoDirection
                .Concat(filterWithAscending)
                .Concat(filterWithDescending)
                .Concat(orderByTestCases),
                (testCase) =>
            {
                SyntacticTree actual   = QueryTokenUtils.ParseQuery("Root", orderby: testCase.OrderBy);
                SyntacticTree expected = new SyntacticTree(
                    null,
                    new[] { "Root" },
                    null,
                    testCase.ExpectedOrderByTokens,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null);

                QueryTokenUtils.VerifySyntaxTreesAreEqual(
                    expected,
                    actual,
                    this.Assert);
            });
        }
示例#6
0
        void RunTest(bool includeSpaceAroundSymbols)
        {
            var testCases = new[]
            {
                new
                {
                    QueryOptions         = (List <KeyValuePair <string, string> >)null,
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$filter", "3 eq 2")
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("CustomValue", "null")
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("null", "CustomValue")
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$custom", "2")
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$a", "1"),
                        new KeyValuePair <string, string>("$b", "2"),
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("a", "1"),
                        new KeyValuePair <string, string>("$filter", "3 eq 2"),
                        new KeyValuePair <string, string>("b", "2"),
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$a", "1"),
                        new KeyValuePair <string, string>("$filter", "3 eq 2"),
                        new KeyValuePair <string, string>("$b", "2"),
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("a", "1"),
                        new KeyValuePair <string, string>("b", "2"),
                        new KeyValuePair <string, string>("a", "2"),
                        new KeyValuePair <string, string>("b", "4"),
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$filter", "3 eq 2"),
                        new KeyValuePair <string, string>("$filter", "2 eq 3"),
                        new KeyValuePair <string, string>("$b", "2"),
                    },
                    ExpectedErrorMessage = "Query option '$filter' was specified more than once, but it must be specified at most once.",
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$select", "Name")
                    },
                    ExpectedErrorMessage = (string)null,
                },
                new
                {
                    QueryOptions = new List <KeyValuePair <string, string> >()
                    {
                        new KeyValuePair <string, string>("$expand", "Products")
                    },
                    ExpectedErrorMessage = (string)null,
                },
            };

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                (testCase) =>
            {
                this.Assert.ExpectedException <ODataException>(
                    () =>
                {
                    SyntacticTree actual = QueryTokenUtils.ParseQuery("Root", testCase.QueryOptions, includeSpaceAroundSymbols);
                    List <CustomQueryOptionToken> options = QueryTokenUtils.NormalizeAndRemoveBuiltInQueryOptions(testCase.QueryOptions);
                    options = options != null && options.Count == 0 ? null : options;

                    SyntacticTree expected = new SyntacticTree(
                        null,
                        new[] { "Feed" },
                        actual.Filter,
                        actual.OrderByTokens,
                        null,
                        null,
                        actual.Skip,
                        null,
                        null,
                        null,
                        options);

                    QueryTokenUtils.VerifySyntaxTreesAreEqual(expected, actual, this.Assert);
                },
                    testCase.ExpectedErrorMessage,
                    null);
            });
        }