Support for projectable generic methods

This commit is contained in:
Koen Bekkenutte
2021-10-27 01:16:26 +08:00
parent 14a8f1ebd2
commit ede53de1c2
45 changed files with 373 additions and 96 deletions
@@ -63,4 +63,4 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
return Verifier.Verify(query.ToQueryString());
}
}
}
}
@@ -0,0 +1,8 @@
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
{
public record Entity : IEntity
{
public int Id { get; set; }
public string? Name { get; set; }
}
}
@@ -0,0 +1,11 @@
using System.Linq;
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
{
public static class EntityExtensions
{
[Projectable]
public static TEntity? DefaultIfIdIsNegative<TEntity>(this TEntity entity) where TEntity : IEntity
=> entity.Id >= 0 ? entity : default;
}
}
@@ -0,0 +1,26 @@
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 VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
{
[UsesVerify]
public class GenericFunctionTests
{
[Fact]
public Task DefaultIfIdIsNegative()
{
using var context = new SampleDbContext<Entity>();
var query = context.Set<Entity>()
.Select(x => x.DefaultIfIdIsNegative());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,5 @@
SELECT CASE
WHEN [e].[Id] >= 0 THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END, [e].[Id], [e].[Name]
FROM [Entity] AS [e]
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
ORDER BY [e].[Id]
@@ -0,0 +1,7 @@
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
{
public interface IEntity
{
int Id { get; }
}
}
@@ -0,0 +1,2 @@
SELECT CAST(1 AS bit), [e].[Id], [e].[Name]
FROM [Entity] AS [e]
@@ -0,0 +1,48 @@
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 VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
{
[UsesVerify]
public class MultipleGenericsTests
{
[Projectable]
public static object? Coalesce<T1, T2>(T1? t1, T2 t2)
=> t1 != null ? t1 : t2;
[Fact]
public Task TestMultipleArguments()
{
using var context = new SampleDbContext<Entity>();
var query = context.Set<Entity>()
.Select(x => Coalesce(x.Id, x.Name));
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public void MultipleInvocations()
{
using var context = new SampleDbContext<Entity>(Infrastructure.CompatibilityMode.Full);
var query1 = context.Set<Entity>()
.Select(x => Coalesce(x.Id, x.Name))
.ToQueryString();
var query2 = context.Set<Entity>()
.Select(x => Coalesce(x.Name, x.Id))
.ToQueryString();
Assert.NotEqual(query1, query2);
}
}
}