From 43d19dedaca85f62217d4a2fbaa865cffeca9d9d Mon Sep 17 00:00:00 2001 From: Andrea Canciani Date: Wed, 3 Jul 2024 09:45:18 +0200 Subject: [PATCH] Add tests for null check removal --- .../Query/NullSemanticsQueryTestBase.cs | 46 +++++++++++++ .../Query/NullSemanticsQuerySqlServerTest.cs | 67 +++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs index e089e1927c6..bd34fd92d8d 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs @@ -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().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().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().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().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().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) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs index db5ab5edae1..b77f60c9cb0 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs @@ -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);