Skip to content

Commit

Permalink
Query: Add test for GroupBy ToList over grouping
Browse files Browse the repository at this point in the history
Resolves #10716

Also add test for #10974
  • Loading branch information
smitpatel committed Feb 15, 2018
1 parent e2a2b9b commit e2b1771
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
39 changes: 37 additions & 2 deletions src/EFCore.Specification.Tests/Query/AsyncGroupByQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,41 @@ public virtual async Task Select_GroupBy_SelectMany()
}
}

#endregion
}
[ConditionalFact]
public virtual async Task Join_GroupBy_entity_ToList()
{
using (var context = CreateContext())
{
var actual = await (from c in context.Customers
join o in context.Orders
on c.CustomerID equals o.CustomerID
group o by c into grp
select new
{
C = grp.Key,
Os = grp.ToList()
}).ToListAsync();

var expected = (from c in Fixture.QueryAsserter.ExpectedData.Set<Customer>()
join o in Fixture.QueryAsserter.ExpectedData.Set<Order>()
on c.CustomerID equals o.CustomerID
group o by c into grp
select new
{
C = grp.Key,
Os = grp.ToList()
}).ToList();

Assert.Equal(expected.Count, actual.Count);

for (var i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i].C, actual[i].C);
Assert.Equal(expected[i].Os.OrderBy(o => o.OrderID), actual[i].Os.OrderBy(o => o.OrderID));
}
}
}

#endregion
}
}
14 changes: 13 additions & 1 deletion src/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4090,7 +4090,7 @@ public virtual void Include_with_group_by_and_last()
var expected = Fixture.QueryAsserter.ExpectedData.Set<Gear>().OrderByDescending(g => g.HasSoulPatch).Include(g => g.Weapons).Select(g => new { g.Rank, g }).GroupBy(g => g.Rank).ToList().OrderBy(g => g.Key).ToList();

Assert.Equal(expected.Count, actual.Count);

for (var i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i].Key, actual[i].Key);
Expand Down Expand Up @@ -4215,6 +4215,18 @@ where MaybeScalar(h, () => h.Eradicated) != true
select h);
}

[ConditionalFact(Skip = "Issue #10974")]
public virtual void Include_collection_group_by_reference()
{
using (var context = CreateContext())
{
var query = context.Set<Gear>()
.Include(g => g.Weapons)
.GroupBy(g => g.Squad)
.ToList();
}
}

protected GearsOfWarContext CreateContext() => Fixture.CreateContext();

protected virtual void ClearLog()
Expand Down
35 changes: 35 additions & 0 deletions src/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,41 @@ public virtual void Select_GroupBy_SelectMany()
}
}

[ConditionalFact]
public virtual void Join_GroupBy_entity_ToList()
{
using (var context = CreateContext())
{
var actual = (from c in context.Customers
join o in context.Orders
on c.CustomerID equals o.CustomerID
group o by c into grp
select new
{
C = grp.Key,
Os = grp.ToList()
}).ToList();

var expected = (from c in Fixture.QueryAsserter.ExpectedData.Set<Customer>()
join o in Fixture.QueryAsserter.ExpectedData.Set<Order>()
on c.CustomerID equals o.CustomerID
group o by c into grp
select new
{
C = grp.Key,
Os = grp.ToList()
}).ToList();

Assert.Equal(expected.Count, actual.Count);

for (var i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i].C, actual[i].C);
Assert.Equal(expected[i].Os.OrderBy(o => o.OrderID), actual[i].Os.OrderBy(o => o.OrderID));
}
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5990,6 +5990,13 @@ FROM [Factions] AS [f]
WHERE [ll].[Discriminator] IN (N'LocustCommander', N'LocustLeader') AND (([t].[Eradicated] <> 1) OR [t].[Eradicated] IS NULL)");
}

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

AssertSql(" ");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,16 @@ FROM [Orders] AS [o]
ORDER BY [o].[OrderID]");
}

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

AssertSql(
@"SELECT [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region], [o0].[OrderID], [o0].[CustomerID], [o0].[EmployeeID], [o0].[OrderDate]
FROM [Customers] AS [c0]
INNER JOIN [Orders] AS [o0] ON [c0].[CustomerID] = [o0].[CustomerID]");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down

0 comments on commit e2b1771

Please sign in to comment.