Beispiel #1
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);
        }
Beispiel #2
0
        private async Task Validate(
            ItemRequestOptions requestOptions,
            Action <ItemResponse <ToDoActivity> > ValidateWrite,
            Action <ItemResponse <ToDoActivity> > ValidateRead)
        {
            ToDoActivity item = ToDoActivity.CreateRandomToDoActivity();
            ItemResponse <ToDoActivity> itemResponse = await this.container.CreateItemAsync(item, requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);
            ValidateWrite(itemResponse);

            itemResponse = await this.container.ReadItemAsync <ToDoActivity>(item.id, new PartitionKey(item.status), requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.OK, itemResponse.StatusCode);
            ValidateRead(itemResponse);

            item.cost    = 424242.42;
            itemResponse = await this.container.UpsertItemAsync <ToDoActivity>(item, requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.OK, itemResponse.StatusCode);
            ValidateWrite(itemResponse);

            item.cost    = 9000.42;
            itemResponse = await this.container.ReplaceItemAsync <ToDoActivity>(
                item,
                item.id,
                new PartitionKey(item.status),
                requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.OK, itemResponse.StatusCode);
            ValidateWrite(itemResponse);

            item.cost = 1000;
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.CreateReplaceOperation("/cost", item.cost)
            };

            itemResponse = await this.containerInternal.PatchItemAsync <ToDoActivity>(
                item.id,
                new PartitionKey(item.status),
                patchOperations : patch,
                requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.OK, itemResponse.StatusCode);
            ValidateWrite(itemResponse);

            itemResponse = await this.container.DeleteItemAsync <ToDoActivity>(
                item.id,
                new PartitionKey(item.status),
                requestOptions : requestOptions);

            Assert.AreEqual(HttpStatusCode.NoContent, itemResponse.StatusCode);
            this.ValidateItemNoContentResponse(itemResponse);
        }
        public async Task BatchCreateAndPatchAsync()
        {
            TestDoc testDoc = BatchTestBase.PopulateTestDoc(this.PartitionKey1);
            List <PatchOperation> patchOperations = new List <PatchOperation>()
            {
                PatchOperation.CreateReplaceOperation("/Cost", testDoc.Cost + 1)
            };

            BatchCore batch = (BatchCore) new BatchCore((ContainerInlineCore)BatchTestBase.JsonContainer, 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);
            testDoc.Cost = testDoc.Cost + 1;
            await BatchTestBase.VerifyByReadAsync(BatchTestBase.JsonContainer, testDoc, isStream : false, isSchematized : false, useEpk : false);
        }
