mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-08-15 18:44:57 +00:00
Rebranded to EntityFrameworkCore.Projectables
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||
<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>
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Tests.Extensions
|
||||
{
|
||||
public class TypeExtensionTests
|
||||
{
|
||||
class InnerType
|
||||
{
|
||||
public class SubsequentlyInnerType
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetNestedTypePath_OuterType_Returns1Entry()
|
||||
{
|
||||
var subject = typeof(TypeExtensionTests);
|
||||
|
||||
var result = subject.GetNestedTypePath();
|
||||
|
||||
Assert.Single(result);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void GetNestedTypePath_InnerType_Returns2Entries()
|
||||
{
|
||||
var subject = typeof(InnerType);
|
||||
|
||||
var result = subject.GetNestedTypePath();
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetNestedTypePath_SubsequentlyInnerType_Returns3Entries()
|
||||
{
|
||||
var subject = typeof(InnerType.SubsequentlyInnerType);
|
||||
|
||||
var result = subject.GetNestedTypePath();
|
||||
|
||||
Assert.Equal(3, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetNestedTypePath_SubsequentlyInnerType_ReturnsTypesInOrder()
|
||||
{
|
||||
var subject = typeof(InnerType.SubsequentlyInnerType);
|
||||
|
||||
var result = subject.GetNestedTypePath();
|
||||
|
||||
Assert.Equal(typeof(TypeExtensionTests), result.First());
|
||||
Assert.Equal(typeof(InnerType.SubsequentlyInnerType), result.Last());
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Tests.Services
|
||||
{
|
||||
public class ExpressionArgumentReplacerTests
|
||||
{
|
||||
[Fact]
|
||||
public void VisitParameter_MapsParametersWithArguments()
|
||||
{
|
||||
var parameter = Expression.Parameter(typeof(int));
|
||||
var argument = Expression.Constant(1);
|
||||
var subject = new ExpressionArgumentReplacer(new[] { (parameter, (Expression)argument) });
|
||||
|
||||
var result = subject.Visit(parameter);
|
||||
|
||||
Assert.Equal(argument, result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Tests.Services
|
||||
{
|
||||
public class ProjectableExpressionReplacerTests
|
||||
{
|
||||
public class ProjectableExpressionResolverStub : IProjectionExpressionResolver
|
||||
{
|
||||
readonly Func<MemberInfo, LambdaExpression> _implementation;
|
||||
|
||||
public ProjectableExpressionResolverStub(Func<MemberInfo, LambdaExpression> implementation)
|
||||
{
|
||||
_implementation = implementation;
|
||||
}
|
||||
|
||||
public LambdaExpression FindGeneratedExpression(MemberInfo projectableMemberInfo) => _implementation(projectableMemberInfo);
|
||||
}
|
||||
|
||||
class Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int SimpleProperty => 0;
|
||||
|
||||
[Projectable]
|
||||
public int SimpleMethod() => 0;
|
||||
|
||||
[Projectable]
|
||||
public int SimpleMethodWithArguments(int a, object b) => 0;
|
||||
|
||||
[Projectable]
|
||||
public int SimpleStatefullProperty => Id;
|
||||
|
||||
[Projectable]
|
||||
public int SimpleStatefullMethod() => Id;
|
||||
|
||||
[Projectable]
|
||||
public static int SimpleStaticMethod() => 0;
|
||||
|
||||
[Projectable]
|
||||
public static int SimpleStaticMethodWithArguments(int a, Entity b) => 0;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleProperty()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => x.SimpleProperty;
|
||||
Expression<Func<Entity, int>> expected = x => 0;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleMethod()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => x.SimpleMethod();
|
||||
Expression<Func<Entity, int>> expected = x => 0;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleMethodWithArguments()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => x.SimpleMethodWithArguments(1, true);
|
||||
Expression<Func<Entity, int>> expected = x => 0;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleStatefullProperty()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => x.SimpleStatefullProperty;
|
||||
Expression<Func<Entity, int>> expected = x => x.Id;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleStatefullMethod()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => x.SimpleStatefullMethod();
|
||||
Expression<Func<Entity, int>> expected = x => x.Id;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleStaticMethod()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => Entity.SimpleStaticMethod();
|
||||
Expression<Func<Entity, int>> expected = x => 0;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VisitMember_SimpleStaticMethodWithArguments()
|
||||
{
|
||||
Expression<Func<Entity, int>> input = x => Entity.SimpleStaticMethodWithArguments(0, x);
|
||||
Expression<Func<Entity, int>> expected = x => 0;
|
||||
|
||||
var resolver = new ProjectableExpressionResolverStub(
|
||||
x => expected
|
||||
);
|
||||
var subject = new ProjectableExpressionReplacer(resolver);
|
||||
|
||||
var actual = subject.Visit(input);
|
||||
|
||||
Assert.Equal(expected.ToString(), actual.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Tests.Services
|
||||
{
|
||||
public class ProjectionExpressionClassNameGeneratorTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("ns", new string[] { "a" }, "m", "ns_a_m")]
|
||||
[InlineData("ns", new string[] { "a", "b" }, "m", "ns_a_b_m")]
|
||||
[InlineData(null, new string[] { "a" }, "m", "_a_m")]
|
||||
public void GenerateName(string? namespaceName, string[] nestedTypeNames, string memberName, string expected)
|
||||
{
|
||||
var result = ProjectionExpressionClassNameGenerator.GenerateName(namespaceName, nestedTypeNames, memberName);
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GeneratedFullName()
|
||||
{
|
||||
var expected = $"{ProjectionExpressionClassNameGenerator.Namespace}.{ProjectionExpressionClassNameGenerator.GenerateName("a", new[] { "b" }, "m")}";
|
||||
var result = ProjectionExpressionClassNameGenerator.GenerateFullName("a", new [] { "b" }, "m");
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
+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.Projectables.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