diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.DotNet9_0.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.DotNet9_0.verified.txt new file mode 100644 index 00000000..997ea982 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.DotNet9_0.verified.txt @@ -0,0 +1,2 @@ +SELECT [dbo].[Function1]() +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.verified.txt new file mode 100644 index 00000000..997ea982 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function1_IsInlined.verified.txt @@ -0,0 +1,2 @@ +SELECT [dbo].[Function1]() +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.DotNet9_0.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.DotNet9_0.verified.txt new file mode 100644 index 00000000..3297b317 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.DotNet9_0.verified.txt @@ -0,0 +1,2 @@ +SELECT [dbo].[Function2]([t].[Id]) +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.verified.txt new file mode 100644 index 00000000..3297b317 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_IsInlined.verified.txt @@ -0,0 +1,2 @@ +SELECT [dbo].[Function2]([t].[Id]) +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.DotNet9_0.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.DotNet9_0.verified.txt new file mode 100644 index 00000000..d7992b64 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.DotNet9_0.verified.txt @@ -0,0 +1,4 @@ +DECLARE @__arg_0 int = 1; + +SELECT [dbo].[Function2](@__arg_0) +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.verified.txt new file mode 100644 index 00000000..d7992b64 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function2_WithExplicitArg_IsInlined.verified.txt @@ -0,0 +1,4 @@ +DECLARE @__arg_0 int = 1; + +SELECT [dbo].[Function2](@__arg_0) +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function3_IsInlined.verified.txt b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function3_IsInlined.verified.txt new file mode 100644 index 00000000..6896ee17 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.Function3_IsInlined.verified.txt @@ -0,0 +1,2 @@ +SELECT [dbo].[Function3]() +FROM [TestEntity] AS [t] \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.cs b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.cs new file mode 100644 index 00000000..6d7aa94f --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.FunctionalTests/DbFunctionTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using EntityFrameworkCore.Projectables.FunctionalTests.Helpers; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; + +namespace EntityFrameworkCore.Projectables.FunctionalTests; + +[UsesVerify] +public class DbFunctionTests +{ + public record TestEntity + { + public int Id { get; set; } + + [Projectable] + public int Sample1() => DbFunctions.Function1(); + + [Projectable] + public int Sample2() => DbFunctions.Function2(Id); + + [Projectable] + public int Sample3(int arg) => DbFunctions.Function2(arg); + } + + public static class DbFunctions + { + public static int Function1() => throw new NotSupportedException(); + + public static int Function2(int a) => throw new NotSupportedException(); + } + + public class DbFunctionSampleDbContext : SampleDbContext + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.HasDbFunction(typeof(DbFunctions).GetMethod(nameof(DbFunctions.Function1))!) + .HasName(nameof(DbFunctions.Function1)); + + modelBuilder.HasDbFunction(typeof(DbFunctions).GetMethod(nameof(DbFunctions.Function2))!) + .HasName(nameof(DbFunctions.Function2)); + + base.OnModelCreating(modelBuilder); + } + } + + [Fact] + public Task Function1_IsInlined() + { + var dbContext = new DbFunctionSampleDbContext(); + var query = dbContext.Set().Select(x => x.Sample1()); + + return Verifier.Verify(query.ToQueryString()); + } + + [Fact] + public Task Function2_IsInlined() + { + var dbContext = new DbFunctionSampleDbContext(); + var query = dbContext.Set().Select(x => x.Sample2()); + + return Verifier.Verify(query.ToQueryString()); + } + + [Fact] + public Task Function2_WithExplicitArg_IsInlined() + { + var dbContext = new DbFunctionSampleDbContext(); + var arg = 1; + var query = dbContext.Set().Select(x => x.Sample3(arg)); + + return Verifier.Verify(query.ToQueryString()); + } +}