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.Remove("/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.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(patch)) { }
            Assert.AreEqual(1, toCount);
        }
        public void ConstructPatchOperationTest()
        {
            PatchOperation operation = PatchOperation.Add(path, "string");

            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, "string");

            DateTime current = DateTime.UtcNow;

            operation = PatchOperation.Add(path, current);
            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, current);

            dynamic complexObject = new { a = "complex", b = 12.34, c = true };

            operation = PatchOperation.Add(path, complexObject);
            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Add, complexObject);

            operation = PatchOperation.Remove(path);
            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Remove, "value not required");

            int[] arrayObject = { 1, 2, 3 };
            operation = PatchOperation.Replace(path, arrayObject);
            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Replace, arrayObject);

            Guid guid = new Guid();

            operation = PatchOperation.Set(path, guid);
            PatchOperationTests.ValidateOperations(operation, PatchOperationType.Set, guid);
        }
        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);
        }
Example #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.Remove("/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);
            }

            PatchItemRequestOptions patchRequestOptions = new PatchItemRequestOptions()
            {
                EnableContentResponseOnWrite = false
            };

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

            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.pk), requestOptions: requestOptions));
            }

            foreach (Task <ItemResponse <ToDoActivity> > result in bulkOperations)
            {
                ItemResponse <ToDoActivity> itemResponse = await result;
                this.ValidateItemResponse(itemResponse);
            }
        }
        public void ThrowsOnNullArguement()
        {
            try
            {
                PatchOperation.Add(null, "1");
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual(ex.ParamName, "path");
            }

            try
            {
                PatchOperation.Remove(null);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual(ex.ParamName, "path");
            }
        }
        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.Remove("/removePath")
            };

            Assert.AreEqual(0, toCount);

            PatchItemRequestOptions patchRequestOptions = new PatchItemRequestOptions();

            // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);

            patch.Add(PatchOperation.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(1, toCount);

            patch.Clear();
            toCount = 0;
            patch.Add(PatchOperation.Add("/addPath", new CosmosJsonDotNetSerializer().ToStream("addValue")));
            // custom serializer is not used since the input value is of type stream
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);
        }
 public static PatchOperation Remove(string path) => PatchOperation.Remove(JsonPointer.Parse(path));
        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.Remove("/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);
        }