Skip to content

Commit

Permalink
Add test for member access over conditional
Browse files Browse the repository at this point in the history
i.e. for issue dotnet#34589
  • Loading branch information
ranma42 committed Jan 6, 2025
1 parent b2b3c18 commit 47c81a9
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 2 deletions.
30 changes: 30 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,36 @@ from t2 in ss.Set<CogTag>()
AssertEqual(e.t2, e.t2);
});

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Conditional_Navigation_With_Trivial_Member_Access(bool async)
=> AssertQuery(
async,
ss => ss.Set<Gear>()
.Where(g => (g.AssignedCity != null ? g.AssignedCity : g.CityOfBirth).Name != "Ephyra")
.Select(g => new { g.Nickname }),
elementSorter: e => e.Nickname);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Conditional_Navigation_With_Member_Access_On_Same_Type(bool async)
=> AssertQuery(
async,
ss => ss.Set<Gear>()
.Where(g => (g.AssignedCity != null ? g.AssignedCity : g.CityOfBirth).Nation == "Tyrus")
.Select(g => new { g.Nickname, g.FullName }),
elementSorter: e => e.Nickname);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Conditional_Navigation_With_Member_Access_On_Related_Types(bool async)
=> AssertQuery(
async,
ss => ss.Set<LocustHorde>()
.Where(g => (g.DeputyCommander != null ? g.DeputyCommander : g.Commander).ThreatLevel == 4)
.Select(g => new { g.Name }),
elementSorter: e => e.Name);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Singleton_Navigation_With_Member_Access(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,61 @@ CROSS JOIN [Tags] AS [t0]
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname]
FROM [Gears] AS [g]
LEFT JOIN [Cities] AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Name]
ELSE [c0].[Name]
END <> N'Ephyra'
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname], [g].[FullName]
FROM [Gears] AS [g]
LEFT JOIN [Cities] AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Nation]
ELSE [c0].[Nation]
END = N'Tyrus'
""");
}

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

AssertSql(
"""
SELECT [f].[Name]
FROM [Factions] AS [f]
LEFT JOIN [LocustLeaders] AS [l] ON [f].[DeputyCommanderName] = [l].[Name]
LEFT JOIN (
SELECT [l0].[Name], [l0].[ThreatLevel]
FROM [LocustLeaders] AS [l0]
WHERE [l0].[Discriminator] = N'LocustCommander'
) AS [l1] ON [f].[CommanderName] = [l1].[Name]
WHERE CASE
WHEN [l].[Name] IS NOT NULL THEN [l].[ThreatLevel]
ELSE [l1].[ThreatLevel]
END = CAST(4 AS smallint)
""");
}

public override async Task Select_Singleton_Navigation_With_Member_Access(bool async)
{
await base.Select_Singleton_Navigation_With_Member_Access(async);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel;
Expand Down Expand Up @@ -1366,6 +1366,75 @@ FROM [Officers] AS [o0]
""");
}

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

AssertSql(
"""
SELECT [u].[Nickname]
FROM (
SELECT [g].[Nickname], [g].[AssignedCityName], [g].[CityOfBirthName]
FROM [Gears] AS [g]
UNION ALL
SELECT [o].[Nickname], [o].[AssignedCityName], [o].[CityOfBirthName]
FROM [Officers] AS [o]
) AS [u]
LEFT JOIN [Cities] AS [c] ON [u].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [u].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Name]
ELSE [c0].[Name]
END <> N'Ephyra'
""");
}

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

AssertSql(
"""
SELECT [u].[Nickname], [u].[FullName]
FROM (
SELECT [g].[Nickname], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[FullName]
FROM [Gears] AS [g]
UNION ALL
SELECT [o].[Nickname], [o].[AssignedCityName], [o].[CityOfBirthName], [o].[FullName]
FROM [Officers] AS [o]
) AS [u]
LEFT JOIN [Cities] AS [c] ON [u].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [u].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Nation]
ELSE [c0].[Nation]
END = N'Tyrus'
""");
}

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

AssertSql(
"""
SELECT [l].[Name]
FROM [LocustHordes] AS [l]
LEFT JOIN (
SELECT [l0].[Name], [l0].[ThreatLevel]
FROM [LocustLeaders] AS [l0]
UNION ALL
SELECT [l1].[Name], [l1].[ThreatLevel]
FROM [LocustCommanders] AS [l1]
) AS [u] ON [l].[DeputyCommanderName] = [u].[Name]
LEFT JOIN [LocustCommanders] AS [l2] ON [l].[CommanderName] = [l2].[Name]
WHERE CASE
WHEN [u].[Name] IS NOT NULL THEN [u].[ThreatLevel]
ELSE [l2].[ThreatLevel]
END = CAST(4 AS smallint)
""");
}

