public async Task ReadReplaceThroughputResponseTests() { int toStreamCount = 0; int fromStreamCount = 0; CosmosSerializerHelper mockJsonSerializer = new CosmosSerializerHelper( null, (x) => fromStreamCount++, (x) => toStreamCount++); //Create a new cosmos client with the mocked cosmos json serializer CosmosClient client = TestCommon.CreateCosmosClient( (cosmosClientBuilder) => cosmosClientBuilder.WithCustomSerializer(mockJsonSerializer)); string databaseId = Guid.NewGuid().ToString(); int throughput = 10000; DatabaseResponse createResponse = await client.CreateDatabaseAsync(databaseId, throughput, null); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); Cosmos.Database cosmosDatabase = createResponse; ThroughputResponse readThroughputResponse = await cosmosDatabase.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(readThroughputResponse); Assert.IsNotNull(readThroughputResponse.Resource); Assert.IsNotNull(readThroughputResponse.MinThroughput); Assert.IsNotNull(readThroughputResponse.Resource.Throughput); Assert.AreEqual(throughput, readThroughputResponse.Resource.Throughput.Value); // Implicit ThroughputProperties throughputProperties = await cosmosDatabase.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(throughputProperties); Assert.AreEqual(throughput, throughputProperties.Throughput); // Simple API int?readThroughput = await cosmosDatabase.ReadThroughputAsync(); Assert.AreEqual(throughput, readThroughput); // Database must have a container before it can be scaled string containerId = Guid.NewGuid().ToString(); string partitionPath = "/users"; ContainerResponse containerResponse = await cosmosDatabase.CreateContainerAsync(containerId, partitionPath, throughput : null); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); ThroughputResponse replaceThroughputResponse = await cosmosDatabase.ReplaceThroughputAsync(readThroughputResponse.Resource.Throughput.Value + 1000); Assert.IsNotNull(replaceThroughputResponse); Assert.IsNotNull(replaceThroughputResponse.Resource); Assert.AreEqual(readThroughputResponse.Resource.Throughput.Value + 1000, replaceThroughputResponse.Resource.Throughput.Value); await cosmosDatabase.DeleteAsync(); Database databaseNoThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), throughput : null); try { ThroughputResponse throughputResponse = await databaseNoThroughput.ReadThroughputAsync(new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } try { ThroughputResponse throughputResponse = await databaseNoThroughput.ReplaceThroughputAsync(2000, new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } int?dbThroughput = await databaseNoThroughput.ReadThroughputAsync(); Assert.IsNull(dbThroughput); Assert.AreEqual(0, toStreamCount, "Custom serializer to stream should not be used for offer operations"); Assert.AreEqual(0, fromStreamCount, "Custom serializer from stream should not be used for offer operations"); await databaseNoThroughput.DeleteAsync(); }
public async Task ReadReplaceThroughputResponseTests() { int toStreamCount = 0; int fromStreamCount = 0; CosmosSerializerHelper mockJsonSerializer = new CosmosSerializerHelper( null, (x) => fromStreamCount++, (x) => toStreamCount++); //Create a new cosmos client with the mocked cosmos json serializer CosmosClient client = TestCommon.CreateCosmosClient( (cosmosClientBuilder) => cosmosClientBuilder.WithCustomSerializer(mockJsonSerializer)); int databaseThroughput = 10000; Cosmos.Database databaseNoThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), null); Cosmos.Database databaseWithThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), databaseThroughput, null); string containerId = Guid.NewGuid().ToString(); string partitionPath = "/users"; Container containerNoThroughput = await databaseWithThroughput.CreateContainerAsync(containerId, partitionPath, throughput : null); try { await containerNoThroughput.ReadThroughputAsync(new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } try { await containerNoThroughput.ReplaceThroughputAsync(2000, new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } int containerThroughput = 1000; Container container = await databaseNoThroughput.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", throughput : containerThroughput); int?containerResponseThroughput = await container.ReadThroughputAsync(); Assert.AreEqual(containerThroughput, containerResponseThroughput); ThroughputResponse containerThroughputResponse = await container.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(containerThroughputResponse); Assert.IsNotNull(containerThroughputResponse.Resource); Assert.IsNotNull(containerThroughputResponse.MinThroughput); Assert.IsNotNull(containerThroughputResponse.Resource.Throughput); Assert.AreEqual(containerThroughput, containerThroughputResponse.Resource.Throughput.Value); containerThroughput += 500; containerThroughputResponse = await container.ReplaceThroughputAsync(containerThroughput, new RequestOptions()); Assert.IsNotNull(containerThroughputResponse); Assert.IsNotNull(containerThroughputResponse.Resource); Assert.IsNotNull(containerThroughputResponse.Resource.Throughput); Assert.AreEqual(containerThroughput, containerThroughputResponse.Resource.Throughput.Value); Assert.AreEqual(0, toStreamCount, "Custom serializer to stream should not be used for offer operations"); Assert.AreEqual(0, fromStreamCount, "Custom serializer from stream should not be used for offer operations"); await databaseNoThroughput.DeleteAsync(); }
public async Task TestQueryWithCustomJsonSerializer() { int toStreamCount = 0; int fromStreamCount = 0; CosmosSerializer serializer = new CosmosSerializerHelper(new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }, (item) => fromStreamCount++, (item) => toStreamCount++); CosmosClient client = TestCommon.CreateCosmosClient(builder => builder.WithCustomSerializer(serializer)); Database database = await client.CreateDatabaseAsync(Guid.NewGuid().ToString()); try { Container container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id"); Assert.AreEqual(0, toStreamCount); Assert.AreEqual(0, fromStreamCount); double cost = 9001.42; for (int i = 0; i < 5; i++) { ToDoActivity toDoActivity = new ToDoActivity() { id = "TestId" + i, cost = cost }; await container.CreateItemAsync <ToDoActivity>(toDoActivity, new PartitionKey(toDoActivity.id)); } Assert.AreEqual(5, toStreamCount); Assert.AreEqual(5, fromStreamCount); toStreamCount = 0; fromStreamCount = 0; QueryDefinition query = new QueryDefinition("select * from T where T.id != @id"). WithParameter("@id", Guid.NewGuid()); FeedIterator <DatabaseProperties> feedIterator = client.GetDatabaseQueryIterator <DatabaseProperties>( query); List <DatabaseProperties> databases = new List <DatabaseProperties>(); while (feedIterator.HasMoreResults) { databases.AddRange(await feedIterator.ReadNextAsync()); } Assert.AreEqual(1, toStreamCount, "parameter should use custom serializer"); Assert.AreEqual(0, fromStreamCount); toStreamCount = 0; fromStreamCount = 0; FeedIterator <ToDoActivity> itemIterator = container.GetItemQueryIterator <ToDoActivity>( query); List <ToDoActivity> items = new List <ToDoActivity>(); while (itemIterator.HasMoreResults) { items.AddRange(await itemIterator.ReadNextAsync()); } Assert.AreEqual(2, toStreamCount); Assert.AreEqual(1, fromStreamCount); toStreamCount = 0; fromStreamCount = 0; // Verify that the custom serializer is actually being used via stream FeedIterator itemStreamIterator = container.GetItemQueryStreamIterator( query); while (itemStreamIterator.HasMoreResults) { ResponseMessage response = await itemStreamIterator.ReadNextAsync(); using (StreamReader reader = new StreamReader(response.Content)) { string content = await reader.ReadToEndAsync(); Assert.IsTrue(content.Contains("9001.42")); Assert.IsFalse(content.Contains("description"), "Description should be ignored and not in the JSON"); } } Assert.AreEqual(2, toStreamCount); Assert.AreEqual(0, fromStreamCount); } finally { if (database != null) { using (await database.DeleteStreamAsync()) { } } } }