Skip to content

Commit

Permalink
Exposed underlying CosmosElements on item QueryResponse<T> using gene… (
Browse files Browse the repository at this point in the history
#1018)

* Exposed underlying CosmosElements on item QueryResponse<T> using generic type parameter.

* Applied PR comment:
- Switching to using 'as' rather than 'is' to save on reflection.

* Applying PR comment:
- Changing 'as' and 'null' check to inline 'is'.

* Reverted to a specific type check for CosmosElement.
- Trying to do the generic cast in all cases leads to CosmosElements being returned for QueryResponse<dynamic>.Resource and QueryResponse<object>.Resource, etc.
- This gives us the least change to the behavior for users that might do these sorts of casts.
  • Loading branch information
john-pao authored and sboshra committed Nov 20, 2019
1 parent dc9222f commit 7ae5c61
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 24 deletions.
19 changes: 13 additions & 6 deletions Microsoft.Azure.Cosmos/src/Query/v3Query/QueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,19 @@ public override IEnumerable<T> Resource
{
if (this.resources == null)
{
this.resources = CosmosElementSerializer.Deserialize<T>(
this.QueryHeaders.ContainerRid,
this.cosmosElements,
this.QueryHeaders.ResourceType,
this.jsonSerializer,
this.serializationOptions);
if (typeof(T) == typeof(CosmosElement))
{
this.resources = this.cosmosElements.Cast<T>();
}
else
{
this.resources = CosmosElementSerializer.Deserialize<T>(
this.QueryHeaders.ContainerRid,
this.cosmosElements,
this.QueryHeaders.ResourceType,
this.jsonSerializer,
this.serializationOptions);
}
}

return this.resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Azure.Cosmos.Tests
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Query;
using Microsoft.Azure.Cosmos.Query.Core.ExecutionComponent;
using Microsoft.Azure.Cosmos.Query.Core.ExecutionContext;
Expand Down Expand Up @@ -58,6 +59,84 @@ public void VerifyCosmosQueryResponseStream()
}
}

[TestMethod]
public void VerifyItemQueryResponseResult()
{
string contianerRid = "mockContainerRid";
(QueryResponseCore response, IList<ToDoItem> items) factoryResponse = QueryResponseMessageFactory.Create(
itemIdPrefix: $"TestPage",
continuationToken: "SomeContinuationToken",
collectionRid: contianerRid,
itemCount: 100);

QueryResponseCore responseCore = factoryResponse.response;
List<CosmosElement> cosmosElements = new List<CosmosElement>(responseCore.CosmosElements);

QueryResponse queryResponse = QueryResponse.CreateSuccess(
result: cosmosElements,
count: cosmosElements.Count,
responseLengthBytes: responseCore.ResponseLengthBytes,
responseHeaders: new CosmosQueryResponseMessageHeaders(
responseCore.ContinuationToken,
responseCore.DisallowContinuationTokenMessage,
ResourceType.Document,
contianerRid)
{
RequestCharge = responseCore.RequestCharge,
ActivityId = responseCore.ActivityId
},
diagnostics: null);

QueryResponse<ToDoItem> itemQueryResponse = QueryResponseMessageFactory.CreateQueryResponse<ToDoItem>(queryResponse);
List<ToDoItem> resultItems = new List<ToDoItem>(itemQueryResponse.Resource);
ToDoItemComparer comparer = new ToDoItemComparer();

Assert.AreEqual(factoryResponse.items.Count, resultItems.Count);
for (int i = 0; i < factoryResponse.items.Count; i++)
{
Assert.AreNotSame(factoryResponse.items[i], resultItems[i]);
Assert.AreEqual(0, comparer.Compare(factoryResponse.items[i], resultItems[i]));
}
}

[TestMethod]
public void VerifyItemQueryResponseCosmosElements()
{
string containerRid = "mockContainerRid";
(QueryResponseCore response, IList<ToDoItem> items) factoryResponse = QueryResponseMessageFactory.Create(
itemIdPrefix: $"TestPage",
continuationToken: "SomeContinuationToken",
collectionRid: containerRid,
itemCount: 100);

QueryResponseCore responseCore = factoryResponse.response;
List<CosmosElement> cosmosElements = new List<CosmosElement>(responseCore.CosmosElements);

QueryResponse queryResponse = QueryResponse.CreateSuccess(
result: cosmosElements,
count: cosmosElements.Count,
responseLengthBytes: responseCore.ResponseLengthBytes,
responseHeaders: new CosmosQueryResponseMessageHeaders(
responseCore.ContinuationToken,
responseCore.DisallowContinuationTokenMessage,
ResourceType.Document,
containerRid)
{
RequestCharge = responseCore.RequestCharge,
ActivityId = responseCore.ActivityId
},
diagnostics: null);

QueryResponse<CosmosElement> itemQueryResponse = QueryResponseMessageFactory.CreateQueryResponse<CosmosElement>(queryResponse);
List<CosmosElement> resultItems = new List<CosmosElement>(itemQueryResponse.Resource);

Assert.AreEqual(cosmosElements.Count, resultItems.Count);
for (int i = 0; i < cosmosElements.Count; i++)
{
Assert.AreSame(cosmosElements[i], resultItems[i]);
}
}

[TestMethod]
public async Task TestCosmosQueryExecutionComponentOnFailure()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public static QueryResponseCore CreateQueryResponse(
return message;
}

public static QueryResponse<TItem> CreateQueryResponse<TItem>(
QueryResponse queryResponse)
{
return QueryResponse<TItem>.CreateResponse<TItem>(queryResponse, cosmosSerializer);
}

public static QueryResponseCore CreateFailureResponse(
HttpStatusCode httpStatusCode,
SubStatusCodes subStatusCodes,
Expand Down
Loading

0 comments on commit 7ae5c61

Please sign in to comment.