Skip to content

Commit 93eeb53

Browse files
author
leminh98
committed
Add unit test for GetIndexMetrics
1 parent dd98f8d commit 93eeb53

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

Microsoft.Azure.Cosmos/src/Linq/CosmosLinqExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,8 @@ public static FeedIterator ToStreamIterator<T>(this IQueryable<T> query)
591591
}
592592

593593
/// <summary>
594-
/// This extension method returns the Index Metrics for a given Response object. The index utilization metrics is to be used for debugging purposes.
595-
/// It's applicable to query response only.
596-
/// This result is only available if QueryRequestOptions.PopulateIndexMetrics is set to true.
594+
/// This extension method returns the Index Metrics for a given Response object. The index utilization metrics is to be used for debugging purposes only.
595+
/// This result is only available if QueryRequestOptions.PopulateIndexMetrics is set to true.Returns null if the index metrics is not available in the response.
597596
/// </summary>
598597
/// <param name="response">The query Response.</param>
599598
/// <returns>A string represents the Index Metrics.</returns>
@@ -609,7 +608,7 @@ public static FeedIterator ToStreamIterator<T>(this IQueryable<T> query)
609608
/// </example>
610609
public static string GetIndexMetrics<T>(this Response<T> response)
611610
{
612-
return ResponseMessage.DecodeIndexMetrics(response.Headers, isBase64Encoded: false).Value;
611+
return ResponseMessage.DecodeIndexMetrics(response.Headers, isBase64Encoded: false)?.Value;
613612
}
614613

615614
/// <summary>

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Linq/CosmosItemLinqTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,34 @@ public async Task QueryableExtentionFunctionsTest()
396396
Assert.AreEqual(100, maxTaskNum);
397397
}
398398

399+
400+
[TestMethod]
401+
public async Task GetIndexMetricsTest()
402+
{
403+
//Creating items for query.
404+
IList<ToDoActivity> itemList = await ToDoActivity.CreateRandomItems(container: this.Container, pkCount: 10, perPKItemCount: 1, randomPartitionKey: true);
405+
406+
QueryRequestOptions queryRequestOptions = new QueryRequestOptions() { PopulateIndexMetrics = true };
407+
IOrderedQueryable<ToDoActivity> linqQueryable = this.Container.GetItemLinqQueryable<ToDoActivity>(
408+
requestOptions: queryRequestOptions);
409+
410+
// Response object with valid index metrics field
411+
Response<int> response = await linqQueryable.Select(item => item.taskNum).SumAsync();
412+
this.VerifyResponse(response, 420, queryRequestOptions);
413+
414+
string indexMetrics = response.GetIndexMetrics();
415+
Assert.AreEqual(
416+
@"{""UtilizedIndexes"":{""SingleIndexes"":[{""IndexSpec"":""/taskNum/?""}],""CompositeIndexes"":[]},""PotentialIndexes"":{""SingleIndexes"":[],""CompositeIndexes"":[]}}",
417+
indexMetrics);
418+
419+
// Response object with null index metrics field]
420+
response.Headers.IndexUtilizationText = null;
421+
indexMetrics = response.GetIndexMetrics();
422+
Assert.AreEqual(
423+
null,
424+
indexMetrics);
425+
}
426+
399427
[DataRow(false)]
400428
[DataRow(true)]
401429
[TestMethod]

0 commit comments

Comments
 (0)