public async Task BatchOperationDiagnostic(bool disableDiagnostics) { string pkValue = "DiagnosticTestPk"; TransactionalBatch batch = this.Container.CreateTransactionalBatch(new PartitionKey(pkValue)); BatchCore batchCore = (BatchCore)batch; List <PatchOperation> patch = new List <PatchOperation>() { PatchOperation.Remove("/cost") }; List <ToDoActivity> createItems = new List <ToDoActivity>(); for (int i = 0; i < 50; i++) { ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue); createItems.Add(item); batch.CreateItem <ToDoActivity>(item); } for (int i = 0; i < 20; i++) { batch.ReadItem(createItems[i].id); batchCore.PatchItem(createItems[i].id, patch); } TransactionalBatchRequestOptions requestOptions = disableDiagnostics ? RequestOptionDisableDiagnostic : null; TransactionalBatchResponse response = await batch.ExecuteAsync(requestOptions); Assert.IsNotNull(response); CosmosDiagnosticsTests.VerifyPointDiagnostics( diagnostics: response.Diagnostics, disableDiagnostics: disableDiagnostics); }
public async Task BatchInvalidOptionsAsync() { Container container = BatchUnitTests.GetContainer(); List <RequestOptions> badBatchOptionsList = new List <RequestOptions>() { new RequestOptions() { IfMatchEtag = "cond", }, new RequestOptions() { IfNoneMatchEtag = "cond2", } }; foreach (RequestOptions batchOptions in badBatchOptionsList) { BatchCore batch = (BatchCore) new BatchCore((ContainerInternal)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1)) .ReadItem("someId"); await BatchUnitTests.VerifyExceptionThrownOnExecuteAsync( batch, typeof(ArgumentException), ClientResources.BatchRequestOptionNotSupported, batchOptions); } }
private async Task <Container> RunWithErrorAsync( Container container, Action <TransactionalBatch> appendOperation, HttpStatusCode expectedFailedOperationStatusCode) { TestDoc testDocToCreate = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TestDoc anotherTestDocToCreate = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TransactionalBatch batch = new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItem(testDocToCreate); appendOperation(batch); TransactionalBatchResponse batchResponse = await batch .CreateItem(anotherTestDocToCreate) .ExecuteAsync(); BatchSinglePartitionKeyTests.VerifyBatchProcessed( batchResponse, numberOfOperations: 3, expectedStatusCode: expectedFailedOperationStatusCode); Assert.AreEqual((HttpStatusCode)StatusCodes.FailedDependency, batchResponse[0].StatusCode); Assert.AreEqual(expectedFailedOperationStatusCode, batchResponse[1].StatusCode); Assert.AreEqual((HttpStatusCode)StatusCodes.FailedDependency, batchResponse[2].StatusCode); await BatchTestBase.VerifyNotFoundAsync(container, testDocToCreate); await BatchTestBase.VerifyNotFoundAsync(container, anotherTestDocToCreate); return(container); }
public async Task BatchServerResponseTooLargeAsync() { Container container = BatchTestBase.JsonContainer; const int operationCount = 10; int appxDocSizeInBytes = 1 * 1024 * 1024; TestDoc doc = await BatchTestBase.CreateJsonTestDocAsync(container, this.PartitionKey1, appxDocSizeInBytes); TransactionalBatch batch = new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)); for (int i = 0; i < operationCount; i++) { batch.ReadItem(doc.Id); } TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); BatchSinglePartitionKeyTests.VerifyBatchProcessed( batchResponse, numberOfOperations: operationCount, expectedStatusCode: HttpStatusCode.RequestEntityTooLarge); Assert.AreEqual((int)StatusCodes.FailedDependency, (int)batchResponse[0].StatusCode); Assert.AreEqual(HttpStatusCode.RequestEntityTooLarge, batchResponse[operationCount - 1].StatusCode); }
public async Task BatchItemTimeToLiveAsync() { // Verify with schematized containers where we are allowed to send TTL as a header const bool isSchematized = true; const bool isStream = true; Container container = BatchTestBase.SchematizedContainer; await this.CreateSchematizedTestDocsAsync(container); { TestDoc testDocToCreate = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TestDoc anotherTestDocToCreate = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TestDoc testDocToReplace = this.GetTestDocCopy(this.TestDocPk1ExistingA); testDocToReplace.Cost++; const int ttlInSeconds = 3; const int infiniteTtl = -1; TestDoc testDocToUpsert = await BatchTestBase.CreateSchematizedTestDocAsync(container, this.PartitionKey1, ttlInSeconds : ttlInSeconds); testDocToUpsert.Cost++; BatchCore batch = (BatchCore)(new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItemStream( BatchTestBase.TestDocToStream(testDocToCreate, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToCreate, isSchematized, ttlInSeconds: ttlInSeconds)) .CreateItemStream( BatchTestBase.TestDocToStream(anotherTestDocToCreate, isSchematized), BatchTestBase.GetBatchItemRequestOptions(anotherTestDocToCreate, isSchematized)) .ReplaceItemStream( BatchTestBase.GetId(testDocToReplace, isSchematized), BatchTestBase.TestDocToStream(testDocToReplace, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToReplace, isSchematized, ttlInSeconds: ttlInSeconds)) .UpsertItemStream( BatchTestBase.TestDocToStream(testDocToUpsert, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToUpsert, isSchematized, ttlInSeconds: infiniteTtl))); TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(BatchTestBase.GetUpdatedBatchRequestOptions(isSchematized: true)); BatchSinglePartitionKeyTests.VerifyBatchProcessed(batchResponse, numberOfOperations: 4); Assert.AreEqual(HttpStatusCode.Created, batchResponse[0].StatusCode); Assert.AreEqual(HttpStatusCode.Created, batchResponse[1].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[2].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[3].StatusCode); // wait for TTL to expire await Task.Delay(TimeSpan.FromSeconds(ttlInSeconds + 1)); await BatchTestBase.VerifyNotFoundAsync(container, testDocToCreate, isSchematized); await BatchTestBase.VerifyByReadAsync(container, anotherTestDocToCreate, isStream, isSchematized); await BatchTestBase.VerifyNotFoundAsync(container, testDocToReplace, isSchematized); await BatchTestBase.VerifyByReadAsync(container, testDocToUpsert, isStream, isSchematized); } }
public async Task BatchNoOperationsAsync() { Container container = BatchUnitTests.GetContainer(); TransactionalBatch batch = new BatchCore((ContainerInternal)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1)); await BatchUnitTests.VerifyExceptionThrownOnExecuteAsync( batch, typeof(ArgumentException), ClientResources.BatchNoOperations); }
public async Task BatchCustomSerializerUsedForPatchAsync() { CosmosClientOptions clientOptions = new CosmosClientOptions() { Serializer = new CosmosJsonDotNetSerializer( new JsonSerializerSettings() { DateFormatString = "yyyy--MM--dd hh:mm" }) }; CosmosClient customSerializationClient = TestCommon.CreateCosmosClient(clientOptions); Container customSerializationContainer = customSerializationClient.GetContainer(BatchTestBase.Database.Id, BatchTestBase.JsonContainer.Id); TestDoc testDoc = BatchTestBase.PopulateTestDoc(this.PartitionKey1); DateTime patchDate = new DateTime(2020, 07, 01, 01, 02, 03); List <PatchOperation> patchOperations = new List <PatchOperation>() { PatchOperation.CreateAddOperation("/date", patchDate) }; BatchCore batch = (BatchCore) new BatchCore((ContainerInlineCore)customSerializationContainer, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItem(testDoc); batch = (BatchCore)batch.PatchItem(testDoc.Id, patchOperations); TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); BatchSinglePartitionKeyTests.VerifyBatchProcessed(batchResponse, numberOfOperations: 2); Assert.AreEqual(HttpStatusCode.Created, batchResponse[0].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[1].StatusCode); JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.DateFormatString = "yyyy--MM--dd hh:mm"; string dateJson = JsonConvert.SerializeObject(patchDate, jsonSettings); // regular container ItemResponse <dynamic> response = await BatchTestBase.JsonContainer.ReadItemAsync <dynamic>( testDoc.Id, BatchTestBase.GetPartitionKey(this.PartitionKey1)); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull(response.Resource); Assert.IsTrue(dateJson.Contains(response.Resource["date"].ToString())); }
public async Task BatchWithTooManyOperationsAsync() { Container container = BatchTestBase.JsonContainer; const int operationCount = Constants.MaxOperationsInDirectModeBatchRequest + 1; TransactionalBatch batch = new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(this.PartitionKey1)); for (int i = 0; i < operationCount; i++) { batch.ReadItem("someId"); } TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); Assert.AreEqual(HttpStatusCode.BadRequest, batchResponse.StatusCode); }
public async Task BatchWithTooManyOperationsAsync() { Container container = BatchUnitTests.GetContainer(); const int operationCount = Constants.MaxOperationsInDirectModeBatchRequest + 1; Batch batch = new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1)); for (int i = 0; i < operationCount; i++) { batch.ReadItem("someId"); } await BatchUnitTests.VerifyExceptionThrownOnExecuteAsync( batch, typeof(ArgumentException), ClientResources.BatchTooLarge); }
public async Task BatchInvalidItemOptionsAsync() { Container container = BatchUnitTests.GetContainer(); List <TransactionalBatchItemRequestOptions> badItemOptionsList = new List <TransactionalBatchItemRequestOptions>() { new TransactionalBatchItemRequestOptions() { Properties = new Dictionary <string, object> { // EPK without string representation { WFConstants.BackendHeaders.EffectivePartitionKey, new byte[1] { 0x41 } } } }, new TransactionalBatchItemRequestOptions() { Properties = new Dictionary <string, object> { // EPK string without corresponding byte representation { WFConstants.BackendHeaders.EffectivePartitionKeyString, "epk" } } }, new TransactionalBatchItemRequestOptions() { Properties = new Dictionary <string, object> { // Partition key without EPK string { HttpConstants.HttpHeaders.PartitionKey, "epk" } } } }; foreach (TransactionalBatchItemRequestOptions itemOptions in badItemOptionsList) { TransactionalBatch batch = new BatchCore((ContainerInternal)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1)) .ReplaceItem("someId", new TestItem("repl"), itemOptions); await BatchUnitTests.VerifyExceptionThrownOnExecuteAsync( batch, typeof(ArgumentException)); } }
private async Task <TransactionalBatchResponse[]> RunTwoLargeBatchesAsync(Container container) { TransactionalBatch batch1 = new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)); TransactionalBatch batch2 = new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)); for (int i = 0; i < Constants.MaxOperationsInDirectModeBatchRequest; i++) { batch1.CreateItem(BatchSinglePartitionKeyTests.PopulateTestDoc(this.PartitionKey1)); batch2.CreateItem(BatchSinglePartitionKeyTests.PopulateTestDoc(this.PartitionKey1)); } Task <TransactionalBatchResponse> batch1Task = batch1.ExecuteAsync(); await Task.Delay(50); Task <TransactionalBatchResponse> batch2Task = batch2.ExecuteAsync(); TransactionalBatchResponse[] batchResponses = await Task.WhenAll(batch1Task, batch2Task); return(batchResponses); }
public async Task BatchLargerThanServerRequestAsync() { Container container = BatchUnitTests.GetContainer(); const int operationCount = 20; int appxDocSize = Constants.MaxDirectModeBatchRequestBodySizeInBytes / operationCount; // Increase the doc size by a bit so all docs won't fit in one server request. appxDocSize = (int)(appxDocSize * 1.05); Batch batch = new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1)); for (int i = 0; i < operationCount; i++) { TestItem testItem = new TestItem(new string('x', appxDocSize)); batch.CreateItem(testItem); } await BatchUnitTests.VerifyExceptionThrownOnExecuteAsync( batch, typeof(RequestEntityTooLargeException)); }
public async Task BatchLargerThanServerRequestAsync() { Container container = BatchTestBase.JsonContainer; const int operationCount = 20; int appxDocSize = Constants.MaxDirectModeBatchRequestBodySizeInBytes / operationCount; // Increase the doc size by a bit so all docs won't fit in one server request. appxDocSize = (int)(appxDocSize * 1.05); TransactionalBatch batch = new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(this.PartitionKey1)); for (int i = 0; i < operationCount; i++) { TestDoc doc = BatchTestBase.PopulateTestDoc(this.PartitionKey1, minDesiredSize: appxDocSize); batch.CreateItem(doc); } TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); Assert.AreEqual(HttpStatusCode.RequestEntityTooLarge, batchResponse.StatusCode); }
public async Task BatchCreateAndPatchAsync() { TestDoc testDoc = BatchTestBase.PopulateTestDoc(this.PartitionKey1); List <PatchOperation> patchOperations = new List <PatchOperation>() { PatchOperation.CreateReplaceOperation("/Cost", testDoc.Cost + 1) }; BatchCore batch = (BatchCore) new BatchCore((ContainerInlineCore)BatchTestBase.JsonContainer, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItem(testDoc); batch = (BatchCore)batch.PatchItem(testDoc.Id, patchOperations); TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); BatchSinglePartitionKeyTests.VerifyBatchProcessed(batchResponse, numberOfOperations: 2); Assert.AreEqual(HttpStatusCode.Created, batchResponse[0].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[1].StatusCode); testDoc.Cost = testDoc.Cost + 1; await BatchTestBase.VerifyByReadAsync(BatchTestBase.JsonContainer, testDoc, isStream : false, isSchematized : false, useEpk : false); }
private async Task <TransactionalBatchResponse> RunCrudAsync(bool isStream, bool isSchematized, bool useEpk, Container container) { RequestOptions batchOptions = null; if (isSchematized) { await this.CreateSchematizedTestDocsAsync(container); batchOptions = BatchTestBase.GetUpdatedBatchRequestOptions(batchOptions, isSchematized, useEpk, this.PartitionKey1); } else { await this.CreateJsonTestDocsAsync(container); } TestDoc testDocToCreate = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TestDoc testDocToUpsert = BatchTestBase.PopulateTestDoc(this.PartitionKey1); TestDoc anotherTestDocToUpsert = this.GetTestDocCopy(this.TestDocPk1ExistingA); anotherTestDocToUpsert.Cost++; TestDoc testDocToReplace = this.GetTestDocCopy(this.TestDocPk1ExistingB); testDocToReplace.Cost++; // We run CRUD operations where all are expected to return HTTP 2xx. TransactionalBatchResponse batchResponse; if (!isStream) { batchResponse = await new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItem(testDocToCreate) .ReadItem(this.TestDocPk1ExistingC.Id) .ReplaceItem(testDocToReplace.Id, testDocToReplace) .UpsertItem(testDocToUpsert) .UpsertItem(anotherTestDocToUpsert) .DeleteItem(this.TestDocPk1ExistingD.Id) .ExecuteAsync(); } else { BatchCore batch = (BatchCore)(new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItemStream( BatchTestBase.TestDocToStream(testDocToCreate, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToCreate, isSchematized)) .ReadItem( BatchTestBase.GetId(this.TestDocPk1ExistingC, isSchematized), BatchTestBase.GetBatchItemRequestOptions(this.TestDocPk1ExistingC, isSchematized)) .ReplaceItemStream( BatchTestBase.GetId(testDocToReplace, isSchematized), BatchTestBase.TestDocToStream(testDocToReplace, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToReplace, isSchematized)) .UpsertItemStream( BatchTestBase.TestDocToStream(testDocToUpsert, isSchematized), BatchTestBase.GetBatchItemRequestOptions(testDocToUpsert, isSchematized)) .UpsertItemStream( BatchTestBase.TestDocToStream(anotherTestDocToUpsert, isSchematized), BatchTestBase.GetBatchItemRequestOptions(anotherTestDocToUpsert, isSchematized)) .DeleteItem( BatchTestBase.GetId(this.TestDocPk1ExistingD, isSchematized), BatchTestBase.GetBatchItemRequestOptions(this.TestDocPk1ExistingD, isSchematized))); batchResponse = await batch.ExecuteAsync(batchOptions); } BatchSinglePartitionKeyTests.VerifyBatchProcessed(batchResponse, numberOfOperations: 6); Assert.AreEqual(HttpStatusCode.Created, batchResponse[0].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[1].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[2].StatusCode); Assert.AreEqual(HttpStatusCode.Created, batchResponse[3].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse[4].StatusCode); Assert.AreEqual(HttpStatusCode.NoContent, batchResponse[5].StatusCode); if (!isStream) { Assert.AreEqual(this.TestDocPk1ExistingC, batchResponse.GetOperationResultAtIndex <TestDoc>(1).Resource); } else { Assert.AreEqual(this.TestDocPk1ExistingC, BatchTestBase.StreamToTestDoc(batchResponse[1].ResourceStream, isSchematized)); } await BatchTestBase.VerifyByReadAsync(container, testDocToCreate, isStream, isSchematized, useEpk); await BatchTestBase.VerifyByReadAsync(container, testDocToReplace, isStream, isSchematized, useEpk); await BatchTestBase.VerifyByReadAsync(container, testDocToUpsert, isStream, isSchematized, useEpk); await BatchTestBase.VerifyByReadAsync(container, anotherTestDocToUpsert, isStream, isSchematized, useEpk); await BatchTestBase.VerifyNotFoundAsync(container, this.TestDocPk1ExistingD, isSchematized, useEpk); return(batchResponse); }
public async Task ItemBatchNoResponseTest() { TransactionalBatchItemRequestOptions requestOptions = new TransactionalBatchItemRequestOptions() { EnableContentResponseOnWrite = false }; string pkId = "TestBatchId"; TransactionalBatch batch = this.container.CreateTransactionalBatch(new PartitionKey(pkId)); int noResponseItemCount = 100; for (int i = 0; i < noResponseItemCount; i++) { ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId); batch.CreateItem <ToDoActivity>(item, requestOptions: requestOptions); } TransactionalBatchResponse response = await batch.ExecuteAsync(); Assert.AreEqual(100, response.Count); this.ValidateResponse(response, noResponseItemCount); pkId = "TestBatchId2"; batch = this.container.CreateTransactionalBatch(new PartitionKey(pkId)); BatchCore batchCore = (BatchCore)batch; List <PatchOperation> patch = new List <PatchOperation>() { PatchOperation.CreateRemoveOperation("/cost") }; noResponseItemCount = 0; for (int i = 0; i < 10; i++) { ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId); batch.CreateItem <ToDoActivity>(item, requestOptions: requestOptions); noResponseItemCount++; ToDoActivity item2 = ToDoActivity.CreateRandomToDoActivity(pk: pkId); item2.id = item.id; batch.ReplaceItem <ToDoActivity>(item2.id, item2, requestOptions); noResponseItemCount++; batchCore.PatchItem(item2.id, patch, requestOptions); noResponseItemCount++; } int withBodyCount = 0; for (int i = 0; i < 5; i++) { ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId); batch.CreateItem <ToDoActivity>(item); withBodyCount++; batch.ReadItem(item.id); withBodyCount++; batchCore.PatchItem(item.id, patch); withBodyCount++; } response = await batch.ExecuteAsync(); Assert.AreEqual(noResponseItemCount + withBodyCount, response.Count); this.ValidateResponse(response, noResponseItemCount); }