Esempio n. 1
0
        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.Add("/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);
            }
        }
Esempio n. 2
0
        public async Task PointSuccessOperationsTest(ConnectionMode mode)
        {
            Container container = await this.GetContainer(mode);

            // Create an item
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity("MyTestPkValue");
            ItemResponse <ToDoActivity> createResponse = await container.CreateItemAsync <ToDoActivity>(testItem);

            ToDoActivity testItemCreated = createResponse.Resource;

            // Read an Item
            await container.ReadItemAsync <ToDoActivity>(testItem.id, new Cosmos.PartitionKey(testItem.id));

            // Upsert an Item
            await container.UpsertItemAsync <ToDoActivity>(testItem);

            // Replace an Item
            await container.ReplaceItemAsync <ToDoActivity>(testItemCreated, testItemCreated.id.ToString());

            // Patch an Item
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/new", "patched")
            };

            await((ContainerInternal)container).PatchItemAsync <ToDoActivity>(
                testItem.id,
                new Cosmos.PartitionKey(testItem.id),
                patch);

            // Delete an Item
            await container.DeleteItemAsync <ToDoActivity>(testItem.id, new Cosmos.PartitionKey(testItem.id));

            await this.WaitAndAssert(12);
        }
        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);
        }
Esempio n. 4
0
        public async Task StreamOperationsTest(ConnectionMode mode)
        {
            Container container = await this.GetContainer(mode);

            // Create an item
            var testItem = new { id = "MyTestItemId", partitionKeyPath = "MyTestPkValue", details = "it's working", status = "done" };
            await container
            .CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem),
                                   new Cosmos.PartitionKey(testItem.id));

            //Upsert an Item
            await container.UpsertItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem), new Cosmos.PartitionKey(testItem.id));

            //Read an Item
            await container.ReadItemStreamAsync(testItem.id, new Cosmos.PartitionKey(testItem.id));

            //Replace an Item
            await container.ReplaceItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem), testItem.id, new Cosmos.PartitionKey(testItem.id));

            // Patch an Item
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/new", "patched")
            };

            await((ContainerInternal)container).PatchItemStreamAsync(
                partitionKey: new Cosmos.PartitionKey(testItem.id),
                id: testItem.id,
                patchOperations: patch);

            //Delete an Item
            await container.DeleteItemStreamAsync(testItem.id, new Cosmos.PartitionKey(testItem.id));

            await this.WaitAndAssert(12);
        }
        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 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.Add("/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()));
        }
Esempio n. 7
0
        public async Task StreamOperationsTest(ConnectionMode mode)
        {
            Container container = await this.CreateClientAndContainer(mode);

            // Create an item
            var testItem = new { id = "MyTestItemId", partitionKeyPath = "MyTestPkValue", details = "it's working", status = "done" };
            await container
            .CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem),
                                   new Cosmos.PartitionKey(testItem.id));

            //Upsert an Item
            await container.UpsertItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem), new Cosmos.PartitionKey(testItem.id));

            //Read an Item
            await container.ReadItemStreamAsync(testItem.id, new Cosmos.PartitionKey(testItem.id));

            //Replace an Item
            await container.ReplaceItemStreamAsync(TestCommon.SerializerCore.ToStream(testItem), testItem.id, new Cosmos.PartitionKey(testItem.id));

            // Patch an Item
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/new", "patched")
            };

            await((ContainerInternal)container).PatchItemStreamAsync(
                partitionKey: new Cosmos.PartitionKey(testItem.id),
                id: testItem.id,
                patchOperations: patch);

            //Delete an Item
            await container.DeleteItemStreamAsync(testItem.id, new Cosmos.PartitionKey(testItem.id));

            IDictionary <string, long> expectedRecordCountInOperation = new Dictionary <string, long>
            {
                { Documents.OperationType.Create.ToString(), 1 },
                { Documents.OperationType.Upsert.ToString(), 1 },
                { Documents.OperationType.Read.ToString(), 1 },
                { Documents.OperationType.Replace.ToString(), 1 },
                { Documents.OperationType.Patch.ToString(), 1 },
                { Documents.OperationType.Delete.ToString(), 1 }
            };

            await this.WaitAndAssert(expectedOperationCount : 12,
                                     expectedOperationRecordCountMap : expectedRecordCountInOperation);
        }
