From 58abc390e0e3eb849b5773da3f5ed2982ade521d Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Fri, 10 Jul 2020 17:05:19 -0700 Subject: [PATCH] Add regression tests for issue#10012 (#21589) Resolves #10012 --- .../Query/NorthwindGroupByQueryTestBase.cs | 55 +++++++++++++++++++ .../NorthwindGroupByQuerySqlServerTest.cs | 46 ++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs index a0b193d6f19..10778a7bcaf 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs @@ -2205,6 +2205,61 @@ public virtual Task GroupBy_aggregate_over_a_subquery(bool async) })); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task GroupBy_aggregate_join_with_grouping_key(bool async) + { + return AssertQuery( + async, + ss => ss.Set() + .GroupBy(o => o.CustomerID) + .Select(g => new { g.Key, Count = g.Count() }) + .Join(ss.Set(), o => o.Key, c => c.CustomerID, (o, c) => new { c, o.Count }), + elementSorter: a => a.c.CustomerID, + elementAsserter: (e, a) => + { + AssertEqual(e.c, a.c); + AssertEqual(e.Count, a.Count); + }, + entryCount: 89); + } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task GroupBy_aggregate_join_with_group_result(bool async) + { + return AssertQuery( + async, + ss => ss.Set() + .GroupBy(o => o.CustomerID, e => e.OrderDate) + .Select(g => new { g.Key, LastOrderDate = g.Max() }) + .Join(ss.Set(), o => o, i => new { Key = i.CustomerID, LastOrderDate = i.OrderDate }, (_, x) => x), + entryCount: 90); + } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task GroupBy_aggregate_from_right_side_of_join(bool async) + { + return AssertQuery( + async, + ss => (from c in ss.Set() + join o in ss.Set().GroupBy(i => i.CustomerID).Select(e => new { e.Key, Max = e.Max(i => i.OrderDate) }) + on c.CustomerID equals o.Key + select new { c, o.Max }) + .OrderBy(e => e.Max) + .ThenBy(c => c.c.CustomerID) + .Skip(10) + .Take(10), + assertOrder: true, + elementAsserter: (e, a) => + { + AssertEqual(e.c, a.c); + AssertEqual(e.Max, a.Max); + }, + entryCount: 10); + } + #endregion #region GroupByWithoutAggregate diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs index 9d4e4fb2930..9a0aa9d8e41 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs @@ -1930,6 +1930,52 @@ SELECT COUNT(*) GROUP BY [o].[CustomerID]"); } + public override async Task GroupBy_aggregate_join_with_grouping_key(bool async) + { + await base.GroupBy_aggregate_join_with_grouping_key(async); + + AssertSql( + @"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [Count] +FROM ( + SELECT [o].[CustomerID], COUNT(*) AS [c] + FROM [Orders] AS [o] + GROUP BY [o].[CustomerID] +) AS [t] +INNER JOIN [Customers] AS [c] ON [t].[CustomerID] = [c].[CustomerID]"); + } + + public override async Task GroupBy_aggregate_join_with_group_result(bool async) + { + await base.GroupBy_aggregate_join_with_group_result(async); + + AssertSql( + @"SELECT [o0].[OrderID], [o0].[CustomerID], [o0].[EmployeeID], [o0].[OrderDate] +FROM ( + SELECT [o].[CustomerID], MAX([o].[OrderDate]) AS [c] + FROM [Orders] AS [o] + GROUP BY [o].[CustomerID] +) AS [t] +INNER JOIN [Orders] AS [o0] ON (([t].[CustomerID] = [o0].[CustomerID]) OR ([t].[CustomerID] IS NULL AND [o0].[CustomerID] IS NULL)) AND (([t].[c] = [o0].[OrderDate]) OR ([t].[c] IS NULL AND [o0].[OrderDate] IS NULL))"); + } + + public override async Task GroupBy_aggregate_from_right_side_of_join(bool async) + { + await base.GroupBy_aggregate_from_right_side_of_join(async); + + AssertSql( + @"@__p_0='10' + +SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region], [t].[c] AS [Max] +FROM [Customers] AS [c] +INNER JOIN ( + SELECT [o].[CustomerID], MAX([o].[OrderDate]) AS [c] + FROM [Orders] AS [o] + GROUP BY [o].[CustomerID] +) AS [t] ON [c].[CustomerID] = [t].[CustomerID] +ORDER BY [t].[c], [c].[CustomerID] +OFFSET @__p_0 ROWS FETCH NEXT @__p_0 ROWS ONLY"); + } + public override async Task GroupBy_with_grouping_key_using_Like(bool async) { await base.GroupBy_with_grouping_key_using_Like(async);