public void CanConstruct() { var instance = new DefaultQueryHandler( Sourcer, Authorizer, Expander, Processor); instance.Should().NotBeNull(); }
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() }); }
/// <summary> /// Initializes a new instance of the <see cref="ApiBase" /> class. /// </summary> /// <param name="serviceProvider"> /// An <see cref="IServiceProvider"/> containing all services. /// </param> protected ApiBase(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; //RWM: This stuff SHOULD be getting passed into a constructor. But the DI implementation is less than awesome. // So we'll work around it for now and still save some allocations. // There are certain unit te var queryExpressionSourcer = serviceProvider.GetService <IQueryExpressionSourcer>(); var queryExpressionAuthorizer = serviceProvider.GetService <IQueryExpressionAuthorizer>(); var queryExpressionExpander = serviceProvider.GetService <IQueryExpressionExpander>(); var queryExpressionProcessor = serviceProvider.GetService <IQueryExpressionProcessor>(); var changeSetInitializer = serviceProvider.GetService <IChangeSetInitializer>(); var changeSetItemAuthorizer = serviceProvider.GetService <IChangeSetItemAuthorizer>(); var changeSetItemValidator = serviceProvider.GetService <IChangeSetItemValidator>(); var changeSetItemFilter = serviceProvider.GetService <IChangeSetItemFilter>(); var submitExecutor = serviceProvider.GetService <ISubmitExecutor>(); if (queryExpressionSourcer == null) { // Missing sourcer throw new NotSupportedException(Resources.QuerySourcerMissing); } if (changeSetInitializer == null) { throw new NotSupportedException(Resources.ChangeSetPreparerMissing); } if (submitExecutor == null) { throw new NotSupportedException(Resources.SubmitExecutorMissing); } queryHandler = new DefaultQueryHandler(queryExpressionSourcer, queryExpressionAuthorizer, queryExpressionExpander, queryExpressionProcessor); submitHandler = new DefaultSubmitHandler(changeSetInitializer, submitExecutor, changeSetItemAuthorizer, changeSetItemValidator, changeSetItemFilter); }