private async Task <IList <ToDoActivity> > CreateRandomItems(ContainerInternal container, int pkCount, int perPKItemCount = 1, bool randomPartitionKey = true) { Assert.IsFalse(!randomPartitionKey && perPKItemCount > 1); List <ToDoActivity> createdList = new List <ToDoActivity>(); for (int i = 0; i < pkCount; i++) { string pk = "TBD"; if (randomPartitionKey) { pk += Guid.NewGuid().ToString(); } for (int j = 0; j < perPKItemCount; j++) { ToDoActivity temp = ToDoActivity.CreateRandomToDoActivity(pk: pk); createdList.Add(temp); await container.CreateItemAsync <ToDoActivity>(item : temp); } } return(createdList); }
public async Task ExecuteNonePkTest() { // Create a container in v2 without a partition key string containerId = "SprocPkNone" + Guid.NewGuid().ToString(); ContainerInternal containerNonePk = await NonPartitionedContainerHelper.CreateNonPartitionedContainer(this.database, containerId); Scripts scriptsNonePk = containerNonePk.Scripts; string sprocId = Guid.NewGuid().ToString(); string sprocBody = @"function() { var context = getContext(); var response = context.getResponse(); var collection = context.getCollection(); var collectionLink = collection.getSelfLink(); var filterQuery = 'SELECT * FROM c'; collection.queryDocuments(collectionLink, filterQuery, { }, function(err, documents) { response.setBody(documents); } ); }"; StoredProcedureResponse storedProcedureResponse = await scriptsNonePk.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody)); Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode); StoredProcedureTests.ValidateStoredProcedureSettings(sprocId, sprocBody, storedProcedureResponse); // Insert document and then query string testPartitionId = Guid.NewGuid().ToString(); var payload = new { id = testPartitionId, user = testPartitionId }; ItemResponse <dynamic> createItemResponse = await containerNonePk.CreateItemAsync <dynamic>(payload); Assert.AreEqual(HttpStatusCode.Created, createItemResponse.StatusCode); StoredProcedureProperties storedProcedure = storedProcedureResponse; StoredProcedureExecuteResponse <JArray> sprocResponse = await scriptsNonePk.ExecuteStoredProcedureAsync <JArray>( sprocId, Cosmos.PartitionKey.None, parameters : null); Assert.AreEqual(HttpStatusCode.OK, sprocResponse.StatusCode); JArray jArray = sprocResponse; Assert.AreEqual(1, jArray.Count); Assert.AreEqual(testPartitionId, jArray[0]["id"]); StoredProcedureResponse deleteResponse = await scriptsNonePk.DeleteStoredProcedureAsync(sprocId); Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode); }
private static async Task <ItemResponse <TestDoc> > CreateItemAsync( ContainerInternal containerCore, string dekId, List <string> pathsToEncrypt, string partitionKey = null) { TestDoc testDoc = TestDoc.Create(partitionKey); ItemResponse <TestDoc> createResponse = await containerCore.CreateItemAsync( testDoc, new PartitionKey(testDoc.PK), EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt)); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); Assert.AreEqual(testDoc, createResponse.Resource); return(createResponse); }
public async Task ChangeFeedIteratorCore_PartitionKey_OfT_ReadAll() { int totalCount = 0; int firstRunTotal = 25; int batchSize = 25; string pkToRead = "pkToRead"; string otherPK = "otherPK"; ContainerInternal itemsCore = await this.InitializeContainerAsync(); for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead)); } for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK)); } FeedIterator <ToDoActivity> feedIterator = itemsCore.GetChangeFeedIterator <ToDoActivity>( ChangeFeedStartFrom.Beginning( new FeedRangePartitionKey( new PartitionKey(pkToRead))), ChangeFeedMode.Incremental, new ChangeFeedRequestOptions() { PageSizeHint = 1, }); string continuation = null; while (feedIterator.HasMoreResults) { try { FeedResponse <ToDoActivity> feedResponse = await feedIterator.ReadNextAsync(this.cancellationToken); totalCount += feedResponse.Count; foreach (ToDoActivity toDoActivity in feedResponse) { Assert.AreEqual(pkToRead, toDoActivity.pk); } continuation = feedResponse.ContinuationToken; } catch (CosmosException cosmosException) when(cosmosException.StatusCode == HttpStatusCode.NotModified) { continuation = cosmosException.Headers.ContinuationToken; break; } } Assert.AreEqual(firstRunTotal, totalCount); int expectedFinalCount = 50; // Insert another batch of 25 and use the last FeedToken from the first cycle for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead)); } FeedIterator <ToDoActivity> setIteratorNew = itemsCore.GetChangeFeedIterator <ToDoActivity>( ChangeFeedStartFrom.ContinuationToken(continuation), ChangeFeedMode.Incremental); while (setIteratorNew.HasMoreResults) { try { FeedResponse <ToDoActivity> feedResponse = await setIteratorNew.ReadNextAsync(this.cancellationToken); totalCount += feedResponse.Count; foreach (ToDoActivity toDoActivity in feedResponse) { Assert.AreEqual(pkToRead, toDoActivity.pk); } } catch (CosmosException cosmosException) when(cosmosException.StatusCode == HttpStatusCode.NotModified) { break; } } Assert.AreEqual(expectedFinalCount, totalCount); }
public async Task ChangeFeedIteratorCore_PartitionKey_ReadAll() { int totalCount = 0; int firstRunTotal = 25; int batchSize = 25; string pkToRead = "pkToRead"; string otherPK = "otherPK"; ContainerInternal itemsCore = await this.InitializeContainerAsync(); for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead)); } for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK)); } ChangeFeedIteratorCore feedIterator = itemsCore.GetChangeFeedStreamIterator( ChangeFeedStartFrom.Beginning( FeedRange.FromPartitionKey( new PartitionKey(pkToRead))), ChangeFeedMode.Incremental, new ChangeFeedRequestOptions() { PageSizeHint = 1, }) as ChangeFeedIteratorCore; string continuation = null; while (feedIterator.HasMoreResults) { using (ResponseMessage responseMessage = await feedIterator.ReadNextAsync(this.cancellationToken)) { if (!responseMessage.IsSuccessStatusCode) { continuation = responseMessage.ContinuationToken; break; } Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data; totalCount += response.Count; foreach (ToDoActivity toDoActivity in response) { Assert.AreEqual(pkToRead, toDoActivity.pk); } } } Assert.AreEqual(firstRunTotal, totalCount); int expectedFinalCount = 50; // Insert another batch of 25 and use the last FeedToken from the first cycle for (int i = 0; i < batchSize; i++) { await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead)); } ChangeFeedIteratorCore setIteratorNew = itemsCore.GetChangeFeedStreamIterator( ChangeFeedStartFrom.ContinuationToken(continuation), ChangeFeedMode.Incremental) as ChangeFeedIteratorCore; while (setIteratorNew.HasMoreResults) { using (ResponseMessage responseMessage = await setIteratorNew.ReadNextAsync(this.cancellationToken)) { if (!responseMessage.IsSuccessStatusCode) { break; } Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data; totalCount += response.Count; foreach (ToDoActivity toDoActivity in response) { Assert.AreEqual(pkToRead, toDoActivity.pk); } } } Assert.AreEqual(expectedFinalCount, totalCount); }