public async Task CannotCallQueryAsyncWithNullContext() { var instance = new DefaultQueryHandler( Sourcer, Authorizer, Expander, Processor); Func <Task> act = () => instance.QueryAsync(default(QueryContext), CancellationToken.None); await act.Should().ThrowAsync <ArgumentNullException>(); }
/// <summary> /// Asynchronously queries for data using an API context. /// </summary> /// <param name="api"> /// An API. /// </param> /// <param name="request"> /// A query request. /// </param> /// <param name="cancellationToken"> /// An optional cancellation token. /// </param> /// <returns> /// A task that represents the asynchronous /// operation whose result is a query result. /// </returns> public static async Task <QueryResult> QueryAsync(this ApiBase api, QueryRequest request, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.NotNull(api, nameof(api)); Ensure.NotNull(request, nameof(request)); var queryContext = new QueryContext(api.ServiceProvider, request); var model = await api.GetModelAsync(cancellationToken).ConfigureAwait(false); queryContext.Model = model; return(await DefaultQueryHandler.QueryAsync(queryContext, cancellationToken).ConfigureAwait(false)); }
/// <summary> /// Asynchronously queries for data using an API context. /// </summary> /// <param name="context"> /// An API context. /// </param> /// <param name="request"> /// A query request. /// </param> /// <param name="cancellationToken"> /// An optional cancellation token. /// </param> /// <returns> /// A task that represents the asynchronous /// operation whose result is a query result. /// </returns> public static async Task <QueryResult> QueryAsync( ApiContext context, QueryRequest request, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.NotNull(context, "context"); Ensure.NotNull(request, "request"); var queryContext = new QueryContext(context, request); var model = await Api.GetModelAsync(context); queryContext.Model = model; return(await DefaultQueryHandler.QueryAsync(queryContext, cancellationToken)); }
public async Task CanCallQueryAsyncWithCount() { var instance = new DefaultQueryHandler( Sourcer, Authorizer, Expander, Processor); var modelMock = new Mock <IEdmModel>(); var entityContainerMock = new Mock <IEdmEntityContainer>(); var list = new List <IEdmEntityContainerElement>(); var entityContainerElementItemMock = new Mock <IEdmEntityContainerElement>(); entityContainerElementItemMock.Setup(x => x.Name).Returns("Tests"); list.Add(entityContainerElementItemMock.Object); modelMock.Setup(x => x.EntityContainer).Returns(entityContainerMock.Object); entityContainerMock.Setup(x => x.Elements).Returns(list); serviceProviderFixture.QueryExecutor.Setup(x => x.ExecuteExpressionAsync <long>( It.IsAny <QueryContext>(), It.IsAny <IQueryProvider>(), It.IsAny <Expression>(), It.IsAny <CancellationToken>())).Returns <QueryContext, IQueryProvider, Expression, CancellationToken>( (q, qp, e, c) => Task.FromResult(new QueryResult(new[] { Expression.Lambda <Func <long> >(e, null).Compile()() }))); var queryContext = new QueryContext( new TestApi(serviceProviderFixture.ServiceProvider.Object), new QueryRequest(new QueryableSource <Test>(Expression.Constant(queryable))) { ShouldReturnCount = true, }) { Model = modelMock.Object, }; var cancellationToken = CancellationToken.None; var result = await instance.QueryAsync(queryContext, cancellationToken); result.Results.Should().BeEquivalentTo(new[] { queryable.LongCount() }); }