public async Task AllowBatchingRequestsSendsToExecutor_PatchStream() { (ContainerInternal container, Mock <BatchAsyncContainerExecutor> mockedExecutor) = this.CreateMockBulkCosmosClientContext(); dynamic testItem = new { id = Guid.NewGuid().ToString(), pk = "FF627B77-568E-4541-A47E-041EAC10E46F", }; List <PatchOperation> patch = new List <PatchOperation>() { PatchOperation.CreateAddOperation("/new", "patched") }; ItemRequestOptions itemRequestOptions = new ItemRequestOptions(); Cosmos.PartitionKey partitionKey = new Cosmos.PartitionKey(testItem.pk); using (ResponseMessage streamResponse = await container.PatchItemStreamAsync( partitionKey: partitionKey, id: testItem.id, patchOperations: patch)) { mockedExecutor.Verify(c => c.AddAsync(It.IsAny <ItemBatchOperation>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>()), Times.Once); } }
public void ConstructPatchOperationTest() { PatchOperation operation = PatchOperation.CreateAddOperation(path, "string"); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, "string"); DateTime current = DateTime.UtcNow; operation = PatchOperation.CreateAddOperation(path, current); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, current); dynamic complexObject = new { a = "complex", b = 12.34, c = true }; operation = PatchOperation.CreateAddOperation(path, complexObject); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, complexObject); operation = PatchOperation.CreateRemoveOperation(path); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Remove, "value not required"); int[] arrayObject = { 1, 2, 3 }; operation = PatchOperation.CreateReplaceOperation(path, arrayObject); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Replace, arrayObject); Guid guid = new Guid(); operation = PatchOperation.CreateSetOperation(path, guid); PatchOperationTests.ValidateOperations(operation, PatchOperationType.Set, guid); }
public void ValidatePatchOperationSerialization() { int toCount = 0; int fromCount = 0; CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper( null, (input) => fromCount++, (input) => toCount++); CosmosSerializerCore serializerCore = new CosmosSerializerCore(serializerHelper); List <PatchOperation> patch = new List <PatchOperation>() { PatchOperation.CreateRemoveOperation("/removePath") }; Assert.AreEqual(0, toCount); // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize using (Stream stream = serializerCore.ToStream(patch)) { } Assert.AreEqual(0, toCount); patch.Add(PatchOperation.CreateAddOperation("/addPath", "addValue")); // custom serializer is used since there is Add operation type also using (Stream stream = serializerCore.ToStream(patch)) { } Assert.AreEqual(1, toCount); }
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 PatchItem_WithBulk() { List <MyDocument> createdDocuments = new List <MyDocument>(); // Create the items List <Task <ItemResponse <MyDocument> > > tasks = new List <Task <ItemResponse <MyDocument> > >(); for (int i = 0; i < 100; i++) { MyDocument createdDocument = CreateItem(i.ToString()); createdDocuments.Add(createdDocument); tasks.Add(ExecuteCreateAsync(this.container, createdDocument)); } await Task.WhenAll(tasks); List <PatchOperation> patch = new List <PatchOperation>() { PatchOperation.CreateAddOperation("/description", "patched") }; List <Task <ItemResponse <MyDocument> > > patchTasks = new List <Task <ItemResponse <MyDocument> > >(); // Patch the items foreach (MyDocument createdDocument in createdDocuments) { patchTasks.Add(ExecutePatchAsync((ContainerInternal)this.container, createdDocument, patch)); } await Task.WhenAll(patchTasks); for (int i = 0; i < 100; i++) { Task <ItemResponse <MyDocument> > task = patchTasks[i]; ItemResponse <MyDocument> result = await task; Assert.IsTrue(result.Headers.RequestCharge > 0); Assert.IsFalse(string.IsNullOrEmpty(result.Diagnostics.ToString())); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual("patched", result.Resource.Description); } }
public void ThrowsOnNullArguement() { try { PatchOperation.CreateAddOperation(null, "1"); Assert.Fail(); } catch (ArgumentNullException ex) { Assert.AreEqual(ex.ParamName, "path"); } try { PatchOperation.CreateRemoveOperation(null); Assert.Fail(); } catch (ArgumentNullException ex) { Assert.AreEqual(ex.ParamName, "path"); } }