Skip to content

Commit

Permalink
Add tests for multiple continuation tokens and multi item order by to…
Browse files Browse the repository at this point in the history
…kens
  • Loading branch information
neildsh committed Apr 30, 2021
1 parent 52db6b7 commit fdcb62f
Showing 1 changed file with 117 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ namespace Microsoft.Azure.Cosmos.Tests.Query.Pipeline
[TestClass]
public class OrderByCrossPartitionQueryPipelineStageTests
{
private static IReadOnlyList<(string serializedToken, CosmosElement element)> TokenTestData()
{
Guid guid = Guid.Parse("69D5AB17-C94A-4173-A278-B59D0D9C7C37");
byte[] randomBytes = guid.ToByteArray();
string hexString = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length);

return new List<(string, CosmosElement)>
{
("[42, 37]", CosmosArray.Parse("[42, 37]")),
($@"{{C_Binary(""0x{hexString}"")}}", CosmosBinary.Create(new ReadOnlyMemory<byte>(randomBytes))),
("false", CosmosBoolean.Create(false)),
($@"{{C_Guid(""{guid}"")}}", CosmosGuid.Create(guid)),
("null", CosmosNull.Create()),
("1", CosmosInt64.Create(1)),
("{\"foo\": false}", CosmosObject.Parse("{\"foo\": false}")),
("asdf", CosmosString.Create("asdf"))
};
}

[TestMethod]
public void MonadicCreate_NullContinuationToken()
{
Expand Down Expand Up @@ -119,21 +138,9 @@ public void MonadicCreate_NonParallelContinuationToken()
public void MonadicCreate_SingleOrderByContinuationToken()
{
Mock<IDocumentContainer> mockDocumentContainer = new Mock<IDocumentContainer>();
Guid guid = Guid.Parse("69D5AB17-C94A-4173-A278-B59D0D9C7C37");
byte[] randomBytes = guid.ToByteArray();
string hexString = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length);
IReadOnlyList<(string serializedToken, CosmosElement element)> tokens = TokenTestData();

foreach ((string serializedToken, CosmosElement element) in new (string, CosmosElement)[]
{
("[42, 37]", CosmosArray.Parse("[42, 37]")),
($@"{{C_Binary(""0x{hexString}"")}}", CosmosBinary.Create(new ReadOnlyMemory<byte>(randomBytes))),
("false", CosmosBoolean.Create(false)),
($@"{{C_Guid(""{guid.ToString()}"")}}", CosmosGuid.Create(guid)),
("null", CosmosNull.Create()),
("1", CosmosInt64.Create(1)),
("{\"foo\": false}", CosmosObject.Parse("{\"foo\": false}")),
("asdf", CosmosString.Create("asdf"))
})
foreach ((string serializedToken, CosmosElement element) in tokens)
{
ParallelContinuationToken parallelContinuationToken = new ParallelContinuationToken(
token: serializedToken,
Expand Down Expand Up @@ -165,58 +172,110 @@ public void MonadicCreate_SingleOrderByContinuationToken()
}));
Assert.IsTrue(monadicCreate.Succeeded);
}

foreach ((string token1, CosmosElement element1) in tokens)
{
foreach ((string token2, CosmosElement element2) in tokens)
{
ParallelContinuationToken parallelContinuationToken1 = new ParallelContinuationToken(
token: $"[{token1}, {token2}]",
range: new Documents.Routing.Range<string>("A", "B", true, false));

OrderByContinuationToken orderByContinuationToken = new OrderByContinuationToken(
parallelContinuationToken1,
new List<OrderByItem>()
{
new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>(){ { "item1", element1 } })),
new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>(){ { "item2", element2 } }))
},
rid: "rid",
skipCount: 42,
filter: "filter");

TryCatch<IQueryPipelineStage> monadicCreate = OrderByCrossPartitionQueryPipelineStage.MonadicCreate(
documentContainer: mockDocumentContainer.Object,
sqlQuerySpec: new SqlQuerySpec("SELECT * FROM c ORDER BY c.item1, c.item2"),
targetRanges: new List<FeedRangeEpk>()
{
new FeedRangeEpk(new Range<string>(min: "A", max: "B", isMinInclusive: true, isMaxInclusive: false)),
new FeedRangeEpk(new Range<string>(min: "B", max: "C", isMinInclusive: true, isMaxInclusive: false)),
},
partitionKey: null,
orderByColumns: new List<OrderByColumn>()
{
new OrderByColumn("item1", SortOrder.Ascending),
new OrderByColumn("item2", SortOrder.Ascending)
},
queryPaginationOptions: new QueryPaginationOptions(pageSizeHint: 10),
maxConcurrency: 10,
cancellationToken: default,
continuationToken: CosmosArray.Create(
new List<CosmosElement>()
{
OrderByContinuationToken.ToCosmosElement(orderByContinuationToken)
}));
Assert.IsTrue(monadicCreate.Succeeded);
}
}
}

