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

[SMALL] Adding regression tests for #12826 - Query: Incorrect syntax near the keyword 'AS` for group by aggregate #21077

Merged
merged 1 commit into from
Jun 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5416,5 +5416,60 @@ public virtual async Task Filtered_include_calling_methods_directly_on_parameter
.Include(l1 => l1.OneToMany_Optional1)
.ThenInclude(l2 => l2.AsQueryable().Where(xx => xx.Id != 42))))).Message;
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Element_selector_with_coalesce_repeated_in_aggregate(bool async)
{
return AssertQueryScalar(
async,
ss => ss.Set<Level1>().GroupBy(
l1 => l1.OneToOne_Required_PK1.OneToOne_Required_PK2.Name,
l1 => new
{
Id = ((int?)l1.OneToOne_Required_PK1.Id ?? 0)
})
.Where(g => g.Min(l1 => l1.Id + l1.Id) > 0)
.Select(g => g.Count()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Nested_object_constructed_from_group_key_properties(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Level1>()
.Where(l1 => l1.OneToOne_Optional_FK1 != null)
.GroupBy(l1 => new
{
l1.Id,
l1.Date,
l1.Name,
InnerId = l1.OneToOne_Optional_FK1.Id,
InnerDate = l1.OneToOne_Optional_FK1.Date,
InnerOptionalId = l1.OneToOne_Optional_FK1.Level1_Optional_Id,
InnerRequiredId = l1.OneToOne_Optional_FK1.Level1_Required_Id,
InnerName = l1.OneToOne_Required_FK1.Name
})
.Select(g => new
{
NestedEntity = new Level1
{
Id = g.Key.Id,
Name = g.Key.Name,
Date = g.Key.Date,
OneToOne_Optional_FK1 = new Level2
{
Id = g.Key.InnerId,
Name = g.Key.InnerName,
Date = g.Key.InnerDate,
Level1_Optional_Id = g.Key.InnerOptionalId,
Level1_Required_Id = g.Key.InnerRequiredId
}
},
Aggregate = g.Sum(x => x.Name.Length)
}));
}
}
}
13 changes: 13 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7574,6 +7574,19 @@ public virtual Task Coalesce_used_with_non_unicode_string_column_and_constant(bo
ss => ss.Set<City>().Select(c => c.Location ?? "Unknown"));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Groupby_anonymous_type_with_navigations_followed_up_by_anonymous_projection_and_orderby(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Weapon>()
.GroupBy(w => new { w.Owner.CityOfBirth.Name, w.Owner.CityOfBirth.Location })
.Select(x => new { x.Key.Name, x.Key.Location, Count = x.Count() })
.OrderBy(x => x.Location),
assertOrder: true);
}

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

protected virtual void ClearLog()
Expand Down
158 changes: 158 additions & 0 deletions test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,73 @@ public virtual Task Group_by_with_arithmetic_operation_inside_aggregate(bool asy
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Group_by_with_projection_into_DTO(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>().GroupBy(o => o.OrderID).Select(x => new LongIntDto { Id = x.Key, Count = x.Count() }),
elementSorter: e => e.Id,
elementAsserter: (e, a) =>
{
Assert.Equal(e.Id, a.Id);
Assert.Equal(e.Count, a.Count);
});
}

