コード例 #1
0
        private static IList <ToDoItem> GenerateAndMockResponseHelper(
            Mock <CosmosQueryClient> mockQueryClient,
            IList <ToDoItem> allItemsOrdered,
            bool isOrderByQuery,
            SqlQuerySpec sqlQuerySpec,
            string containerRid,
            string initContinuationToken,
            int maxPageSize,
            MockPartitionResponse[] mockResponseForSinglePartition,
            CancellationToken cancellationTokenForMocks)
        {
            if (mockResponseForSinglePartition == null)
            {
                throw new ArgumentNullException(nameof(mockResponseForSinglePartition));
            }

            // Loop through all the partitions
            foreach (MockPartitionResponse partitionAndMessages in mockResponseForSinglePartition)
            {
                PartitionKeyRange partitionKeyRange = partitionAndMessages.PartitionKeyRange;

                string previousContinuationToken = initContinuationToken;

                // Loop through each message inside the partition
                List <int[]> pages         = partitionAndMessages.MessagesWithItemIndex;
                int          pagesCount    = pages == null ? 0 : pages.Count;
                int          lastPageIndex = pagesCount - 1;
                for (int i = 0; i < pagesCount; i++)
                {
                    int[]             itemsInPage          = partitionAndMessages.MessagesWithItemIndex[i];
                    string            newContinuationToken = null;
                    QueryResponseCore queryResponse;

                    bool isFailureResponse = itemsInPage.Length == 1 && itemsInPage[0] < 0;
                    if (isFailureResponse)
                    {
                        if (itemsInPage[0] == MockPartitionResponse.MessageWithToManyRequestFailure)
                        {
                            queryResponse = QueryResponseMessageFactory.CreateFailureToManyRequestResponse();
                        }
                        else
                        {
                            throw new ArgumentException($"Unknown mocked failure response {itemsInPage[0]}");
                        }
                    }
                    else
                    {
                        List <ToDoItem> currentPageItems = new List <ToDoItem>();

                        // Null represents an empty page. Page with a single negative value represents a failure response
                        if (itemsInPage != null)
                        {
                            foreach (int itemPosition in itemsInPage)
                            {
                                currentPageItems.Add(allItemsOrdered[itemPosition]);
                            }
                        }

                        // Last message should have null continuation token
                        // Split means it's not the last message for this PK range
                        if (i != lastPageIndex || partitionAndMessages.HasSplit)
                        {
                            newContinuationToken = Guid.NewGuid().ToString();
                        }

                        queryResponse = QueryResponseMessageFactory.CreateQueryResponse(
                            currentPageItems,
                            isOrderByQuery,
                            newContinuationToken,
                            containerRid);
                    }

                    mockQueryClient.Setup(x =>
                                          x.ExecuteItemQueryAsync(
                                              It.IsAny <Uri>(),
                                              ResourceType.Document,
                                              OperationType.Query,
                                              It.IsAny <QueryRequestOptions>(),
                                              It.Is <SqlQuerySpec>(specInput => MockItemProducerFactory.IsSqlQuerySpecEqual(sqlQuerySpec, specInput)),
                                              previousContinuationToken,
                                              It.Is <PartitionKeyRangeIdentity>(rangeId => string.Equals(rangeId.PartitionKeyRangeId, partitionKeyRange.Id) && string.Equals(rangeId.CollectionRid, containerRid)),
                                              It.IsAny <bool>(),
                                              maxPageSize,
                                              It.IsAny <SchedulingStopwatch>(),
                                              cancellationTokenForMocks))
                    .Returns(Task.FromResult(queryResponse));

                    previousContinuationToken = newContinuationToken;
                }

                if (partitionAndMessages.HasSplit)
                {
                    QueryResponseCore querySplitResponse = QueryResponseMessageFactory.CreateSplitResponse(containerRid);

                    mockQueryClient.Setup(x =>
                                          x.TryGetOverlappingRangesAsync(
                                              containerRid,
                                              It.Is <Documents.Routing.Range <string> >(inputRange => inputRange.Equals(partitionKeyRange.ToRange())),
                                              true)).Returns(Task.FromResult(partitionAndMessages.GetPartitionKeyRangeOfSplit()));

                    mockQueryClient.Setup(x =>
                                          x.ExecuteItemQueryAsync(
                                              It.IsAny <Uri>(),
                                              ResourceType.Document,
                                              OperationType.Query,
                                              It.IsAny <QueryRequestOptions>(),
                                              It.Is <SqlQuerySpec>(specInput => MockItemProducerFactory.IsSqlQuerySpecEqual(sqlQuerySpec, specInput)),
                                              previousContinuationToken,
                                              It.Is <PartitionKeyRangeIdentity>(rangeId => string.Equals(rangeId.PartitionKeyRangeId, partitionKeyRange.Id) && string.Equals(rangeId.CollectionRid, containerRid)),
                                              It.IsAny <bool>(),
                                              maxPageSize,
                                              It.IsAny <SchedulingStopwatch>(),
                                              cancellationTokenForMocks))
                    .Returns(Task.FromResult(querySplitResponse));

                    GenerateAndMockResponseHelper(
                        mockQueryClient: mockQueryClient,
                        allItemsOrdered: allItemsOrdered,
                        isOrderByQuery: isOrderByQuery,
                        sqlQuerySpec: sqlQuerySpec,
                        containerRid: containerRid,
                        initContinuationToken: previousContinuationToken,
                        maxPageSize: maxPageSize,
                        mockResponseForSinglePartition: partitionAndMessages.Split,
                        cancellationTokenForMocks: cancellationTokenForMocks);
                }
            }

            return(allItemsOrdered);
        }