Skip to content

Commit

Permalink
[Internal] Tests: Fixes query tests for new emulator (#3334)
Browse files Browse the repository at this point in the history
Co-authored-by: Neil Deshpande <[email protected]>
  • Loading branch information
ealsur and neildsh committed Jul 11, 2022
1 parent dc009da commit 7d96452
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1593,56 +1593,61 @@ public void TestDistinctSelectManyIssues()
[TestMethod]
public void TestDistinctTranslation()
{
static LinqTestInput DistinctTestInput(string description, System.Linq.Expressions.Expression<Func<bool, IQueryable>> expr)
{
return new LinqTestInput(description, expr, true);
}

List<LinqTestInput> inputs = new List<LinqTestInput>();

// Simple distinct
// Select -> Distinct for all data types
inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct string",
b => getQuery(b).Select(f => f.FamilyId).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct int",
b => getQuery(b).Select(f => f.Int).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct bool",
b => getQuery(b).Select(f => f.IsRegistered).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct nullable int",
b => getQuery(b).Where(f => f.NullableInt != null).Select(f => f.NullableInt).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct null",
b => getQuery(b).Where(f => f.NullObject != null).Select(f => f.NullObject).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct children",
b => getQuery(b).SelectMany(f => f.Children).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct parent",
b => getQuery(b).SelectMany(f => f.Parents).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct family",
b => getQuery(b).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Multiple distincts",
b => getQuery(b).Distinct().Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct new obj",
b => getQuery(b).Select(f => new { Parents = f.Parents.Count(), Children = f.Children.Count() }).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct new obj",
b => getQuery(b).Select(f => new { Parents = f.Parents.Count(), Children = f.Children.Count() }).Select(f => f.Parents).Distinct()));

// Distinct + Take
inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct -> Take",
b => getQuery(b).Select(f => f.Int).Distinct().Take(10)));

Expand All @@ -1655,7 +1660,7 @@ public void TestDistinctTranslation()
"Distinct -> OrderBy",
b => getQuery(b).Select(f => f.Int).Distinct().OrderBy(x => x)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"OrderBy -> Distinct",
b => getQuery(b).OrderBy(f => f.Int).Distinct()));

Expand All @@ -1669,20 +1674,20 @@ public void TestDistinctTranslation()
b => getQuery(b).Select(f => f.Int).OrderBy(x => x).Take(10).Distinct()));

// Distinct + Where
inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Where -> Distinct",
b => getQuery(b).Select(f => f.Int).Where(x => x > 10).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"Distinct -> Where",
b => getQuery(b).Select(f => f.Int).Distinct().Where(x => x > 10)));

// SelectMany w Distinct
inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Select obj) -> Distinct",
b => getQuery(b).SelectMany(f => f.Parents).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select)) -> Distinct",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1691,7 +1696,7 @@ public void TestDistinctTranslation()
.Select(pet => pet)))
.Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select -> Distinct))",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1700,7 +1705,7 @@ public void TestDistinctTranslation()
.Select(pet => pet)
.Distinct()))));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select -> Select) -> Distinct)",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1710,7 +1715,7 @@ public void TestDistinctTranslation()
.Select(name => name.Count())))
.Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select new {} -> Select) -> Distinct)",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1724,7 +1729,7 @@ public void TestDistinctTranslation()
}).Select(p => p.child))
.Distinct())));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select new {}) -> Distinct)",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1738,7 +1743,7 @@ public void TestDistinctTranslation()
})))
.Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(SelectMany(Where -> Select new {} -> Distinct))",
b => getQuery(b)
.SelectMany(family => family.Children
Expand All @@ -1753,15 +1758,15 @@ public void TestDistinctTranslation()
.Distinct()))));

// SelectMany(Distinct)
inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Distinct",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Where",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Where -> Distinct",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10).Distinct()));

Expand Down Expand Up @@ -1795,7 +1800,7 @@ public void TestDistinctTranslation()
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10).Distinct()
.OrderBy(f => f).Take(5).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Where -> Distinct -> Take",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10).Distinct().Take(5)));

Expand Down Expand Up @@ -1834,7 +1839,7 @@ public void TestDistinctTranslation()
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10)
.OrderBy(f => f).Take(5)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Where -> Where",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Where(f => f.FamilyName.Count() > 10)
.Where(f => f.FamilyName.Count() < 20)));
Expand All @@ -1847,15 +1852,15 @@ public void TestDistinctTranslation()
"SelectMany(Distinct) -> OrderBy -> Take",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).OrderBy(f => f.FamilyName).Take(5)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Take",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Take(5)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Select",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Select(f => f.FamilyName.Count())));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Distinct) -> Select -> Take",
b => getQuery(b).SelectMany(f => f.Parents.Distinct()).Select(f => f.FamilyName.Count()).Take(5)));

