private static async Task VerifyExceptionThrownOnExecuteAsync( TransactionalBatch batch, Type expectedTypeOfException, string expectedExceptionMessage = null, TransactionalBatchRequestOptions requestOptions = null) { bool wasExceptionThrown = false; try { if (requestOptions != null) { await batch.ExecuteAsync(requestOptions); } else { await batch.ExecuteAsync(); } } catch (Exception ex) { Assert.AreEqual(expectedTypeOfException, ex.GetType()); if (expectedExceptionMessage != null) { Assert.IsTrue(ex.Message.Contains(expectedExceptionMessage)); } wasExceptionThrown = true; } if (!wasExceptionThrown) { Assert.Fail("Exception was expected to be thrown but was not."); } }
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 override async Task <TransactionalBatchResponse> ExecuteAsync( TransactionalBatchRequestOptions requestOptions, CancellationToken cancellationToken = default) { CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(options: null); using (diagnosticsContext.CreateScope("TransactionalBatch.ExecuteAsync.WithRequestOptions")) { TransactionalBatchResponse response = await this.transactionalBatch.ExecuteAsync(requestOptions, cancellationToken); return(await this.DecryptTransactionalBatchResponseAsync( response, diagnosticsContext, cancellationToken)); } }
protected static TransactionalBatchRequestOptions GetUpdatedBatchRequestOptions( TransactionalBatchRequestOptions batchOptions = null, bool isSchematized = false, bool useEpk = false, object partitionKey = null) { if (isSchematized) { if (batchOptions == null) { batchOptions = new TransactionalBatchRequestOptions(); } Dictionary <string, object> properties = new Dictionary <string, object>() { { WFConstants.BackendHeaders.BinaryPassthroughRequest, bool.TrueString } }; if (batchOptions.Properties != null) { foreach (KeyValuePair <string, object> entry in batchOptions.Properties) { properties.Add(entry.Key, entry.Value); } } if (useEpk) { string epk = new Microsoft.Azure.Documents.PartitionKey(partitionKey) .InternalKey .GetEffectivePartitionKeyString(BatchTestBase.PartitionKeyDefinition); properties.Add(WFConstants.BackendHeaders.EffectivePartitionKeyString, epk); properties.Add(WFConstants.BackendHeaders.EffectivePartitionKey, BatchTestBase.HexStringToBytes(epk)); batchOptions.IsEffectivePartitionKeyRouting = true; } batchOptions.Properties = properties; } return(batchOptions); }
private async Task <TransactionalBatchResponse> RunCrudAsync(bool isStream, bool isSchematized, bool useEpk, Container container) { TransactionalBatchRequestOptions 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++; TestDoc testDocToPatch = this.GetTestDocCopy(this.TestDocPk1ExistingC); List <PatchOperation> patchOperations = new List <PatchOperation>() { PatchOperation.CreateReplaceOperation("/Cost", testDocToPatch.Cost + 1) }; // We run CRUD operations where all are expected to return HTTP 2xx. TransactionalBatchResponse batchResponse; BatchCore batch; if (!isStream) { batch = (BatchCore) new BatchCore((ContainerInlineCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1)) .CreateItem(testDocToCreate) .ReadItem(this.TestDocPk1ExistingC.Id) .ReplaceItem(testDocToReplace.Id, testDocToReplace) .UpsertItem(testDocToUpsert) .UpsertItem(anotherTestDocToUpsert) .DeleteItem(this.TestDocPk1ExistingD.Id); batch = (BatchCore)batch.PatchItem(testDocToPatch.Id, patchOperations); } else { batch = (BatchCore) new BatchCore((ContainerInlineCore)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: isStream ? 6 :7); 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); Assert.AreEqual(HttpStatusCode.OK, batchResponse[6].StatusCode); testDocToPatch.Cost = testDocToPatch.Cost + 1; await BatchTestBase.VerifyByReadAsync(container, testDocToPatch, isStream, isSchematized, useEpk); } 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 override Task <TransactionalBatchResponse> ExecuteAsync(TransactionalBatchRequestOptions requestOptions, CancellationToken cancellationToken = new CancellationToken()) => throw new NotImplementedException();
public override Task <TransactionalBatchResponse> ExecuteAsync(TransactionalBatchRequestOptions requestOptions, CancellationToken cancellationToken = new CancellationToken()) { throw new InvalidOperationException("Storage Session will execute the transaction"); }