Beispiel #1
0
        private void VerifyQuery <TClient, TClientResult, TResult>(Func <IQueryable <TClient>, IQueryable <TClientResult> > clientQueryTransform, Func <IQueryable <A>, IQueryable <TResult> > expectedTransform, bool requireNonEmpty = true)
        {
            var comparer = GetComparer <TResult>();

            var translated = new LinqToODataTranslator().Translate(clientQueryTransform(Empty <TClient> .Array.AsQueryable()).Expression, out var rootQuery, out var resultTranslator);

            Assert.That(translated, Is.InstanceOf <ODataQueryExpression>());
            Assert.That(rootQuery, Is.InstanceOf <IQueryable <TClient> >());

            var random      = new Random(123456);
            var wasNonEmpty = false;

            for (var i = 0; i < 10; ++i)
            {
                var randomQuery = this.RandomQueryable(random);
                var transformed = expectedTransform(randomQuery);
                var expected    = transformed.ToArray();
                wasNonEmpty |= expected.Any();
                Console.WriteLine("original = " + transformed);
                Console.WriteLine("odata = " + HttpUtility.UrlDecode(translated.ToString()));
                var rawApplied = ODataQueryFilter.Apply(randomQuery, ODataQueryParser.Parse(randomQuery.ElementType, translated.ToString()), out var inlineCountQuery);
                var applied    = (IEnumerable <TResult>)resultTranslator(rawApplied, null);
                Console.WriteLine("applied = " + rawApplied);
                applied.ToArray().CollectionShouldEqual(expected, comparer: comparer, orderMatters: true);
                Console.WriteLine(new string('-', 80));
            }
            if (requireNonEmpty)
            {
                wasNonEmpty.ShouldEqual(true, "At least 1 run should produce non-empty results!");
            }
        }
        private void TestFilterQuery(string filter, string orderBy, string skip, string top, int[] expectedIds)
        {
            var query = ODataQueryParser.Parse(typeof(A), new NameValueCollection {
                { "$filter", filter }, { "$orderby", orderBy }, { "$skip", skip }, { "$top", top }
            });
            var results = ODataQueryFilter.Apply(this._records, query, out var inlineCountQuery);

            results.Select(a => a.Id).CollectionShouldEqual(expectedIds, orderMatters: true);

            var equivalentToInlineCountResults = ODataQueryFilter.Apply(this._records, query.Update(top: null, skip: 0), out _);

            inlineCountQuery.Select(a => a.Id).CollectionShouldEqual(equivalentToInlineCountResults.Select(a => a.Id));
        }