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.CreateRemoveOperation("/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);
        }
示例#2
0
        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);
        }
示例#4
0
        public async Task ItemBulkNoResponseTest()
        {
            ItemRequestOptions requestOptions = new ItemRequestOptions()
            {
                EnableContentResponseOnWrite = false
            };

            CosmosClient          bulkClient            = TestCommon.CreateCosmosClient((builder) => builder.WithBulkExecution(true));
            Container             bulkContainer         = bulkClient.GetContainer(this.database.Id, this.container.Id);
            ContainerInternal     bulkContainerInternal = (ContainerInternal)bulkContainer;
            string                pkId  = "TestBulkId";
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.CreateRemoveOperation("/cost")
            };

            List <Task <ItemResponse <ToDoActivity> > > bulkOperations = new List <Task <ItemResponse <ToDoActivity> > >();
            List <ToDoActivity> items = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                items.Add(item);
                bulkOperations.Add(bulkContainer.CreateItemAsync <ToDoActivity>(item, requestOptions: requestOptions));
            }

            foreach (Task <ItemResponse <ToDoActivity> > result in bulkOperations)
            {
                ItemResponse <ToDoActivity> itemResponse = await result;
                this.ValidateItemNoContentResponse(itemResponse);
            }

            foreach (ToDoActivity item in items)
            {
                bulkOperations.Add(bulkContainerInternal.PatchItemAsync <ToDoActivity>(item.id, new PartitionKey(item.status), patch, requestOptions: requestOptions));
            }

            foreach (Task <ItemResponse <ToDoActivity> > result in bulkOperations)
            {
                ItemResponse <ToDoActivity> itemResponse = await result;
                this.ValidateItemNoContentResponse(itemResponse);
            }

            bulkOperations = new List <Task <ItemResponse <ToDoActivity> > >();
            foreach (ToDoActivity item in items)
            {
                bulkOperations.Add(bulkContainer.ReadItemAsync <ToDoActivity>(item.id, new PartitionKey(item.status), requestOptions: requestOptions));
            }

            foreach (Task <ItemResponse <ToDoActivity> > result in bulkOperations)
            {
                ItemResponse <ToDoActivity> itemResponse = await result;
                this.ValidateItemResponse(itemResponse);
            }
        }
示例#5
0
        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");
            }
        }
示例#6
0
        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);
        }