mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-25 20:09:00 +00:00
Rewrite of most internals
This commit is contained in:
+10
@@ -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]
|
||||
+6
@@ -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]
|
||||
@@ -8,14 +8,16 @@ using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using EntityFrameworkCore.Projections.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
|
||||
public partial class ComplexModelTests
|
||||
|
||||
[UsesVerify]
|
||||
public class ComplexModelTests
|
||||
{
|
||||
public class User
|
||||
{
|
||||
@@ -45,45 +47,27 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public DateTime RecordDate { get; set; }
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task ProjectOverNavigationProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<User>();
|
||||
|
||||
scenario.Fact("We can project over a projectable navigation property", () => {
|
||||
const string expectedQueryString =
|
||||
@"SELECT (
|
||||
SELECT TOP(1) [o].[RecordDate]
|
||||
FROM [Order] AS [o]
|
||||
WHERE [u].[Id] = [o].[UserId]
|
||||
ORDER BY [o].[RecordDate] DESC)
|
||||
FROM [User] AS [u]";
|
||||
var query = dbContext.Set<User>()
|
||||
.Select(x => x.LastOrder.RecordDate);
|
||||
|
||||
var query = dbContext.Set<User>()
|
||||
.Select(x => x.LastOrder.RecordDate);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task ProjectOverCollectionNavigationProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<User>();
|
||||
|
||||
scenario.Fact("We can project over a projectable navigation collection property", () => {
|
||||
const string expectedQueryString =
|
||||
@"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]";
|
||||
var query = dbContext.Set<User>()
|
||||
.SelectMany(x => x.Last2Orders)
|
||||
.Select(x => x.RecordDate);
|
||||
|
||||
var query = dbContext.Set<User>()
|
||||
.SelectMany(x => x.Last2Orders)
|
||||
.Select(x => x.RecordDate);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -11,6 +11,7 @@
|
||||
<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>
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests.ExtensionMethods
|
||||
{
|
||||
|
||||
public partial class ExtensionMethodTests
|
||||
public class Entity
|
||||
{
|
||||
public class Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -3,6 +3,9 @@
|
||||
public static class EntityExtensions
|
||||
{
|
||||
[Projectable]
|
||||
public static int Foo(this ExtensionMethodTests.Entity entity) => entity.Id + 1;
|
||||
public static int Squared(this int i) => i * i;
|
||||
|
||||
[Projectable]
|
||||
public static int Foo(this Entity entity) => entity.Id + 1;
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] * [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + 1
|
||||
FROM [Entity] AS [e]
|
||||
+17
-9
@@ -6,27 +6,35 @@ using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests.ExtensionMethods
|
||||
{
|
||||
|
||||
public partial class ExtensionMethodTests
|
||||
[UsesVerify]
|
||||
public class ExtensionMethodTests
|
||||
{
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task ExtensionOnPrimitive()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select on a projectable extension method", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Id.Squared());
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Foo());
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE ([e].[Id] + 1) = 2
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
DECLARE @__argument_0 int = 1;
|
||||
|
||||
SELECT [e].[Id] + @__argument_0
|
||||
FROM [Entity] AS [e]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + 1
|
||||
FROM [Entity] AS [e]
|
||||
+35
-35
@@ -8,11 +8,13 @@ using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using EntityFrameworkCore.Projections.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatefullComplexFunctionTests
|
||||
[UsesVerify]
|
||||
public class StatefullComplexFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -22,53 +24,51 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Computed(int argument) => Id + argument;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__argument_0 int = 1;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed(1) == 2);
|
||||
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE ([e].[Id] + @__argument_0) = 2";
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
var query = dbContext.Set<Entity>().AsQueryable()
|
||||
.Where(x => x.Computed(1) == 2);
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(1));
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__argument_0 int = 1;
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
SELECT [e].[Id] + @__argument_0
|
||||
FROM [Entity] AS [e]";
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.AsQueryable()
|
||||
.Select(x => x.Computed(1));
|
||||
[Fact]
|
||||
public Task PassInVariableArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
var argument = 1;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(argument));
|
||||
|
||||
scenario.Fact("We can pass in variables", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__argument_0 int = 1;
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
SELECT [e].[Id] + @__argument_0
|
||||
FROM [Entity] AS [e]";
|
||||
[Fact]
|
||||
public Task PassInReferenceArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var argument = 1;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(argument));
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(x.Id));
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + ([e].[Id] * 2)
|
||||
FROM [Entity] AS [e]
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE ([e].[Id] * 2) = 2
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE [e].[Id] = 1
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] * 2
|
||||
FROM [Entity] AS [e]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
@@ -7,11 +7,13 @@ using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatefullPropertyTests
|
||||
[UsesVerify]
|
||||
public class StatefullPropertyTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -24,56 +26,59 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Computed2 => Id * 2;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE [e].[Id] = 1";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed1 == 1);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed1 == 1);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task FilterOnComplexProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a more complex projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE ([e].[Id] * 2) = 2";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed2 == 2);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed2 == 2);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectComplexProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select a more complex projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id] * 2\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed2);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed2);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task CombineSelectProjectableProperties()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can combine multiple projectable properties", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id] + ([e].[Id] * 2)\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1 + x.Computed2);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1 + x.Computed2);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] + ([e].[Id] * 2)
|
||||
FROM [Entity] AS [e]
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE ([e].[Id] * 2) = 2
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE [e].[Id] = 1
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id] * 2
|
||||
FROM [Entity] AS [e]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
+40
-35
@@ -7,11 +7,13 @@ using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatefullSimpleFunctionTests
|
||||
[UsesVerify]
|
||||
public class StatefullSimpleFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -26,56 +28,59 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Test(int i) => i;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE [e].[Id] = 1";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed1() == 1);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed1() == 1);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1());
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1());
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task FilterOnComplexProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a more complex projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE ([e].[Id] * 2) = 2";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed2() == 2);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed2() == 2);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectComplexProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select a more complex projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id] * 2\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed2());
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed2());
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task CombineSelectProjectableProperties()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can combine multiple projectable properties", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id] + ([e].[Id] * 2)\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1() + x.Computed2());
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed1() + x.Computed2());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE 0 = 1
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
DECLARE @__argument_0 int = 1;
|
||||
|
||||
SELECT @__argument_0
|
||||
FROM [Entity] AS [e]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT 0
|
||||
FROM [Entity] AS [e]
|
||||
+27
-39
@@ -8,11 +8,13 @@ using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using EntityFrameworkCore.Projections.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessComplexFunctionTests
|
||||
[UsesVerify]
|
||||
public class StatelessComplexFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -22,53 +24,39 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Computed(int argument1) => argument1;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__p_0 bit = CAST(0 AS bit);
|
||||
var query = dbContext.Set<Entity>().AsQueryable()
|
||||
.Where(x => x.Computed(0) == 1);
|
||||
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE @__p_0 = CAST(1 AS bit)";
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
var query = dbContext.Set<Entity>().AsQueryable()
|
||||
.Where(x => x.Computed(0) == 1);
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(0));
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__argument1_0 int = 0;
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
SELECT @__argument1_0
|
||||
FROM [Entity] AS [e]";
|
||||
[Fact]
|
||||
public Task PassInVariableArguments()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.AsQueryable()
|
||||
.Select(x => x.Computed(0));
|
||||
var argument = 1;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(argument));
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
scenario.Fact("We can pass in variables", () => {
|
||||
const string expectedQueryString =
|
||||
@"DECLARE @__argument1_0 int = 0;
|
||||
|
||||
SELECT @__argument1_0
|
||||
FROM [Entity] AS [e]";
|
||||
|
||||
var argument = 0;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(argument));
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE 0 = 1
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT 0
|
||||
FROM [Entity] AS [e]
|
||||
@@ -7,11 +7,13 @@ using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessPropertyTests
|
||||
[UsesVerify]
|
||||
public class StatelessPropertyTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -21,29 +23,26 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Computed => 0;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE 0 = 1";
|
||||
var query = dbContext.Set<Entity>().AsQueryable()
|
||||
.Where(x => x.Computed == 1);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed == 1);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT 0\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
WHERE 0 = 1
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT 0
|
||||
FROM [Entity] AS [e]
|
||||
+17
-18
@@ -7,11 +7,13 @@ using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessSimpleFunctionTests
|
||||
[UsesVerify]
|
||||
public class StatelessSimpleFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
@@ -21,29 +23,26 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
public int Computed() => 0;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
[Fact]
|
||||
public Task FilterOnProjectableProperty()
|
||||
{
|
||||
// Setup
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can filter on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT [e].[Id]\r\nFROM [Entity] AS [e]\r\nWHERE 0 = 1";
|
||||
var query = dbContext.Set<Entity>().AsQueryable()
|
||||
.Where(x => x.Computed() == 1);
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed() == 1);
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
[Fact]
|
||||
public Task SelectProjectableProperty()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
scenario.Fact("We can select on a projectable property", () => {
|
||||
const string expectedQueryString = "SELECT 0\r\nFROM [Entity] AS [e]";
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed());
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => 0;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => 0;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => projectionTarget.Bar + projectionTarget.Bar + projectionTarget.Bar;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => @this.Bar + @this.Bar + @this.Bar;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression(int a, string b, object d) =>
|
||||
projectionTarget => a;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C ,int ,string ,object, int>> Expression =>
|
||||
(global::Foo.C @this,int a,string b,object d) => a;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression(int i) =>
|
||||
projectionTarget => i;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C ,int, int>> Expression =>
|
||||
(global::Foo.C @this,int i) => i;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => projectionTarget.Bar();
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => @this.Bar();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => projectionTarget.Bar;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => @this.Bar;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -8,7 +8,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.D, int>> Expression(D d) =>
|
||||
projectionTarget => 1;
|
||||
public static System.Linq.Expressions.Expression<System.Func<D, int>> Expression =>
|
||||
(D d) => 1;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using EntityFrameworkCore.Projections;
|
||||
using System;
|
||||
using Foo;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<int, int>> Expression =>
|
||||
(int i) => i;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -8,7 +8,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, D>> Expression() =>
|
||||
projectionTarget => projectionTarget.Dees.First();
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, D>> Expression =>
|
||||
(global::Foo.C @this) => @this.Dees.First();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_D_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C.D, int>> Expression() =>
|
||||
projectionTarget => projectionTarget.Bar + 1;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C.D, int>> Expression =>
|
||||
(global::Foo.C.D @this) => @this.Bar + 1;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => projectionTarget.Bar + 1;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => @this.Bar + 1;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => 1;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => 1;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace EntityFrameworkCore.Projections.Generated
|
||||
{
|
||||
public static class Foo_C_Foo
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
|
||||
projectionTarget => 1;
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression =>
|
||||
(global::Foo.C @this) => 1;
|
||||
}
|
||||
}
|
||||
+23
@@ -317,6 +317,29 @@ namespace Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ProjectableExtensionMethod2()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using System;
|
||||
using System.Linq;
|
||||
using EntityFrameworkCore.Projections;
|
||||
namespace Foo {
|
||||
static class C {
|
||||
[Projectable]
|
||||
public static int Foo(this int i) => i;
|
||||
}
|
||||
}
|
||||
");
|
||||
|
||||
var result = RunGenerator(compilation);
|
||||
|
||||
Assert.Empty(result.Diagnostics);
|
||||
Assert.Single(result.GeneratedTrees);
|
||||
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
|
||||
#region Helpers
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.Tests.Services
|
||||
{
|
||||
public class ProjectableExpressionReplacerTests
|
||||
{
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.Tests.Services
|
||||
{
|
||||
public class ProjectionExpressionResolverTests
|
||||
{
|
||||
//public Expression<Func<int, int>> Test1(int arg1)
|
||||
// => arg1 + 1;
|
||||
|
||||
[Fact]
|
||||
public void Foo1()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user