public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions { Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); client.Setup(x => x.GetCachedContainerQueryPropertiesAsync(It.IsAny <Uri>(), It.IsAny <Cosmos.PartitionKey?>(), cancellationtoken)).Returns(Task.FromResult(new ContainerQueryProperties("mockContainer", null, partitionKeyDefinition))); client.Setup(x => x.ByPassQueryParsing()).Returns(false); client.Setup(x => x.GetPartitionedQueryExecutionInfoAsync( sqlQuerySpec, partitionKeyDefinition, true, isContinuationExpected, allowNonValueAggregateQuery, false, // has logical partition key cancellationtoken)).Throws(new InvalidOperationException("Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query")); CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters() { SqlQuerySpec = sqlQuerySpec, InitialUserContinuationToken = null, MaxBufferedItemCount = queryRequestOptions?.MaxBufferedItemCount, MaxConcurrency = queryRequestOptions?.MaxConcurrency, MaxItemCount = queryRequestOptions?.MaxItemCount, PartitionKey = queryRequestOptions?.PartitionKey, Properties = queryRequestOptions?.Properties }; CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore( client: client.Object, queryRequestOptions: queryRequestOptions, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), resourceLink: new Uri("dbs/mockdb/colls/mockColl", UriKind.Relative), isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); CosmosQueryExecutionContextFactory factory = new CosmosQueryExecutionContextFactory( cosmosQueryContext: cosmosQueryContext, inputParameters: inputParameters); await factory.ExecuteNextAsync(cancellationtoken); }
public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions(); queryRequestOptions.Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CollectionCache> mockCollectionCache = new Mock <CollectionCache>(); mockCollectionCache.Setup(x => x.ResolveCollectionAsync(It.IsAny <DocumentServiceRequest>(), cancellationtoken)).Returns(Task.FromResult(new ContainerProperties("mockContainer", "/pk"))); Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); client.Setup(x => x.GetCollectionCacheAsync()).Returns(Task.FromResult(mockCollectionCache.Object)); client.Setup(x => x.ByPassQueryParsing()).Returns(false); client.Setup(x => x.GetPartitionedQueryExecutionInfoAsync( sqlQuerySpec, partitionKeyDefinition, true, isContinuationExpected, allowNonValueAggregateQuery, false, // has logical partition key cancellationtoken)).Throws(new InvalidOperationException("Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query")); CosmosQueryExecutionContextFactory factory = new CosmosQueryExecutionContextFactory( client: client.Object, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), sqlQuerySpec: sqlQuerySpec, continuationToken: null, queryRequestOptions: queryRequestOptions, resourceLink: new Uri("dbs/mockdb/colls/mockColl", UriKind.Relative), isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); await factory.ExecuteNextAsync(cancellationtoken); }