Checkpoint (still hacking about)

This commit is contained in:
Koen Bekkenutte
2021-05-28 03:07:16 +08:00
parent d808b69310
commit 0d7bec4bd6
30 changed files with 413 additions and 98 deletions
@@ -0,0 +1,11 @@
namespace EntityFrameworkCore.Projections.FunctionalTests.ExtensionMethods
{
public partial class ExtensionMethodTests
{
public class Entity
{
public int Id { get; set; }
}
}
}
@@ -0,0 +1,8 @@
namespace EntityFrameworkCore.Projections.FunctionalTests.ExtensionMethods
{
public static class EntityExtensions
{
[Projectable]
public static int Foo(this ExtensionMethodTests.Entity entity) => entity.Id + 1;
}
}
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
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.ExtensionMethods
{
public partial class ExtensionMethodTests
{
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
public void PlayScenario(ScenarioContext scenario)
{
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.Foo());
Assert.Equal(expectedQueryString, query.ToQueryString());
});
}
}
}
@@ -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.Projections.FunctionalTests.Helpers;
using EntityFrameworkCore.Projections.Services;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using Xunit;
namespace EntityFrameworkCore.Projections.FunctionalTests
{
public partial class StatefullComplexFunctionTests
{
public record Entity
{
public int Id { get; set; }
[Projectable]
public int Computed(int argument) => Id + argument;
}
[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 =
@"DECLARE @__argument_0 int = 1;
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE ([e].[Id] + @__argument_0) = 2";
var query = dbContext.Set<Entity>().AsQueryable()
.Where(x => x.Computed(1) == 2);
Assert.Equal(expectedQueryString, query.ToQueryString());
});
scenario.Fact("We can select on a projectable property", () => {
const string expectedQueryString =
@"DECLARE @__argument_0 int = 1;
SELECT [e].[Id] + @__argument_0
FROM [Entity] AS [e]";
var query = dbContext.Set<Entity>()
.AsQueryable()
.Select(x => x.Computed(1));
Assert.Equal(expectedQueryString, query.ToQueryString());
});
scenario.Fact("We can pass in variables", () => {
const string expectedQueryString =
@"DECLARE @__argument_0 int = 1;
SELECT [e].[Id] + @__argument_0
FROM [Entity] AS [e]";
var argument = 1;
var query = dbContext.Set<Entity>()
.Select(x => x.Computed(argument));
Assert.Equal(expectedQueryString, query.ToQueryString());
});
}
}
}
@@ -5,6 +5,7 @@ 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;
@@ -28,25 +29,39 @@ namespace EntityFrameworkCore.Projections.FunctionalTests
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";
const string expectedQueryString =
@"DECLARE @__p_0 bit = CAST(0 AS bit);
var query = dbContext.Set<Entity>()
SELECT [e].[Id]
FROM [Entity] AS [e]
WHERE @__p_0 = CAST(1 AS bit)";
var query = dbContext.Set<Entity>().AsQueryable()
.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]";
const string expectedQueryString =
@"DECLARE @__argument1_0 int = 0;
SELECT @__argument1_0
FROM [Entity] AS [e]";
var query = dbContext.Set<Entity>()
.AsQueryable()
.Select(x => x.Computed(0));
Assert.Equal(expectedQueryString, query.ToQueryString());
Assert.Equal(expectedQueryString, query.ToQueryString());
});
scenario.Fact("We can pass in variables", () => {
const string expectedQueryString = "SELECT 0\r\nFROM [Entity] AS [e]";
const string expectedQueryString =
@"DECLARE @__argument1_0 int = 0;
SELECT @__argument1_0
FROM [Entity] AS [e]";
var argument = 0;
var query = dbContext.Set<Entity>()