private void RunOrderByTestAndVerify(IEnumerable <Product> products, string orderBy, int[] expectedOrderedId, params string[] expectedExprs)
        {
            // Act - Bind string to Linq.Expression
            OrderByBinderResult binderResult = BindOrderBy <Product>(orderBy, _model);

            // Assert
            Assert.NotNull(binderResult);

            bool                         alreadyOrdered = false;
            IList <string>               exprs          = new List <string>();
            OrderByBinderResult          result         = binderResult;
            IEnumerable <Product>        collections    = products;
            IOrderedEnumerable <Product> orderedProducts;

            do
            {
                exprs.Add(ExpressionStringBuilder.ToString(result.OrderByExpression));

                // Act - Invoke Orderby
                orderedProducts = InvokeOrderBy(collections, result.OrderByExpression, result.Direction, alreadyOrdered);

                alreadyOrdered = true;

                result = result.ThenBy;

                collections = orderedProducts;
            }while (result != null);

            // Assert
            Assert.True(expectedExprs.SequenceEqual(exprs));
            Assert.True(expectedOrderedId.SequenceEqual(orderedProducts.Select(a => a.ProductID))); // ordered id
        }
Ejemplo n.º 2
0
        public void OrderByBinder_BindsComputedPropertyInDollarOrderBy_FromDollarCompute()
        {
            // Arrange
            QueryClause clause = CreateQueryClause("$orderby=Total&$compute=Price mul Qty as Total", _model, typeof(ComputeCustomer));

            ODataQuerySettings querySettings = new ODataQuerySettings();
            IOrderByBinder     binder        = new OrderByBinder();
            QueryBinderContext context       = new QueryBinderContext(_model, querySettings, typeof(ComputeCustomer));

            if (clause.Compute != null)
            {
                context.AddComputedProperties(clause.Compute.ComputedItems);
            }

            // Act
            OrderByBinderResult orderByResult = binder.BindOrderBy(clause.OrderBy, context);

            // Assert
            Assert.Null(orderByResult.ThenBy);
            Assert.Equal(OrderByDirection.Ascending, orderByResult.Direction);
            string resultExpression = ExpressionStringBuilder.ToString(orderByResult.OrderByExpression);

            Assert.Equal("$it => ($it.Price * Convert($it.Qty))", resultExpression);

            IEnumerable <ComputeCustomer> customers = new[]
            {
                new ComputeCustomer {
                    Id = 1, Qty = 3, Price = 5.99
                },
                new ComputeCustomer {
                    Id = 2, Qty = 5, Price = 2.99
                },
                new ComputeCustomer {
                    Id = 3, Qty = 2, Price = 18.01
                },
                new ComputeCustomer {
                    Id = 4, Qty = 4, Price = 9.99
                },
            };

            var orderedResult = OrderByBinderTests.InvokeOrderBy(customers, orderByResult.OrderByExpression, orderByResult.Direction, false);

            Assert.True(new [] { 2, 1, 3, 4 }.SequenceEqual(orderedResult.Select(a => a.Id))); // ordered id
        }