Skip to content

Commit

Permalink
Add tests for null check removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ranma42 committed Jul 3, 2024
1 parent d0246ed commit 43d19de
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,52 @@ public virtual Task Is_null_on_column_followed_by_OrElse_optimizes_nullability_c
? x.NullableBoolA != x.NullableBoolC
: x.NullableBoolC != x.NullableBoolA)));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Is_not_null_optimizes_unary_op(bool async)
=> AssertQuery(
async,
ss => ss.Set<NullSemanticsEntity1>().Select(
x => x.NullableIntA != null ? ~x.NullableIntA : null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Is_not_null_optimizes_binary_op(bool async)
=> AssertQuery(
async,
ss => ss.Set<NullSemanticsEntity1>().Select(
x => x.NullableIntA != null && x.NullableIntB != null
? x.NullableIntA + x.NullableIntB
: null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Is_not_null_optimizes_binary_op_with_partial_checks(bool async)
=> AssertQuery(
async,
ss => ss.Set<NullSemanticsEntity1>().Select(
x => x.NullableStringA != null && x.NullableStringB != null
? x.NullableStringA + x.NullableStringB + x.NullableStringC
: null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Is_not_null_optimizes_binary_op_with_nested_checks(bool async)
=> AssertQuery(
async,
ss => ss.Set<NullSemanticsEntity1>().Select(
x => x.NullableStringA != null
? x.NullableStringB != null ? x.NullableStringA + x.NullableStringB : null
: null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Is_not_null_optimizes_binary_op_with_mixed_checks(bool async)
=> AssertQuery(
async,
ss => ss.Set<NullSemanticsEntity1>().Select(
x => x.NullableStringA != null && x.BoolA ? x.NullableStringA + x.NullableStringB : null));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Sum_function_is_always_considered_non_nullable(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4459,6 +4459,73 @@ ELSE CAST(0 AS bit)
""");
}

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

AssertSql(
"""
SELECT CASE
WHEN [e].[NullableIntA] IS NOT NULL THEN ~[e].[NullableIntA]
END
FROM [Entities1] AS [e]
""");
}

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

AssertSql(
"""
SELECT CASE
WHEN [e].[NullableIntA] IS NOT NULL AND [e].[NullableIntB] IS NOT NULL THEN [e].[NullableIntA] + [e].[NullableIntB]
END
FROM [Entities1] AS [e]
""");
}

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

AssertSql(
"""
SELECT CASE
WHEN [e].[NullableStringA] IS NOT NULL AND [e].[NullableStringB] IS NOT NULL THEN [e].[NullableStringA] + [e].[NullableStringB] + COALESCE([e].[NullableStringC], N'')
END
FROM [Entities1] AS [e]
""");
}

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

AssertSql(
"""
SELECT CASE
WHEN [e].[NullableStringA] IS NOT NULL THEN CASE
WHEN [e].[NullableStringB] IS NOT NULL THEN [e].[NullableStringA] + [e].[NullableStringB]
END
END
FROM [Entities1] AS [e]
""");
}

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

AssertSql(
"""
SELECT CASE
WHEN [e].[NullableStringA] IS NOT NULL AND [e].[BoolA] = CAST(1 AS bit) THEN [e].[NullableStringA] + COALESCE([e].[NullableStringB], N'')
END
FROM [Entities1] AS [e]
""");
}

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

0 comments on commit 43d19de

Please sign in to comment.