コード例 #1
0
        public async Task TestCustomJsonSerializer()
        {
            int toStreamCount   = 0;
            int fromStreamCount = 0;

            Mock <CosmosJsonSerializer> mockJsonSerializer = new Mock <CosmosJsonSerializer>();

            //The item object will be serialized with the custom json serializer.
            ToDoActivity testItem = CreateRandomToDoActivity();

            mockJsonSerializer.Setup(x => x.ToStream <ToDoActivity>(It.IsAny <ToDoActivity>())).Callback(() => toStreamCount++).Returns(this.jsonSerializer.ToStream <ToDoActivity>(testItem));
            mockJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(It.IsAny <Stream>())).Callback <Stream>(x => { x.Dispose(); fromStreamCount++; }).Returns(testItem);

            //Create a new cosmos client with the mocked cosmos json serializer
            CosmosConfiguration configuration = TestCommon.GetDefaultConfiguration();

            configuration.UseCustomJsonSerializer(mockJsonSerializer.Object);
            CosmosClient    mockClient    = TestCommon.CreateCosmosClient(configuration);
            CosmosContainer mockContainer = mockClient.Databases[this.database.Id].Containers[this.container.Id];

            //Validate that the custom json serializer is used for creating the item
            CosmosItemResponse <ToDoActivity> response = await mockContainer.Items.CreateItemAsync <ToDoActivity>(partitionKey : testItem.status, item : testItem);

            Assert.IsNotNull(response);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);

            Assert.AreEqual(1, toStreamCount);
            Assert.AreEqual(1, fromStreamCount);

            await mockContainer.Items.DeleteItemAsync <ToDoActivity>(testItem.status, testItem.id);
        }