コード例 #1
0
        public IEnumerable <T> CreateSplittedEnumerable <TInner>(MethodCallExpression call, Expression innerExpression)
        {
            IEnumerable <TInner> enumerable = Query.Provider.CreateQuery <TInner>(innerExpression);
            // We need to ask for the GetEnumerator to be able to check the query
            // This call will throw if the LINQ-to-SQL can't produce the query
            IEnumerator <TInner> enumerator = enumerable.GetEnumerator();
            // Success!
            // We transform back the enumerator to an enumerable
            IEnumerable <TInner> enumerable2 = new FakeEnumerable <TInner>(enumerator, enumerable);
            // "Quick-n-dirty". We use a fake Queryable. The "right" way would probably be
            // transform all the outer query from IQueryable.Method<T> to IEnumerable.Method<T>
            // Too much long :)
            // Note that this Queryable is executed in "local" (the AsQueryable<T> method is nearly
            // useless... The second time in my life I was able to use it for something)
            IQueryable <TInner> queryable = Queryable.AsQueryable(enumerable2);
            // We rebuild a new expression by changing the "old" inner parameter
            // of the MethodCallExpression with the queryable we just
            // built
            Expression expression2 = new SimpleExpressionReplacer {
                Call = call, Queryable = queryable
            }.Visit(Query.Expression);
            // We "execute" locally the whole query through a second
            // "outer" instance of the EnumerableQuery (this class is
            // the class that "implements" the "fake-magic" of AsQueryable)
            EnumerableQuery <T> query2 = new EnumerableQuery <T>(expression2);

            // And we return an enumerator to it
            return(query2.AsEnumerable());
        }
コード例 #2
0
        public async Task QueryableAsEnumearbleTest()
        {
            IQueryable <int> queryableEnumeable = new EnumerableQuery <int>(Enumerable.Range(-100, 200).ToArray()).Where(v => v < 5 && v > 0);
            var task = queryableEnumeable.AsEnumerable().AsAsync();

            Assert.That(task.IsCompleted, Is.True);

            int before = 1;

            foreach (var value in await task)
            {
                Assert.That(value, Is.EqualTo(before));
                before++;
            }
        }
コード例 #3
0
        public static Mock <IDocumentClient> CreateDocumentClient <T>(T[] collectionDocuments)
            where T : class, new()
        {
            var client = new Mock <IDocumentClient>(MockBehavior.Strict);
            var mockOrderedQueryable = new Mock <IOrderedQueryable <T> >(MockBehavior.Strict);
            var provider             = new Mock <IQueryProvider>(MockBehavior.Strict);
            var dataSource           = collectionDocuments.AsQueryable();

            // Dynamic query for the "select" calls that filter by field names
            provider
            .Setup(_ => _.CreateQuery <object>(It.IsAny <Expression>()))
            .Returns((Expression expression) =>
            {
                var mockDocumentQuery = new Mock <IFakeDocumentQuery <object> >(MockBehavior.Strict);
                var query             = new EnumerableQuery <object>(expression);
                // This converts the anonymous types back to typed values.
                var result   = query.Select(item => GetItemFromDynamic <T>(item)).ToList();
                var response = new FeedResponse <T>(result);

                mockDocumentQuery.Setup(_ => _.Provider)
                .Returns(provider.Object);
                mockDocumentQuery.Setup(_ => _.Expression)
                .Returns(expression);

                mockDocumentQuery
                .SetupSequence(_ => _.HasMoreResults)
                .Returns(true)
                .Returns(false);

                mockDocumentQuery
                .Setup(_ => _.ExecuteNextAsync <T>(It.IsAny <CancellationToken>()))
                .ReturnsAsync(response);

                mockDocumentQuery
                .Setup(_ => _.GetEnumerator())
                .Returns(query.AsEnumerable().GetEnumerator());

                return(mockDocumentQuery.Object);
            });

            // Typed query for the initial "where clauses"
            provider
            .Setup(_ => _.CreateQuery <T>(It.IsAny <Expression>()))
            .Returns((Expression expression) =>
            {
                var mockDocumentQuery = new Mock <IFakeDocumentQuery <T> >(MockBehavior.Strict);
                var query             = new EnumerableQuery <T>(expression);
                //var response = new FeedResponse<T>(query);

                var response = ToFeedResponse(query);

                // Required for selects to pass to the above more generic version
                mockDocumentQuery.Setup(_ => _.Provider)
                .Returns(provider.Object);
                mockDocumentQuery.Setup(_ => _.Expression)
                .Returns(expression);

                mockDocumentQuery
                .SetupSequence(_ => _.HasMoreResults)
                .Returns(true)
                .Returns(false);

                mockDocumentQuery
                .Setup(_ => _.ExecuteNextAsync <T>(It.IsAny <CancellationToken>()))
                .ReturnsAsync(response);

                mockDocumentQuery
                .Setup(_ => _.GetEnumerator())
                .Returns(query.AsEnumerable().GetEnumerator());

                return(mockDocumentQuery.Object);
            });

            mockOrderedQueryable.Setup(x => x.Provider).Returns(provider.Object);
            mockOrderedQueryable.Setup(x => x.Expression).Returns(() => dataSource.Expression);

            client.Setup(c => c.CreateDocumentQuery <T>(It.IsAny <Uri>(), It.IsAny <FeedOptions>()))
            .Returns(mockOrderedQueryable.Object);

            client.Setup(c => c.CreateDocumentAsync(
                             It.IsAny <Uri>(),
                             It.IsAny <T>(),
                             null,
                             false,
                             default(CancellationToken)))
            .Returns((Uri uri, T item, RequestOptions ro, bool b, CancellationToken ct) => GetDocumentForItem(item));

            client.Setup(c => c.CreateDocumentAsync(
                             It.IsAny <Uri>(),
                             It.IsAny <AuditEntry <T> >(),
                             null,
                             false,
                             default(CancellationToken)))
            .Returns((Uri uri, AuditEntry <T> item, RequestOptions ro, bool b, CancellationToken ct) => GetDocumentForItem(item));


            mockOrderedQueryable
            .Setup(_ => _.GetEnumerator())
            .Returns(dataSource.AsEnumerable().GetEnumerator());

            client.Setup(c => c.ReadDocumentAsync(It.IsAny <Uri>(),
                                                  null,
                                                  default(CancellationToken))).Returns((Uri item, RequestOptions ro, CancellationToken ct) => GetDocumentByIdForItem(item, mockOrderedQueryable.Object));

            client.Setup(c => c.UpsertDocumentAsync(
                             It.IsAny <Uri>(),
                             It.IsAny <T>(),
                             null,
                             false,
                             default(CancellationToken)))
            .Returns((Uri uri, T item, RequestOptions ro, bool b, CancellationToken ct) => GetDocumentForItem(item));

            return(client);
        }