private class LongIntDto
{
public long Id { get; set; }
public int Count { get; set; }
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Where_select_function_groupby_followed_by_another_select_with_aggregates(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Order>()
.Where(o => o.CustomerID.StartsWith("A"))
.Select(o => new { o.CustomerID, Age = 2020 - o.OrderDate.Value.Year, o.OrderID })
.GroupBy(x => x.CustomerID)
.Select(x => new
{
x.Key,
Sum1 = x.Sum(y => y.Age <= 30 ? y.OrderID : 0),
Sum2 = x.Sum(y => y.Age > 30 && y.Age <= 60 ? y.OrderID : 0)
}));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Group_by_column_project_constant(bool async)
{
return AssertQueryScalar(
async,
ss => ss.Set<Order>().GroupBy(o => o.CustomerID).OrderBy(g => g.Key).Select(e => 42));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Key_plus_key_in_projection(bool async)
{
return AssertQuery(
async,
ss => (from o in ss.Set<Order>()
join c in ss.Set<Customer>() on o.CustomerID equals c.CustomerID into grouping
from c in grouping.DefaultIfEmpty()
select o)
.GroupBy(o => o.OrderID)
.Select(
g => new
{
Value = g.Key + g.Key,
Average = g.Average(o => o.OrderID)
}));
}

#endregion

#region GroupByAnonymousAggregate
Expand Down Expand Up @@ -998,6 +1065,19 @@ public virtual Task GroupBy_based_on_renamed_property_complex(bool async)
elementSorter: e => e.Key);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Join_groupby_anonymous_orderby_anonymous_projection(bool async)
{
return AssertQuery(
async,
ss => from c in ss.Set<Customer>()
join o in ss.Set<Order>() on c.CustomerID equals o.CustomerID
group new { c, o } by new { c.CustomerID, o.OrderDate } into grouping
orderby grouping.Key.OrderDate
select new { grouping.Key.CustomerID, grouping.Key.OrderDate });
}

#endregion

#region GroupByWithElementSelectorAggregate
Expand Down Expand Up @@ -1193,6 +1273,30 @@ public virtual Task GroupBy_element_selector_complex_aggregate4(bool async)
.Select(g => g.Sum(e => e)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Element_selector_with_case_block_repeated_inside_another_case_block_in_projection(bool async)
{
return AssertQuery(
async,
ss => from order in ss.Set<Order>()
group new
{
IsAlfki = order.CustomerID == "ALFKI",
OrderId = order.OrderID > 1000 ? order.OrderID : -order.OrderID
} by
new
{
order.OrderID
}
into g
select new
{
g.Key.OrderID,
Aggregate = g.Sum(s => s.IsAlfki ? s.OrderId : -s.OrderId)
});
}

#endregion

#region GroupByAfterComposition
Expand Down Expand Up @@ -1538,6 +1642,25 @@ public virtual Task GroupBy_principal_key_property_optimization(bool async)
g => new { g.Key, Count = g.Count() }));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_after_anonymous_projection_and_distinct_followed_by_another_anonymous_projection(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>()
.Select(o => new { o.CustomerID, o.OrderID })
.Distinct()
.GroupBy(x => new { x.CustomerID })
.Select(g => new { Key = g.Key.CustomerID, Count = g.Count() }),
elementSorter: e => (e.Key, e.Count),
elementAsserter: (e, a) =>
{
Assert.Equal(e.Key, a.Key);
Assert.Equal(e.Count, a.Count);
});
}

#endregion

#region GroupByAggregateComposition
Expand Down Expand Up @@ -2008,6 +2131,41 @@ public virtual Task GroupBy_Property_Select_LongCount_with_predicate(bool async)
ss => ss.Set<Order>().GroupBy(o => o.CustomerID).Select(g => g.LongCount(o => o.OrderID < 10300)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_orderby_projection_with_coalesce_operation(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>()
.GroupBy(c => c.City)
.OrderByDescending(x => x.Count())
.ThenBy(x => x.Key)
.Select(x => new
{
Locality = x.Key ?? "Unknown",
Count = x.Count()
}));
}

[ConditionalTheory(Skip = "issue #18923")]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_let_orderby_projection_with_coalesce_operation(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>()
.GroupBy(c => c.City)
.Select(g => new { citiesCount = g.Count(), g })
.OrderByDescending(x => x.citiesCount)
.ThenBy(x => x.g.Key)
.Select(x => new
{
Locality = x.g.Key ?? "Unknown",
Count = x.citiesCount
}));
}

#endregion

#region GroupByWithoutAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5749,6 +5749,32 @@ public override void Filtered_include_outer_parameter_used_inside_filter_split()
AssertSql(" ");
}

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

AssertSql(
@"SELECT COUNT(*)
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Id]
LEFT JOIN [LevelThree] AS [l1] ON [l0].[Id] = [l1].[Id]
GROUP BY [l1].[Name]
HAVING MIN(COALESCE([l0].[Id], 0) + COALESCE([l0].[Id], 0)) > 0");
}

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

AssertSql(
@"SELECT [l].[Id], [l].[Name], [l].[Date], [l0].[Id], [l1].[Name], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], COALESCE(SUM(CAST(LEN([l].[Name]) AS int)), 0) AS [Aggregate]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
LEFT JOIN [LevelTwo] AS [l1] ON [l].[Id] = [l1].[Level1_Required_Id]
WHERE [l0].[Id] IS NOT NULL
GROUP BY [l].[Id], [l].[Date], [l].[Name], [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l1].[Name]");
}

private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7006,6 +7006,19 @@ public override async Task Coalesce_used_with_non_unicode_string_column_and_cons
FROM [Cities] AS [c]");
}

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

AssertSql(
@"SELECT [c].[Name], [c].[Location], COUNT(*) AS [Count]
FROM [Weapons] AS [w]
LEFT JOIN [Gears] AS [g] ON [w].[OwnerFullName] = [g].[FullName]
LEFT JOIN [Cities] AS [c] ON [g].[CityOfBirthName] = [c].[Name]
GROUP BY [c].[Name], [c].[Location]
ORDER BY [c].[Location]");
}

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