public async Task BatchSingleServerResponseAsync()
        {
            List <BatchOperationResult> expectedResults = new List <BatchOperationResult>();
            CosmosJsonDotNetSerializer  jsonSerializer  = new CosmosJsonDotNetSerializer();
            TestItem testItem = new TestItem("tst");

            Stream       itemStream     = jsonSerializer.ToStream <TestItem>(testItem);
            MemoryStream resourceStream = itemStream as MemoryStream;

            if (resourceStream == null)
            {
                await itemStream.CopyToAsync(resourceStream);

                resourceStream.Position = 0;
            }

            expectedResults.Add(
                new BatchOperationResult(HttpStatusCode.OK)
            {
                ETag           = "theETag",
                SubStatusCode  = (SubStatusCodes)1100,
                ResourceStream = resourceStream
            });
            expectedResults.Add(new BatchOperationResult(HttpStatusCode.Conflict));

            double requestCharge = 3.6;

            TestHandler testHandler = new TestHandler(async(request, cancellationToken) =>
            {
                ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK, requestMessage: null, errorMessage: null)
                {
                    Content = await new BatchResponsePayloadWriter(expectedResults).GeneratePayloadAsync()
                };

                responseMessage.Headers.RequestCharge = requestCharge;
                return(responseMessage);
            });

            Container container = BatchUnitTests.GetContainer(testHandler);

            BatchResponse batchResponse = await new BatchCore((ContainerCore)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1))
                                          .ReadItem("id1")
                                          .ReadItem("id2")
                                          .ExecuteAsync();

            Assert.AreEqual(HttpStatusCode.OK, batchResponse.StatusCode);
            Assert.AreEqual(requestCharge, batchResponse.RequestCharge);

            BatchOperationResult <TestItem> result0 = batchResponse.GetOperationResultAtIndex <TestItem>(0);

            Assert.AreEqual(expectedResults[0].StatusCode, result0.StatusCode);
            Assert.AreEqual(expectedResults[0].SubStatusCode, result0.SubStatusCode);
            Assert.AreEqual(expectedResults[0].ETag, result0.ETag);
            Assert.AreEqual(testItem, result0.Resource);

            Assert.AreEqual(expectedResults[1].StatusCode, batchResponse[1].StatusCode);
            Assert.AreEqual(SubStatusCodes.Unknown, batchResponse[1].SubStatusCode);
            Assert.IsNull(batchResponse[1].ETag);
            Assert.IsNull(batchResponse[1].ResourceStream);
        }
Exemplo n.º 2
0
        public async Task BatchReadsOnlyAsync()
        {
            Container container = BatchTestBase.JsonContainer;

            await this.CreateJsonTestDocsAsync(container);

            BatchResponse batchResponse = await new BatchCore((ContainerCore)container, BatchTestBase.GetPartitionKey(this.PartitionKey1))
                                          .ReadItem(this.TestDocPk1ExistingA.Id)
                                          .ReadItem(this.TestDocPk1ExistingB.Id)
                                          .ReadItem(this.TestDocPk1ExistingC.Id)
                                          .ExecuteAsync();

            BatchSinglePartitionKeyTests.VerifyBatchProcessed(batchResponse, numberOfOperations: 3);

            Assert.AreEqual(HttpStatusCode.OK, batchResponse[0].StatusCode);
            Assert.AreEqual(HttpStatusCode.OK, batchResponse[1].StatusCode);
            Assert.AreEqual(HttpStatusCode.OK, batchResponse[2].StatusCode);

            Assert.AreEqual(this.TestDocPk1ExistingA, batchResponse.GetOperationResultAtIndex <TestDoc>(0).Resource);
            Assert.AreEqual(this.TestDocPk1ExistingB, batchResponse.GetOperationResultAtIndex <TestDoc>(1).Resource);
            Assert.AreEqual(this.TestDocPk1ExistingC, batchResponse.GetOperationResultAtIndex <TestDoc>(2).Resource);
        }