mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-26 04:09:15 +00:00
Moved functional tests to their correct place
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using EntityFrameworkCore.Projections.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
|
||||
public partial 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.Projections.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.Projections.FunctionalTests.ComplexModelTests.Order> Last2Orders =>
|
||||
Orders.OrderByDescending(x => x.RecordDate).Take(2);
|
||||
|
||||
}
|
||||
|
||||
public class Order
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public DateTime RecordDate { get; set; }
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<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="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.Projections.Generator\EntityFrameworkCore.Projections.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections\EntityFrameworkCore.Projections.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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.Projections.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.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.UseProjections();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<TEntity>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatefullPropertyTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int Computed1 => Id;
|
||||
|
||||
[Projectable]
|
||||
public int Computed2 => Id * 2;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
// 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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial 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;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
// 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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessComplexFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int Computed(int argument1) => argument1;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
// 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 0 = 1";
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed(0) == 1);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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(0));
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
scenario.Fact("We can pass in variables", () => {
|
||||
const string expectedQueryString = "SELECT 0\r\nFROM [Entity] AS [e]";
|
||||
|
||||
var argument = 0;
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.Computed(argument));
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessPropertyTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int Computed => 0;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
// 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 0 = 1";
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed == 1);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScenarioTests;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projections.FunctionalTests
|
||||
{
|
||||
public partial class StatelessSimpleFunctionTests
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int Computed() => 0;
|
||||
}
|
||||
|
||||
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
|
||||
public void PlayScenario(ScenarioContext scenario)
|
||||
{
|
||||
// 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 0 = 1";
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Where(x => x.Computed() == 1);
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
|
||||
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());
|
||||
|
||||
Assert.Equal(expectedQueryString, query.ToQueryString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user