Expand All @@ -1868,19 +1873,19 @@ public void TestDistinctTranslation()
"SelectMany(Select -> Distinct) -> Where -> OrderBy -> Take",
b => getQuery(b).SelectMany(f => f.Parents.Select(p => p.GivenName).Distinct()).Where(n => n.Count() > 10).OrderBy(n => n).Take(5)));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Select) -> Distinct",
b => getQuery(b).SelectMany(f => f.Parents.Select(p => p.FamilyName)).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Select -> Distinct)",
b => getQuery(b).SelectMany(f => f.Parents.Select(p => p.FamilyName).Distinct())));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Select -> Distinct) -> Distinct",
b => getQuery(b).SelectMany(f => f.Parents.Select(p => p.GivenName).Distinct()).Distinct()));

inputs.Add(new LinqTestInput(
inputs.Add(DistinctTestInput(
"SelectMany(Select -> Distinct) -> Where",
b => getQuery(b).SelectMany(f => f.Parents.Select(p => p.GivenName).Distinct()).Where(n => n.Count() > 10)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class DistinctQueryTests : QueryTestsBase
[TestMethod]
public async Task TestDistinct_ExecuteNextAsync()
{
async Task ImplementationAsync(Container container, IReadOnlyList<CosmosObject> documents)
static async Task ImplementationAsync(Container container, IReadOnlyList<CosmosObject> documents)
{
#region Queries
// To verify distint queries you can run it once without the distinct clause and run it through a hash set
Expand Down Expand Up @@ -110,16 +110,13 @@ async Task ImplementationAsync(Container container, IReadOnlyList<CosmosObject>
documentsFromWithDistinct.AddRange(cosmosQueryResponse);
}

Assert.AreEqual(documentsFromWithDistinct.Count, documentsFromWithoutDistinct.Count);
for (int i = 0; i < documentsFromWithDistinct.Count; i++)
{
CosmosElement documentFromWithDistinct = documentsFromWithDistinct.ElementAt(i);
CosmosElement documentFromWithoutDistinct = documentsFromWithoutDistinct.ElementAt(i);
Assert.AreEqual(
expected: documentFromWithoutDistinct,
actual: documentFromWithDistinct,
message: $"{documentFromWithDistinct} did not match {documentFromWithoutDistinct} at index {i} for {queryWithDistinct}, with page size: {pageSize} on a container");
}
string[] expectedDocuments = documentsFromWithoutDistinct.Select(x => x.ToString()).ToArray();
string[] actualDocuments = documentsFromWithDistinct.Select(x => x.ToString()).ToArray();

CollectionAssert.AreEquivalent(
expected: expectedDocuments,
actual: actualDocuments,
message: $"Documents didn't match for {queryWithDistinct}, with page size: {pageSize} on a container");

if (allowDCount)
{
Expand Down Expand Up @@ -246,9 +243,12 @@ async Task ImplemenationAsync(Container container, IReadOnlyList<CosmosObject> d
},
QueryDrainingMode.HoldState | QueryDrainingMode.CosmosElementContinuationToken);

Assert.AreEqual(
expected: CosmosArray.Create(documentsFromWithDistinct),
actual: CosmosArray.Create(documentsFromWithoutDistinct),
string[] expectedDocuments = documentsFromWithoutDistinct.Select(x => x.ToString()).ToArray();
string[] actualDocuments = documentsFromWithDistinct.Select(x => x.ToString()).ToArray();

CollectionAssert.AreEquivalent(
expected: expectedDocuments,
actual: actualDocuments,
message: $"Documents didn't match for {queryWithDistinct} on a Partitioned container");

if (allowDCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ private async Task TestQueryCrossPartitionTopOrderByDifferentDimensionHelper(
Container container,
IReadOnlyList<CosmosObject> documents)
{
await QueryTestsBase.NoOp();

string[] expected = new[] { "documentId2", "documentId5", "documentId8" };
List<CosmosElement> query = await QueryTestsBase.RunQueryAsync(
string[] expectedOrdered = new[] { "documentId2", "documentId5", "documentId8" };
string[] expectedAll = new[] { "documentId1", "documentId2", "documentId3", "documentId4", "documentId5", "documentId6", "documentId7", "documentId8", "documentId9" };
List<CosmosElement> results = await QueryTestsBase.RunQueryAsync(
container,
"SELECT r.id FROM r ORDER BY r.prop DESC",
new QueryRequestOptions()
Expand All @@ -65,9 +64,12 @@ private async Task TestQueryCrossPartitionTopOrderByDifferentDimensionHelper(
MaxConcurrency = 1,
});

Assert.AreEqual(
string.Join(", ", expected),
string.Join(", ", query.Select(doc => ((CosmosString)(doc as CosmosObject)["id"]).Value)));
string[] actual = results
.Select(doc => ((CosmosString)(doc as CosmosObject)["id"]).Value.ToString())
.ToArray();

CollectionAssert.AreEqual(expectedOrdered, actual.Take(expectedOrdered.Length).ToArray());
CollectionAssert.AreEquivalent(expectedAll, actual);
}

[TestMethod]
Expand Down

0 comments on commit 7d96452

Please sign in to comment.