[TestMethod]
public void MonadicCreate_MultipleOrderByContinuationToken()
{
Mock<IDocumentContainer> mockDocumentContainer = new Mock<IDocumentContainer>();
IReadOnlyList<(string serializedToken, CosmosElement element)> tokens = TokenTestData();

ParallelContinuationToken parallelContinuationToken1 = new ParallelContinuationToken(
token: "asdf",
range: new Documents.Routing.Range<string>("A", "B", true, false));

OrderByContinuationToken orderByContinuationToken1 = new OrderByContinuationToken(
parallelContinuationToken1,
new List<OrderByItem>() { new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>() { { "item", CosmosString.Create("asdf") } })) },
rid: "rid",
skipCount: 42,
filter: "filter");

ParallelContinuationToken parallelContinuationToken2 = new ParallelContinuationToken(
token: "asdf",
range: new Documents.Routing.Range<string>("B", "C", true, false));

OrderByContinuationToken orderByContinuationToken2 = new OrderByContinuationToken(
parallelContinuationToken2,
new List<OrderByItem>() { new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>() { { "item", CosmosString.Create("asdf") } })) },
rid: "rid",
skipCount: 42,
filter: "filter");

TryCatch<IQueryPipelineStage> monadicCreate = OrderByCrossPartitionQueryPipelineStage.MonadicCreate(
documentContainer: mockDocumentContainer.Object,
sqlQuerySpec: new SqlQuerySpec("SELECT * FROM c ORDER BY c._ts"),
targetRanges: new List<FeedRangeEpk>()
{
new FeedRangeEpk(new Range<string>(min: "A", max: "B", isMinInclusive: true, isMaxInclusive: false)),
new FeedRangeEpk(new Range<string>(min: "B", max: "C", isMinInclusive: true, isMaxInclusive: false)),
},
partitionKey: null,
orderByColumns: new List<OrderByColumn>()
foreach((string token1, CosmosElement element1) in tokens)
{
foreach ((string token2, CosmosElement element2) in tokens)
{
new OrderByColumn("_ts", SortOrder.Ascending)
},
queryPaginationOptions: new QueryPaginationOptions(pageSizeHint: 10),
maxConcurrency: 10,
cancellationToken: default,
continuationToken: CosmosArray.Create(
new List<CosmosElement>()
{
OrderByContinuationToken.ToCosmosElement(orderByContinuationToken1),
OrderByContinuationToken.ToCosmosElement(orderByContinuationToken2)
}));
Assert.IsTrue(monadicCreate.Succeeded);
ParallelContinuationToken parallelContinuationToken1 = new ParallelContinuationToken(
token: token1,
range: new Documents.Routing.Range<string>("A", "B", true, false));

OrderByContinuationToken orderByContinuationToken1 = new OrderByContinuationToken(
parallelContinuationToken1,
new List<OrderByItem>() { new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>() { { "item", element1 } })) },
rid: "rid",
skipCount: 42,
filter: "filter");

ParallelContinuationToken parallelContinuationToken2 = new ParallelContinuationToken(
token: token2,
range: new Documents.Routing.Range<string>("B", "C", true, false));

OrderByContinuationToken orderByContinuationToken2 = new OrderByContinuationToken(
parallelContinuationToken2,
new List<OrderByItem>() { new OrderByItem(CosmosObject.Create(new Dictionary<string, CosmosElement>() { { "item", element2 } })) },
rid: "rid",
skipCount: 42,
filter: "filter");

TryCatch<IQueryPipelineStage> monadicCreate = OrderByCrossPartitionQueryPipelineStage.MonadicCreate(
documentContainer: mockDocumentContainer.Object,
sqlQuerySpec: new SqlQuerySpec("SELECT * FROM c ORDER BY c.item"),
targetRanges: new List<FeedRangeEpk>()
{
new FeedRangeEpk(new Range<string>(min: "A", max: "B", isMinInclusive: true, isMaxInclusive: false)),
new FeedRangeEpk(new Range<string>(min: "B", max: "C", isMinInclusive: true, isMaxInclusive: false)),
},
partitionKey: null,
orderByColumns: new List<OrderByColumn>()
{
new OrderByColumn("item", SortOrder.Ascending)
},
queryPaginationOptions: new QueryPaginationOptions(pageSizeHint: 10),
maxConcurrency: 10,
cancellationToken: default,
continuationToken: CosmosArray.Create(
new List<CosmosElement>()
{
OrderByContinuationToken.ToCosmosElement(orderByContinuationToken1),
OrderByContinuationToken.ToCosmosElement(orderByContinuationToken2)
}));
Assert.IsTrue(monadicCreate.Succeeded);
}
}
}

[TestMethod]
Expand Down

0 comments on commit fdcb62f

Please sign in to comment.