Skip to content

Commit

Permalink
Tests : A better infrastructure for asserting includes in the query
Browse files Browse the repository at this point in the history
Remove AssertIncludeQuery which is based on queryable
Introduce AssertInclude method over element which can be used anywhere element asserter can be passed just like collection asserter

Resolves #15368
  • Loading branch information
smitpatel committed May 14, 2020
1 parent 7d5c2aa commit d8e09b6
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 715 deletions.
638 changes: 264 additions & 374 deletions test/EFCore.Specification.Tests/Query/ComplexNavigationsQueryTestBase.cs

Large diffs are not rendered by default.

353 changes: 162 additions & 191 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
}
}
21 changes: 8 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,17 @@ public static int AssertResults<T>(
Assert.Equal(expected.Count, actual.Count);

if (elementSorter == null
&& !verifyOrdered)
&& !verifyOrdered
&& 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

0 comments on commit d8e09b6

Please sign in to comment.