Esempio n. 8
0
        public async Task PointSuccessOperationsTest(ConnectionMode mode)
        {
            Container container = await this.CreateClientAndContainer(mode);

            // Create an item
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity("MyTestPkValue");
            ItemResponse <ToDoActivity> createResponse = await container.CreateItemAsync <ToDoActivity>(testItem);

            ToDoActivity testItemCreated = createResponse.Resource;

            // Read an Item
            await container.ReadItemAsync <ToDoActivity>(testItem.id, new Cosmos.PartitionKey(testItem.id));

            // Upsert an Item
            await container.UpsertItemAsync <ToDoActivity>(testItem);

            // Replace an Item
            await container.ReplaceItemAsync <ToDoActivity>(testItemCreated, testItemCreated.id.ToString());

            // Patch an Item
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/new", "patched")
            };

            await((ContainerInternal)container).PatchItemAsync <ToDoActivity>(
                testItem.id,
                new Cosmos.PartitionKey(testItem.id),
                patch);

            // Delete an Item
            await container.DeleteItemAsync <ToDoActivity>(testItem.id, new Cosmos.PartitionKey(testItem.id));

            IDictionary <string, long> expectedRecordCountInOperation = new Dictionary <string, long>
            {
                { Documents.OperationType.Create.ToString(), 1 },
                { Documents.OperationType.Upsert.ToString(), 1 },
                { Documents.OperationType.Read.ToString(), 1 },
                { Documents.OperationType.Replace.ToString(), 1 },
                { Documents.OperationType.Patch.ToString(), 1 },
                { Documents.OperationType.Delete.ToString(), 1 }
            };

            await this.WaitAndAssert(expectedOperationCount : 12,
                                     expectedOperationRecordCountMap : expectedRecordCountInOperation);
        }
Esempio n. 9
0
        public async Task PatchItem_WithBulk()
        {
            List <ToDoActivity> createdDocuments = new List <ToDoActivity>();
            // Create the items
            List <Task <ItemResponse <ToDoActivity> > > tasks = new List <Task <ItemResponse <ToDoActivity> > >();

            for (int i = 0; i < 100; i++)
            {
                ToDoActivity createdDocument = CosmosItemBulkTests.CreateItem(i.ToString());
                createdDocuments.Add(createdDocument);
                tasks.Add(CosmosItemBulkTests.ExecuteCreateAsync(this.container, createdDocument));
            }

            await Task.WhenAll(tasks);

            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/description", "patched")
            };
            List <Task <ItemResponse <ToDoActivity> > > patchTasks = new List <Task <ItemResponse <ToDoActivity> > >();

            // Patch the items
            foreach (ToDoActivity createdDocument in createdDocuments)
            {
                patchTasks.Add(CosmosItemBulkTests.ExecutePatchAsync((ContainerInternal)this.container, createdDocument, patch));
            }

            await Task.WhenAll(patchTasks);

            for (int i = 0; i < 100; i++)
            {
                Task <ItemResponse <ToDoActivity> > task   = patchTasks[i];
                ItemResponse <ToDoActivity>         result = await task;
                Assert.IsTrue(result.Headers.RequestCharge > 0);
                Assert.IsNotNull(result.Headers.Session);
                Assert.IsNotNull(result.Headers.ActivityId);
                Assert.IsFalse(string.IsNullOrEmpty(result.Diagnostics.ToString()));
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
                Assert.AreEqual("patched", result.Resource.description);
            }
        }
        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 async Task PatchItemStream_WithBulk()
        {
            List <MyDocument> createdDocuments = new List <MyDocument>();
            // Create the items
            List <Task <ResponseMessage> > tasks = new List <Task <ResponseMessage> >();

            for (int i = 0; i < 100; i++)
            {
                MyDocument createdDocument = CreateItem(i.ToString());
                createdDocuments.Add(createdDocument);
                tasks.Add(ExecuteCreateStreamAsync(this.container, createdDocument));
            }

            await Task.WhenAll(tasks);

            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.Add("/description", "patched")
            };
            List <Task <ResponseMessage> > PatchTasks = new List <Task <ResponseMessage> >();

            // Patch the items
            foreach (MyDocument createdDocument in createdDocuments)
            {
                PatchTasks.Add(ExecutePatchStreamAsync((ContainerInternal)this.container, createdDocument, patch));
            }

            await Task.WhenAll(PatchTasks);

            for (int i = 0; i < 100; i++)
            {
                Task <ResponseMessage> task   = PatchTasks[i];
                ResponseMessage        result = await task;
                Assert.IsTrue(result.Headers.RequestCharge > 0);
                Assert.IsFalse(string.IsNullOrEmpty(result.Diagnostics.ToString()));
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            }
        }
        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 Add(string path, JsonElementProxy value) => PatchOperation.Add(JsonPointer.Parse(path), value);