Beispiel #4
0
        private async Task ValidateItemStream(
            ItemRequestOptions requestOptions,
            Action <ResponseMessage> ValidateWrite,
            Action <ResponseMessage> ValidateRead)
        {
            ToDoActivity item = ToDoActivity.CreateRandomToDoActivity();

            using (ResponseMessage responseMessage = await this.container.CreateItemStreamAsync(
                       TestCommon.SerializerCore.ToStream(item),
                       new PartitionKey(item.status),
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode);
                ValidateWrite(responseMessage);
            }

            using (ResponseMessage responseMessage = await this.container.ReadItemStreamAsync(
                       item.id,
                       new PartitionKey(item.status),
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
                ValidateRead(responseMessage);
            }

            item.cost = 424242.42;
            using (ResponseMessage responseMessage = await this.container.UpsertItemStreamAsync(
                       TestCommon.SerializerCore.ToStream(item),
                       new PartitionKey(item.status),
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
                ValidateWrite(responseMessage);
            }

            item.cost = 9000.42;
            using (ResponseMessage responseMessage = await this.container.ReplaceItemStreamAsync(
                       TestCommon.SerializerCore.ToStream(item),
                       item.id,
                       new PartitionKey(item.status),
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
                ValidateWrite(responseMessage);
            }

            item.cost = 1000;
            List <PatchOperation> patch = new List <PatchOperation>()
            {
                PatchOperation.CreateReplaceOperation("/cost", item.cost)
            };

            using (ResponseMessage responseMessage = await this.containerInternal.PatchItemStreamAsync(
                       item.id,
                       new PartitionKey(item.status),
                       patchOperations: patch,
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode);
                ValidateWrite(responseMessage);
            }

            using (ResponseMessage responseMessage = await this.container.DeleteItemStreamAsync(
                       item.id,
                       new PartitionKey(item.status),
                       requestOptions: requestOptions))
            {
                Assert.AreEqual(HttpStatusCode.NoContent, responseMessage.StatusCode);
                this.ValidateItemStreamNoContentResponse(responseMessage);
            }
        }
        public async Task PointOperationDiagnostic(bool disableDiagnostics)
        {
            ItemRequestOptions requestOptions = new ItemRequestOptions();

            if (disableDiagnostics)
            {
                requestOptions.DiagnosticContextFactory = () => EmptyCosmosDiagnosticsContext.Singleton;
            }
            else
            {
                // Add 10 seconds to ensure CPU history is recorded
                await Task.Delay(TimeSpan.FromSeconds(10));
            }

            //Checking point operation diagnostics on typed operations
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
            ItemResponse <ToDoActivity> createResponse = await this.Container.CreateItemAsync <ToDoActivity>(
                item : testItem,
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createResponse.Diagnostics,
                disableDiagnostics);

            ItemResponse <ToDoActivity> readResponse = await this.Container.ReadItemAsync <ToDoActivity>(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                readResponse.Diagnostics,
                disableDiagnostics);

            testItem.description = "NewDescription";
            ItemResponse <ToDoActivity> replaceResponse = await this.Container.ReplaceItemAsync <ToDoActivity>(
                item : testItem,
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            Assert.AreEqual(replaceResponse.Resource.description, "NewDescription");

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                replaceResponse.Diagnostics,
                disableDiagnostics);

            testItem.description = "PatchedDescription";
            ContainerInternal     containerInternal = (ContainerInternal)this.Container;
            List <PatchOperation> patch             = new List <PatchOperation>()
            {
                PatchOperation.CreateReplaceOperation("/description", testItem.description)
            };
            ItemResponse <ToDoActivity> patchResponse = await containerInternal.PatchItemAsync <ToDoActivity>(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                patchOperations : patch,
                requestOptions : requestOptions);

            Assert.AreEqual(patchResponse.Resource.description, "PatchedDescription");

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                patchResponse.Diagnostics,
                disableDiagnostics);

            ItemResponse <ToDoActivity> deleteResponse = await this.Container.DeleteItemAsync <ToDoActivity>(
                partitionKey : new Cosmos.PartitionKey(testItem.status),
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(deleteResponse);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                deleteResponse.Diagnostics,
                disableDiagnostics);

            //Checking point operation diagnostics on stream operations
            ResponseMessage createStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage readStreamResponse = await this.Container.ReadItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                readStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage replaceStreamResponse = await this.Container.ReplaceItemStreamAsync(
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                replaceStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage patchStreamResponse = await containerInternal.PatchItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                patchOperations : patch,
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                patchStreamResponse.Diagnostics,
                disableDiagnostics);

            ResponseMessage deleteStreamResponse = await this.Container.DeleteItemStreamAsync(
                id : testItem.id,
                partitionKey : new PartitionKey(testItem.status),
                requestOptions : requestOptions);

            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                deleteStreamResponse.Diagnostics,
                disableDiagnostics);

            // Ensure diagnostics are set even on failed operations
            testItem.description = new string('x', Microsoft.Azure.Documents.Constants.MaxResourceSizeInBytes + 1);
            ResponseMessage createTooBigStreamResponse = await this.Container.CreateItemStreamAsync(
                partitionKey : new PartitionKey(testItem.status),
                streamPayload : TestCommon.SerializerCore.ToStream <ToDoActivity>(testItem),
                requestOptions : requestOptions);

            Assert.IsFalse(createTooBigStreamResponse.IsSuccessStatusCode);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                createTooBigStreamResponse.Diagnostics,
                disableDiagnostics);
        }
        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);
        }