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

Query: Add regression tests #18892

Merged
merged 5 commits into from
Nov 22, 2019
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
24 changes: 24 additions & 0 deletions test/EFCore.Specification.Tests/MusicStoreTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,30 @@ await context.Database.CreateExecutionStrategy().ExecuteAsync(
});
}

[ConditionalTheory]
[InlineData(true)]
[InlineData(false)]
public virtual async Task Custom_projection_FirstOrDefault_works(bool async)
{
using var context = CreateContext();
var shoppingCartId = "CartId_A";
var id = 1;
var query = context.CartItems
.Select(ci => new CartItem
{
CartId = ci.CartId,
CartItemId = ci.CartItemId,
Count = ci.Count,
Album = new Album { Title = ci.Album.Title }
});

var cartItem = async
? await query.FirstOrDefaultAsync(ci => ci.CartId == shoppingCartId && ci.CartItemId == id)
: query.FirstOrDefault(ci => ci.CartId == shoppingCartId && ci.CartItemId == id);

Assert.Null(cartItem);
}

private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItems)
{
var albums = CreateTestAlbums(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5559,5 +5559,33 @@ public virtual Task OrderBy_collection_count_ThenBy_reference_navigation(bool as
() => l1.OneToOne_Required_FK1.OneToOne_Required_FK2.Name)),
assertOrder: true);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Null_conditional_is_not_applied_explicitly_for_optional_navigation(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Level1>().Where(l1 => l1.OneToOne_Optional_FK1 != null && l1.OneToOne_Optional_FK1.Name == "L2 01"));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task LeftJoin_with_Any_on_outer_source_and_projecting_collection_from_inner(bool async)
{
var validIds = new List<string> { "L1 01", "L1 02" };

return AssertQuery(
async,
ss => from l1 in ss.Set<Level1>().Where(l1 => validIds.Any(e => e == l1.Name))
join l2 in ss.Set<Level2>()
on l1.Id equals l2.Level1_Required_Id into l2s
from l2 in l2s.DefaultIfEmpty()
select new Level2
{
Id = l2 == null ? 0 : l2.Id,
OneToMany_Required2 = l2 == null ? null : l2.OneToMany_Required2
});
}
}
}
23 changes: 23 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7259,6 +7259,29 @@ await AssertQuery(
ss => ss.Set<Gear>().Where(g => (g.HasSoulPatch || prm) != prm));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Cast_OfType_works_correctly(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Gear>().Cast<Gear>().OfType<Officer>().Select(o => o.FullName));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Join_inner_source_custom_projection_followed_by_filter(bool async)
{
return AssertQuery(
async,
ss => from ll in ss.Set<LocustLeader>()
join h in ss.Set<Faction>().OfType<LocustHorde>()
.Select(f => new { IsEradicated = f.Name == "Locust" ? (bool?)true : null, f.CommanderName, f.Name })
on ll.Name equals h.CommanderName
where h.IsEradicated != true
select h);
}

protected async Task AssertTranslationFailed(Func<Task> testCode)
{
Assert.Contains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,26 @@ public virtual Task Complex_query_with_groupBy_in_subquery4(bool async)
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_scalar_subquery(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>()
.GroupBy(o => ss.Set<Customer>()
.Where(c => c.CustomerID == o.CustomerID)
.Select(c => c.ContactName)
.FirstOrDefault())
.Select(
g => new
{
g.Key,
Count = g.Count()
}),
elementSorter: e => e.Key);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,33 @@ FROM [LevelThree] AS [l2]
WHERE [l0].[Id] IS NOT NULL AND ([l0].[Id] = [l2].[OneToMany_Required_Inverse3Id])), [l1].[Name]");
}

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

AssertSql(
@"SELECT [l].[Id], [l].[Date], [l].[Name], [l].[OneToMany_Optional_Self_Inverse1Id], [l].[OneToMany_Required_Self_Inverse1Id], [l].[OneToOne_Optional_Self1Id]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]
WHERE [l0].[Id] IS NOT NULL AND ([l0].[Name] = N'L2 01')");
}

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

AssertSql(
@"SELECT CASE
WHEN [l0].[Id] IS NULL THEN 0
ELSE [l0].[Id]
END, [l].[Id], [l1].[Id], [l1].[Level2_Optional_Id], [l1].[Level2_Required_Id], [l1].[Name], [l1].[OneToMany_Optional_Inverse3Id], [l1].[OneToMany_Optional_Self_Inverse3Id], [l1].[OneToMany_Required_Inverse3Id], [l1].[OneToMany_Required_Self_Inverse3Id], [l1].[OneToOne_Optional_PK_Inverse3Id], [l1].[OneToOne_Optional_Self3Id]
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Required_Id]
LEFT JOIN [LevelThree] AS [l1] ON [l0].[Id] = [l1].[OneToMany_Required_Inverse3Id]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
ORDER BY [l].[Id], [l1].[Id]");
}

private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7226,6 +7226,40 @@ FROM [Gears] AS [g]
WHERE [g].[Discriminator] IN (N'Gear', N'Officer') AND ([g].[HasSoulPatch] <> @__prm_0)");
}

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

AssertSql(
@"SELECT [g].[FullName]
FROM [Gears] AS [g]
WHERE [g].[Discriminator] IN (N'Gear', N'Officer') AND ([g].[Discriminator] = N'Officer')");
}

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

AssertSql(
@"SELECT CASE
WHEN [t].[Name] = N'Locust' THEN CAST(1 AS bit)
ELSE NULL
END AS [IsEradicated], [t].[CommanderName], [t].[Name]
FROM [LocustLeaders] AS [l]
INNER JOIN (
SELECT [f].[Id], [f].[CapitalName], [f].[Discriminator], [f].[Name], [f].[CommanderName], [f].[Eradicated]
FROM [Factions] AS [f]
WHERE ([f].[Discriminator] = N'LocustHorde') AND ([f].[Discriminator] = N'LocustHorde')
) AS [t] ON [l].[Name] = [t].[CommanderName]
WHERE [l].[Discriminator] IN (N'LocustLeader', N'LocustCommander') AND ((CASE
WHEN [t].[Name] = N'Locust' THEN CAST(1 AS bit)
ELSE NULL
END <> CAST(1 AS bit)) OR CASE
WHEN [t].[Name] = N'Locust' THEN CAST(1 AS bit)
ELSE NULL
END IS NULL)");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,14 @@ FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]");
}

[ConditionalTheory(Skip = "Issue#19027")]
public override async Task GroupBy_scalar_subquery(bool async)
{
await base.GroupBy_scalar_subquery(async);

AssertSql(" ");
}

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

Expand Down