public override async Task Select_Singleton_Navigation_With_Member_Access(bool async)
{
await base.Select_Singleton_Navigation_With_Member_Access(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,65 @@ FROM [Gears] AS [g0]
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname]
FROM [Gears] AS [g]
LEFT JOIN [Cities] AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Name]
ELSE [c0].[Name]
END <> N'Ephyra'
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname], [g].[FullName]
FROM [Gears] AS [g]
LEFT JOIN [Cities] AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Nation]
ELSE [c0].[Nation]
END = N'Tyrus'
""");
}

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

AssertSql(
"""
SELECT [f].[Name]
FROM [Factions] AS [f]
INNER JOIN [LocustHordes] AS [l] ON [f].[Id] = [l].[Id]
LEFT JOIN (
SELECT [l0].[Name], [l0].[ThreatLevel]
FROM [LocustLeaders] AS [l0]
) AS [s] ON [l].[DeputyCommanderName] = [s].[Name]
LEFT JOIN (
SELECT [l1].[Name], [l1].[ThreatLevel]
FROM [LocustLeaders] AS [l1]
INNER JOIN [LocustCommanders] AS [l2] ON [l1].[Name] = [l2].[Name]
) AS [s0] ON [l].[CommanderName] = [s0].[Name]
WHERE CASE
WHEN [s].[Name] IS NOT NULL THEN [s].[ThreatLevel]
ELSE [s0].[ThreatLevel]
END = CAST(4 AS smallint)
""");
}

public override async Task Select_Singleton_Navigation_With_Member_Access(bool async)
{
await base.Select_Singleton_Navigation_With_Member_Access(async);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.SqlServer.Internal;
Expand Down Expand Up @@ -2482,6 +2482,61 @@ LEFT JOIN (
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname]
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
LEFT JOIN [Cities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Name]
ELSE [c0].[Name]
END <> N'Ephyra'
""");
}

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

AssertSql(
"""
SELECT [g].[Nickname], [g].[FullName]
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
LEFT JOIN [Cities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [c] ON [g].[AssignedCityName] = [c].[Name]
INNER JOIN [Cities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [c0] ON [g].[CityOfBirthName] = [c0].[Name]
WHERE CASE
WHEN [c].[Name] IS NOT NULL THEN [c].[Nation]
ELSE [c0].[Nation]
END = N'Tyrus'
""");
}

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

AssertSql(
"""
SELECT [f].[Name]
FROM [Factions] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [f]
LEFT JOIN [LocustLeaders] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l] ON [f].[DeputyCommanderName] = [l].[Name]
LEFT JOIN (
SELECT [l0].[Name], [l0].[ThreatLevel]
FROM [LocustLeaders] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l0]
WHERE [l0].[Discriminator] = N'LocustCommander'
) AS [l1] ON [f].[CommanderName] = [l1].[Name]
WHERE CASE
WHEN [l].[Name] IS NOT NULL THEN [l].[ThreatLevel]
ELSE [l1].[ThreatLevel]
END = CAST(4 AS smallint)
""");
}

public override async Task Select_Singleton_Navigation_With_Member_Access(bool async)
{
await base.Select_Singleton_Navigation_With_Member_Access(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7766,6 +7766,61 @@ CROSS JOIN "Tags" AS "t0"
""");
}

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

AssertSql(
"""
SELECT "g"."Nickname"
FROM "Gears" AS "g"
LEFT JOIN "Cities" AS "c" ON "g"."AssignedCityName" = "c"."Name"
INNER JOIN "Cities" AS "c0" ON "g"."CityOfBirthName" = "c0"."Name"
WHERE CASE
WHEN "c"."Name" IS NOT NULL THEN "c"."Name"
ELSE "c0"."Name"
END <> 'Ephyra'
""");
}

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

AssertSql(
"""
SELECT "g"."Nickname", "g"."FullName"
FROM "Gears" AS "g"
LEFT JOIN "Cities" AS "c" ON "g"."AssignedCityName" = "c"."Name"
INNER JOIN "Cities" AS "c0" ON "g"."CityOfBirthName" = "c0"."Name"
WHERE CASE
WHEN "c"."Name" IS NOT NULL THEN "c"."Nation"
ELSE "c0"."Nation"
END = 'Tyrus'
""");
}

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

AssertSql(
"""
SELECT "f"."Name"
FROM "Factions" AS "f"
LEFT JOIN "LocustLeaders" AS "l" ON "f"."DeputyCommanderName" = "l"."Name"
LEFT JOIN (
SELECT "l0"."Name", "l0"."ThreatLevel"
FROM "LocustLeaders" AS "l0"
WHERE "l0"."Discriminator" = 'LocustCommander'
) AS "l1" ON "f"."CommanderName" = "l1"."Name"
WHERE CASE
WHEN "l"."Name" IS NOT NULL THEN "l"."ThreatLevel"
ELSE "l1"."ThreatLevel"
END = 4
""");
}

public override async Task Complex_GroupBy_after_set_operator_using_result_selector(bool async)
{
await base.Complex_GroupBy_after_set_operator_using_result_selector(async);
Expand Down

0 comments on commit 47c81a9

Please sign in to comment.