Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests : A better infrastructure for asserting includes in the query #20958

Merged
merged 1 commit into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
638 changes: 264 additions & 374 deletions test/EFCore.Specification.Tests/Query/ComplexNavigationsQueryTestBase.cs

Large diffs are not rendered by default.

349 changes: 159 additions & 190 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,31 @@ where o2.CustomerID.StartsWith("A")
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_Where_Navigation_Included(bool async)
{
return AssertIncludeQuery(
return AssertQuery(
async,
ss => from o in ss.Set<Order>().Include(o => o.Customer)
where o.Customer.City == "Seattle"
select o,
new List<IExpectedInclude> { new ExpectedInclude<Order>(o => o.Customer) },
elementAsserter: (e, a) => AssertInclude(e, a, new ExpectedInclude<Order>(o => o.Customer)),
entryCount: 15);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Include_with_multiple_optional_navigations(bool async)
{
var expectedIncludes = new List<IExpectedInclude>
var expectedIncludes = new IExpectedInclude[]
{
new ExpectedInclude<OrderDetail>(od => od.Order),
new ExpectedInclude<Order>(o => o.Customer, "Order")
};

return AssertIncludeQuery(
return AssertQuery(
async,
ss => ss.Set<OrderDetail>()
.Include(od => od.Order.Customer)
.Where(od => od.Order.Customer.City == "London"),
expectedIncludes,
elementAsserter: (e, a) => AssertInclude(e, a, expectedIncludes),
entryCount: 164);
}

Expand Down Expand Up @@ -985,7 +985,7 @@ public virtual void Navigation_in_subquery_referencing_outer_query()
{
using var context = CreateContext();
var query = from o in context.Orders
// ReSharper disable once UseMethodAny.0
// ReSharper disable once UseMethodAny.0
where (from od in context.OrderDetails
where o.Customer.Country == od.Order.Customer.Country
select od).Count()
Expand Down
47 changes: 6 additions & 41 deletions test/EFCore.Specification.Tests/Query/QueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,47 +80,6 @@ public Task AssertQueryScalar<TResult>(
where TResult : struct
=> Fixture.QueryAsserter.AssertQueryScalar(actualQuery, expectedQuery, assertOrder, async, testMethodName);

public Task<List<TResult>> AssertIncludeQuery<TResult>(
bool async,
Func<ISetSource, IQueryable<TResult>> query,
List<IExpectedInclude> expectedIncludes,
Func<TResult, object> elementSorter = null,
List<Func<TResult, object>> clientProjections = null,
bool assertOrder = false,
int entryCount = 0,
[CallerMemberName] string testMethodName = null)
=> AssertIncludeQuery(
async,
query,
query,
expectedIncludes,
elementSorter,
clientProjections,
assertOrder,
entryCount,
testMethodName);

public Task<List<TResult>> AssertIncludeQuery<TResult>(
bool async,
Func<ISetSource, IQueryable<TResult>> actualQuery,
Func<ISetSource, IQueryable<TResult>> expectedQuery,
List<IExpectedInclude> expectedIncludes,
Func<TResult, object> elementSorter = null,
List<Func<TResult, object>> clientProjections = null,
bool assertOrder = false,
int entryCount = 0,
[CallerMemberName] string testMethodName = null)
=> Fixture.QueryAsserter.AssertIncludeQuery(
actualQuery,
expectedQuery,
expectedIncludes,
elementSorter,
clientProjections,
assertOrder,
entryCount,
async,
testMethodName);

protected Task AssertSingleResult<TResult>(
bool async,
Expression<Func<ISetSource, TResult>> syncQuery,
Expand Down Expand Up @@ -1117,6 +1076,12 @@ protected void AssertCollection<TElement>(
Action<TElement, TElement> elementAsserter = null)
=> Fixture.QueryAsserter.AssertCollection(expected, actual, ordered, elementSorter, elementAsserter);

protected void AssertInclude<TEntity>(
TEntity expected,
TEntity actual,
params IExpectedInclude[] expectedIncludes)
=> Fixture.QueryAsserter.AssertInclude(expected, actual, expectedIncludes);

protected void AssertGrouping<TKey, TElement>(
IGrouping<TKey, TElement> expected,
IGrouping<TKey, TElement> actual,
Expand Down
66 changes: 3 additions & 63 deletions test/EFCore.Specification.Tests/TestUtilities/QueryAsserter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,69 +212,6 @@ public override async Task AssertQueryScalar<TResult>(
assertOrder);
}

public override async Task<List<TResult>> AssertIncludeQuery<TResult>(
Func<ISetSource, IQueryable<TResult>> actualQuery,
Func<ISetSource, IQueryable<TResult>> expectedQuery,
List<IExpectedInclude> expectedIncludes,
Func<TResult, object> elementSorter,
List<Func<TResult, object>> clientProjections,
bool assertOrder,
int entryCount,
bool async,
string testMethodName)
{
using var context = _contextCreator();
var query = actualQuery(SetSourceCreator(context));
if (ProceduralQueryGeneration && !async)
{
new ProcedurallyGeneratedQueryExecutor().Execute(query, context, testMethodName);

return default;
}

OrderingSettingsVerifier(assertOrder, query.Expression.Type, elementSorter);

var actual = async
? await query.ToListAsync()
: query.ToList();

AssertRogueExecution(actual.Count, query);

var expected = GetExpectedResults(expectedQuery).ToList();

if (!assertOrder
&& elementSorter == null)
{
_entitySorters.TryGetValue(typeof(TResult), out var sorter);
elementSorter = (Func<TResult, object>)sorter;
}

if (elementSorter != null)
{
actual = actual.OrderBy(elementSorter).ToList();
expected = expected.OrderBy(elementSorter).ToList();
}

if (clientProjections != null)
{
foreach (var clientProjection in clientProjections)
{
var projectedActual = actual.Select(clientProjection).ToList();
var projectedExpected = expected.Select(clientProjection).ToList();

_includeResultAsserter.AssertResult(projectedExpected, projectedActual, expectedIncludes);
}
}
else
{
_includeResultAsserter.AssertResult(expected, actual, expectedIncludes);
}

Assert.Equal(entryCount, context.ChangeTracker.Entries().Count());

return actual;
}

#region Assert termination operation methods

public override async Task AssertAny<TResult>(
Expand Down Expand Up @@ -1551,6 +1488,9 @@ public override void AssertCollection<TElement>(
}
}

public override void AssertInclude<TEntity>(TEntity expected, TEntity actual, IExpectedInclude[] expectedIncludes)
=> _includeResultAsserter.AssertResult(expected, actual, expectedIncludes);

#endregion

private class DefaultSetSource : ISetSource
Expand Down
16 changes: 5 additions & 11 deletions test/EFCore.Specification.Tests/TestUtilities/QueryAsserterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ public abstract Task AssertQueryScalar<TResult>(
string testMethodName)
where TResult : struct;

public abstract Task<List<TResult>> AssertIncludeQuery<TResult>(
Func<ISetSource, IQueryable<TResult>> actualQuery,
Func<ISetSource, IQueryable<TResult>> expectedQuery,
List<IExpectedInclude> expectedIncludes,
Func<TResult, object> elementSorter,
List<Func<TResult, object>> clientProjections,
bool assertOrder,
int entryCount,
bool async,
string testMethodName);

#region Assert termination operation methods

public abstract Task AssertAny<TResult>(
Expand Down Expand Up @@ -537,6 +526,11 @@ public abstract void AssertCollection<TElement>(
Func<TElement, object> elementSorter = null,
Action<TElement, TElement> elementAsserter = null);

public abstract void AssertInclude<TEntity>(
TEntity expected,
TEntity actual,
IExpectedInclude[] expectedIncludes);

#endregion
}
}
22 changes: 9 additions & 13 deletions test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,18 @@ public static int AssertResults<T>(
Assert.Equal(expected.Count, actual.Count);

if (elementSorter == null
&& !verifyOrdered)
&& !verifyOrdered
&& expected.Count > 1 // If there is only 1 element then sorting is not necessary
&& expected.FirstOrDefault(e => e != null) is T nonNullElement
&& nonNullElement.GetType().GetInterface(nameof(IComparable)) == null)
{
if (expected.Count > 1)
if (elementAsserter != null)
{
if (expected.FirstOrDefault(e => e != null) is T nonNullElement
&& nonNullElement.GetType().GetInterface(nameof(IComparable)) == null)
{
if (elementAsserter != null)
{
throw new InvalidOperationException(
"Element asserter will not be used because results are not properly ordered - either remove asserter from the AssertQuery, add element sorter or set assertOrder to 'true'.");
}

return AssertResults(expected, actual);
}
throw new InvalidOperationException(
"Element asserter will not be used because results are not properly ordered - either remove asserter from the AssertQuery, add element sorter or set assertOrder to 'true'.");
}

return AssertResults(expected, actual);
}

elementSorter ??= (e => e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4839,22 +4839,6 @@ FROM [Gears] AS [g]
) AS [t0] ON [s].[Id] = [t0].[SquadId]");
}

public override async Task Include_with_order_by_constant_null_of_non_mapped_type(bool async)
{
await base.Include_with_order_by_constant_null_of_non_mapped_type(async);

AssertSql(
"");
}

public override void Include_groupby_constant_null_of_non_mapped_type()
{
base.Include_groupby_constant_null_of_non_mapped_type();

AssertSql(
"");
}

public override async Task Include_collection_OrderBy_aggregate(bool async)
{
await base.Include_collection_OrderBy_aggregate(async);
Expand Down