コード例 #1
0
        ValueTask <TResult> IAsyncQueryProvider.ExecuteAsync <TResult>(Expression expression, CancellationToken cancellationToken)
        {
            if (TryResolveAggregate(expression, out var query, out var aggregate))
            {
                if (ReferenceEquals(query, this))
                {
                    query = Tail;                               // skip a level of indirection when possible
                }
                return(query.Provider.ExecuteAggregateAsync <TResult>(query, aggregate, cancellationToken));
            }

            // RX doesn't provide default implementations for ToListAsync/ToArrayAsync - we need to do that ourselves
            if (IsFromQueryable(expression, out var method, out var args, out query, out var argCount))
            {
                if (ReferenceEquals(query, this))
                {
                    query = Tail;                               // skip a level of indirection when possible
                }
                switch (method.Name)
                {
                case nameof(AsyncQueryable.ToListAsync) when argCount == 0 && typeof(TResult) == typeof(List <T>):
                    if (query.Take == 0)
                    {
                        return(CoerceAsync <List <T>, TResult>(new List <T>()));                  // trivial
                    }
                    return(Coerce <ValueTask <List <T> >, ValueTask <TResult> >(QueryableAsyncExtensions.ToListCoreAsync <T>(query, cancellationToken)));

                case nameof(AsyncQueryable.ToArrayAsync) when argCount == 0 && typeof(TResult) == typeof(T[]):
                    if (query.Take == 0)
                    {
                        return(CoerceAsync <T[], TResult>(Array.Empty <T>()));                   // trivial
                    }
                    return(Coerce <ValueTask <T[]>, ValueTask <TResult> >(QueryableAsyncExtensions.ToArrayCoreAsync <T>(query, cancellationToken)));
                }
            }
            ThrowNotSupported(expression);
            return(default !);
コード例 #2
0
ファイル: AsyncExtensionsTests.cs プロジェクト: nsarris/DALIA
        public async Task Linq2dbAsyncTestAsync()
        {
            var mockConn = new Mock <IDbConnection>();

            var mockProvider = new Mock <LinqToDB.DataProvider.IDataProvider>();

            mockProvider.Setup(x => x.Name).Returns("TestProvider");
            mockProvider.Setup(x => x.IsCompatibleConnection(It.IsAny <IDbConnection>())).Returns(true);
            mockProvider.Setup(x => x.MappingSchema).Returns(new MappingSchema());
            mockProvider.Setup(x => x.GetSqlOptimizer()).Throws(new TestAsyncMethodsException());

            var c = new LinqToDB.Data.DataConnection(mockProvider.Object, mockConn.Object);
            var l = c.GetTable <Customer>();

            Assert.That(async() => { return(await QueryableAsyncExtensions.AllAsync(l, x => x.Int > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AnyAsync(l, x => x.Int > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AnyAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.NullableDecimal).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Decimal).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.NullableDouble).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Double).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.NullableFloat).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Float).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.NullableInt).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Int).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Int).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.NullableLong).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l.Select(cust => cust.Long).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.NullableDecimal)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Decimal)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.NullableDouble)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Double)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.NullableFloat)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Float)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.NullableInt)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Int)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Int)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.NullableLong)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.AverageAsync(l, cust => cust.Long)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.ContainsAsync(l, new Customer())); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.CountAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.CountAsync(l, x => x.Id > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.FirstAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.FirstAsync(l, cust => cust.Int > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.FirstOrDefaultAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.FirstOrDefaultAsync(l, cust => cust.Int > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { await QueryableAsyncExtensions.ForEachAsync <Customer>(l, customer => { }); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { await QueryableAsyncExtensions.ForEachUntilAsync(l, cust => cust.Int > 0); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.LongCountAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.LongCountAsync(l, cust => cust.Int > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.MaxAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.MaxAsync(l, cust => cust.Long > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.MinAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.MinAsync(l, cust => cust.Long > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.SingleAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SingleAsync(l, cust => cust.Long > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.SingleOrDefaultAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SingleOrDefaultAsync(l, cust => cust.Long > 0)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.NullableDecimal).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Decimal).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.NullableDouble).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Double).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.NullableFloat).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Float).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.NullableInt).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Int).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Int).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.NullableLong).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l.Select(cust => cust.Long).AsQueryable())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.NullableDecimal)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Decimal)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.NullableDouble)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Double)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.NullableFloat)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Float)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.NullableInt)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Int)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Int)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.NullableLong)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.SumAsync(l, cust => cust.Long)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.ToArrayAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.ToDictionaryAsync(l, (x) => x.Id)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.ToDictionaryAsync(l, (x) => x.Id, x => x.Float)); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.ToDictionaryAsync(l, (x) => x.Id, x => x.Float, new IntEqualityComparer())); }, Throws.TypeOf <TestAsyncMethodsException>());
            Assert.That(async() => { return(await QueryableAsyncExtensions.ToDictionaryAsync(l, (x) => x.Id, new IntEqualityComparer())); }, Throws.TypeOf <TestAsyncMethodsException>());

            Assert.That(async() => { return(await QueryableAsyncExtensions.ToListAsync(l)); }, Throws.TypeOf <TestAsyncMethodsException>());
        }