Rebranded to EntityFrameworkCore.Projectables

This commit is contained in:
Koen Bekkenutte
2021-05-31 21:27:17 +08:00
parent e3eda6be69
commit 300aab0767
89 changed files with 141 additions and 141 deletions
@@ -0,0 +1,10 @@
SELECT [t0].[RecordDate]
FROM [User] AS [u]
INNER JOIN (
SELECT [t].[RecordDate], [t].[UserId]
FROM (
SELECT [o].[RecordDate], [o].[UserId], ROW_NUMBER() OVER(PARTITION BY [o].[UserId] ORDER BY [o].[RecordDate] DESC) AS [row]
FROM [Order] AS [o]
) AS [t]
WHERE [t].[row] <= 2
) AS [t0] ON [u].[Id] = [t0].[UserId]
@@ -0,0 +1,6 @@
SELECT (
SELECT TOP(1) [o].[RecordDate]
FROM [Order] AS [o]
WHERE [u].[Id] = [o].[UserId]
ORDER BY [o].[RecordDate] DESC)
FROM [User] AS [u]
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
#nullable disable
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class ComplexModelTests
{
public class User
{
public int Id { get; set; }
public string DisplayName { get; set; }
public ICollection<Order> Orders { get; set; }
// todo: since Order is a nested class, we currently have to fully express the location of this class
[Projectable]
public EntityFrameworkCore.Projectables.FunctionalTests.ComplexModelTests.Order LastOrder =>
Orders.OrderByDescending(x => x.RecordDate).FirstOrDefault();
// todo: since Order is a nested class, we currently have to fully express the location of this class
[Projectable]
[NotMapped]
public IEnumerable<EntityFrameworkCore.Projectables.FunctionalTests.ComplexModelTests.Order> Last2Orders =>
Orders.OrderByDescending(x => x.RecordDate).Take(2);
}
public class Order
{
public int Id { get; set; }
public DateTime RecordDate { get; set; }
}
[Fact]
public Task ProjectOverNavigationProperty()
{
using var dbContext = new SampleDbContext<User>();
var query = dbContext.Set<User>()
.Select(x => x.LastOrder.RecordDate);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ProjectOverCollectionNavigationProperty()
{
using var dbContext = new SampleDbContext<User>();
var query = dbContext.Set<User>()
.SelectMany(x => x.Last2Orders)
.Select(x => x.RecordDate);
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="ScenarioTests.XUnit" Version="0.5.2" />
<PackageReference Include="Verify.Xunit" Version="11.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Remove="**\*.verified.txt" />
<None Remove="**\*.received.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables.Generator\EntityFrameworkCore.Projectables.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,7 @@
namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
{
public class Entity
{
public int Id { get; set; }
}
}
@@ -0,0 +1,11 @@
namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
{
public static class EntityExtensions
{
[Projectable]
public static int Squared(this int i) => i * i;
[Projectable]
public static int Foo(this Entity entity) => entity.Id + 1;
}
}
@@ -0,0 +1,2 @@
SELECT [e].[Id] * [e].[Id]
FROM [Entity] AS [e]
@@ -0,0 +1,40 @@
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 ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
{
[UsesVerify]
public class ExtensionMethodTests
{
[Fact]
public Task ExtensionOnPrimitive()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Id.Squared());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableExtensionMethod()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Foo());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Extensions;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.FunctionalTests.Helpers
{
public class SampleDbContext<TEntity> : DbContext
where TEntity : class
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\v11.0;Integrated Security=true"); // Fake connection string as we're actually never connecting
optionsBuilder.UseProjectables();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TEntity>();
}
}
}
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE ([e].[Id] + 1) = 2
@@ -0,0 +1,2 @@
SELECT [e].[Id] + [e].[Id]
FROM [Entity] AS [e]
@@ -0,0 +1,4 @@
DECLARE @__argument_0 int = 1;
SELECT [e].[Id] + @__argument_0
FROM [Entity] AS [e]
@@ -0,0 +1,2 @@
SELECT [e].[Id] + 1
FROM [Entity] AS [e]
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatefullComplexFunctionTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed(int argument) => Id + argument;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Where(x => x.Computed(1) == 2);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(1));
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task PassInVariableArguments()
{
using var dbContext = new SampleDbContext<Entity>();
var argument = 1;
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(argument));
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task PassInReferenceArguments()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(x.Id));
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,2 @@
SELECT [e].[Id] + ([e].[Id] * 2)
FROM [Entity] AS [e]
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE ([e].[Id] * 2) = 2
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE [e].[Id] = 1
@@ -0,0 +1,2 @@
SELECT [e].[Id] * 2
FROM [Entity] AS [e]
@@ -0,0 +1,2 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatefullPropertyTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed1 => Id;
[Projectable]
public int Computed2 => Id * 2;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Where(x => x.Computed1 == 1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task FilterOnComplexProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Where(x => x.Computed2 == 2);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectComplexProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed2);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task CombineSelectProjectableProperties()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed1 + x.Computed2);
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,2 @@
SELECT [e].[Id] + ([e].[Id] * 2)
FROM [Entity] AS [e]
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE ([e].[Id] * 2) = 2
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE [e].[Id] = 1
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatefullSimpleFunctionTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed1() => Id;
[Projectable]
public int Computed2() => Id * 2;
public int Test(int i) => i;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Where(x => x.Computed1() == 1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed1());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task FilterOnComplexProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Where(x => x.Computed2() == 2);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectComplexProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed2());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task CombineSelectProjectableProperties()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed1() + x.Computed2());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE 0 = 1
@@ -0,0 +1,4 @@
DECLARE @__argument_0 int = 1;
SELECT @__argument_0
FROM [Entity] AS [e]
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatelessComplexFunctionTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed(int argument1) => argument1;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>().AsQueryable()
.Where(x => x.Computed(0) == 1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(0));
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task PassInVariableArguments()
{
using var dbContext = new SampleDbContext<Entity>();
var argument = 1;
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(argument));
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE 0 = 1
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatelessPropertyTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed => 0;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>().AsQueryable()
.Where(x => x.Computed == 1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed);
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -0,0 +1,3 @@
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE 0 = 1
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class StatelessSimpleFunctionTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed() => 0;
}
[Fact]
public Task FilterOnProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>().AsQueryable()
.Where(x => x.Computed() == 1);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableProperty()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Computed());
return Verifier.Verify(query.ToQueryString());
}
}
}