public async Task BatchRequestSerializationFillAsync()
        {
            const int    maxBodySize       = 5 * 1024;
            const int    maxOperationCount = 10;
            const int    operationBodySize = 2 * 1024;
            const string partitionKey1     = "pk1";
            const string id = "random";

            ItemBatchOperation[] operations = new ItemBatchOperation[]
            {
                new ItemBatchOperation(
                    operationType: OperationType.Replace,
                    id: id,
                    operationIndex: 0)
                {
                    ResourceBody = Encoding.UTF8.GetBytes(new string('w', operationBodySize))
                },
                new ItemBatchOperation(
                    operationType: OperationType.Create,
                    operationIndex: 1)
                {
                    ResourceBody = Encoding.UTF8.GetBytes(new string('x', operationBodySize))
                },
                new ItemBatchOperation(
                    operationType: OperationType.Upsert,
                    operationIndex: 2)
                {
                    ResourceBody = Encoding.UTF8.GetBytes(new string('y', operationBodySize))
                },
                new ItemBatchOperation(
                    operationType: OperationType.Create,
                    operationIndex: 3)
                {
                    ResourceBody = Encoding.UTF8.GetBytes(new string('z', operationBodySize))
                }
            };
            ServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                new Cosmos.PartitionKey(partitionKey1),
                new ArraySegment <ItemBatchOperation>(operations),
                maxBodySize,
                maxOperationCount,
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : CancellationToken.None);

            Assert.AreEqual(2, batchRequest.Operations.Count);

            using (MemoryStream payload = batchRequest.TransferBodyStream())
            {
                Assert.IsNotNull(payload);

                List <ItemBatchOperation> readOperations = await new BatchRequestPayloadReader().ReadPayloadAsync(payload);
                Assert.AreEqual(2, readOperations.Count);

                ItemBatchOperationEqualityComparer comparer = new ItemBatchOperationEqualityComparer();
                Assert.IsTrue(comparer.Equals(operations[0], readOperations[0]));
                Assert.IsTrue(comparer.Equals(operations[1], readOperations[1]));
            }
        }
示例#2
0
        public async Task BatchRequestSerializationAsync()
        {
            const string partitionKey1 = "pk1";

            using CosmosClient cosmosClient = MockCosmosUtil.CreateMockCosmosClient();
            ContainerInternal containerCore = (ContainerInlineCore)cosmosClient.GetDatabase("db").GetContainer("cont");

            ItemBatchOperation[] operations = new ItemBatchOperation[]
            {
                new ItemBatchOperation(
                    operationType: OperationType.Create,
                    operationIndex: 0,
                    containerCore: containerCore)
                {
                    ResourceBody = new byte[] { 0x41, 0x42 }
                },
                new ItemBatchOperation(
                    id: "id2",
                    operationType: OperationType.Replace,
                    operationIndex: 1,
                    containerCore: containerCore,
                    requestOptions: new TransactionalBatchItemRequestOptions()
                {
                    IfMatchEtag = "theCondition"
                })
            };

            ServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                new Cosmos.PartitionKey(partitionKey1),
                new ArraySegment <ItemBatchOperation>(operations),
                serializerCore : MockCosmosUtil.Serializer,
                trace : NoOpTrace.Singleton,
                cancellationToken : CancellationToken.None);

            Assert.AreEqual(2, batchRequest.Operations.Count);

            using (MemoryStream payload = batchRequest.TransferBodyStream())
            {
                Assert.IsNotNull(payload);

                List <ItemBatchOperation> readOperations = await new BatchRequestPayloadReader().ReadPayloadAsync(payload);
                Assert.AreEqual(2, readOperations.Count);
                ItemBatchOperationEqualityComparer comparer = new ItemBatchOperationEqualityComparer();
                Assert.IsTrue(comparer.Equals(operations[0], readOperations[0]));
                Assert.IsTrue(comparer.Equals(operations[1], readOperations[1]));
            }
        }
        public async Task BatchRequestSerializationAsync()
        {
            const int    maxBodySize       = 5 * 1024;
            const int    maxOperationCount = 10;
            const string partitionKey1     = "pk1";

            ItemBatchOperation[] operations = new ItemBatchOperation[]
            {
                new ItemBatchOperation(
                    operationType: OperationType.Create,
                    operationIndex: 0)
                {
                    ResourceBody = new byte[] { 0x41, 0x42 }
                },
                new ItemBatchOperation(
                    id: "id2",
                    operationType: OperationType.Replace,
                    operationIndex: 1,
                    requestOptions: new BatchItemRequestOptions()
                {
                    IfMatchEtag = "theCondition"
                })
            };

            ServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                new Cosmos.PartitionKey(partitionKey1),
                new ArraySegment <ItemBatchOperation>(operations),
                maxBodySize,
                maxOperationCount,
                serializer : new CosmosJsonDotNetSerializer(),
                cancellationToken : CancellationToken.None);

            Assert.AreEqual(2, batchRequest.Operations.Count);

            using (MemoryStream payload = batchRequest.TransferBodyStream())
            {
                Assert.IsNotNull(payload);

                List <ItemBatchOperation> readOperations = await new BatchRequestPayloadReader().ReadPayloadAsync(payload);
                Assert.AreEqual(2, readOperations.Count);
                ItemBatchOperationEqualityComparer comparer = new ItemBatchOperationEqualityComparer();
                Assert.IsTrue(comparer.Equals(operations[0], readOperations[0]));
                Assert.IsTrue(comparer.Equals(operations[1], readOperations[1]));
            }
        }