Skip to content

Commit

Permalink
Update to Entity Framework Core 3
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones committed Sep 27, 2019
1 parent e683fe0 commit 1347b20
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>Bricelam.EntityFrameworkCore.SqlServer.HierarchyId.Abstractions</AssemblyName>
<RootNamespace>Bricelam.EntityFrameworkCore</RootNamespace>
<Authors>Brice Lambson</Authors>
Expand All @@ -19,7 +19,7 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="dotMorten.Microsoft.SqlServer.Types" Version="1.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>

<IsPackable>false</IsPackable>

Expand All @@ -11,9 +11,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
134 changes: 93 additions & 41 deletions EFCore.SqlServer.HierarchyId.Test/QueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System;
using System.Linq;
using Bricelam.EntityFrameworkCore.Test.Models;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Bricelam.EntityFrameworkCore
Expand All @@ -17,90 +18,90 @@ public QueryTests()
}

[Fact]
public void GetLevel_works()
public void GetLevel_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 0
select p.Name);

Assert.Equal(
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 0"),
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(0 AS smallint)"),
condense(_db.Sql));

Assert.Equal(new[] { "Abraham" }, results);
}

[Fact]
public void IsDescendantOf_works()
public void IsDescendantOf_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 3
select p.Id.IsDescendantOf(p.Id.GetAncestor(1)));

Assert.Equal(
condense(@"SELECT [p].[Id].IsDescendantOf([p].[Id].GetAncestor(1)) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 3"),
condense(@"SELECT [p].[Id].IsDescendantOf([p].[Id].GetAncestor(1)) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(3 AS smallint)"),
condense(_db.Sql));

Assert.All(results, b => Assert.True(b));
}

[Fact]
public void GetAncestor_0_works()
public void GetAncestor_0_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 0
select p.Id.GetAncestor(0));

Assert.Equal(
condense(@"SELECT [p].[Id].GetAncestor(0) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 0"),
condense(@"SELECT [p].[Id].GetAncestor(0) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(0 AS smallint)"),
condense(_db.Sql));

Assert.All(results, h => Assert.Equal(HierarchyId.GetRoot(), h));
}

[Fact]
public void GetAncestor_1_works()
public void GetAncestor_1_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 1
select p.Id.GetAncestor(1));

Assert.Equal(
condense(@"SELECT [p].[Id].GetAncestor(1) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 1"),
condense(@"SELECT [p].[Id].GetAncestor(1) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(1 AS smallint)"),
condense(_db.Sql));

Assert.All(results, h => Assert.Equal(HierarchyId.GetRoot(), h));
}

[Fact]
public void GetAncestor_2_works()
public void GetAncestor_2_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 2
select p.Id.GetAncestor(2));

Assert.Equal(
condense(@"SELECT [p].[Id].GetAncestor(2) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 2"),
condense(@"SELECT [p].[Id].GetAncestor(2) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(2 AS smallint)"),
condense(_db.Sql));

Assert.All(results, h => Assert.Equal(HierarchyId.GetRoot(), h));
}

[Fact]
public void GetAncestor_3_works()
public void GetAncestor_3_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 3
select p.Id.GetAncestor(3));

Assert.Equal(
condense(@"SELECT [p].[Id].GetAncestor(3) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 3"),
condense(@"SELECT [p].[Id].GetAncestor(3) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(3 AS smallint)"),
condense(_db.Sql));

Assert.All(results, h => Assert.Equal(HierarchyId.GetRoot(), h));
Expand All @@ -115,70 +116,121 @@ where p.Id.GetLevel() == 0
select p.Id.GetAncestor(1));

Assert.Equal(
condense(@"SELECT [p].[Id].GetAncestor(1) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 0"),
condense(@"SELECT [p].[Id].GetAncestor(1) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(0 AS smallint)"),
condense(_db.Sql));

Assert.Equal(new HierarchyId[] { null }, results);
}

[Fact]
public void ToString_works()
public void GetDescendent_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 1
select p.Id.ToString());
where p.Id.GetLevel() == 0
select p.Id.GetDescendant(null, null));

Assert.Equal(
condense(@"SELECT [p].[Id].ToString() FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 1"),
condense(@"SELECT [p].[Id].GetDescendant(NULL, NULL) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(0 AS smallint)"),
condense(_db.Sql));

Assert.Equal(new[] { "/1/" }, results);
Assert.Equal(new[] { HierarchyId.Parse("/1/") }, results);
}

[Fact]
public void GetDescendent_works()
public void HierarchyId_can_be_sent_as_parameter()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 0
select p.Id.GetDescendant(null, null));
where p.Id == HierarchyId.Parse("/1/")
select p.Name);

Assert.Equal(
condense(@"SELECT [p].[Id].GetDescendant(NULL, NULL) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 0"),
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id] = '/1/'"),
condense(_db.Sql));

Assert.Equal(new[] { HierarchyId.Parse("/1/") }, results);
Assert.Equal(new[] { "Isaac" }, results);
}

//[Fact]
//public void GetRoot_works()
//{
// var results = Enumerable.ToList(
// from p in _db.Patriarchy
// where p.Id == HierarchyId.GetRoot()
// select p.Name);
[Fact]
public void HierarchyId_get_ancestor_of_level_is_root()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetAncestor(p.Id.GetLevel()) == HierarchyId.GetRoot() // HierarchyId.Parse("/1/") // HierarchyId.Parse(p.Id.ToString()).GetAncestor(HierarchyId.Parse(p.Id.ToString()).GetLevel())
select p.Name);

Assert.Equal(
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id].GetAncestor(CAST([p].[Id].GetLevel() AS int)) = '/'"),
condense(_db.Sql));

// Assert.Equal(
// condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id] = hierarchyid::GetRoot()"),
// condense(_db.Sql));
var all = Enumerable.ToList(
from p in _db.Patriarchy
select p.Name);

// Assert.Equal(new[] { "Abraham" }, results);
//}
Assert.Equal(all, results);
}

[Fact]
public void Parse_works()
public void HierarchyId_can_call_method_on_parameter()
{
var isaac = HierarchyId.Parse("/1/");

var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 0
select HierarchyId.Parse(p.Id.GetDescendant(null, null).ToString()));
where isaac.IsDescendantOf(p.Id)
select p.Name);

Assert.Equal(
condense(@"SELECT hierarchyid::Parse([p].[Id].GetDescendant(NULL, NULL).ToString()) FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = 0"),
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE @__isaac_0.IsDescendantOf([p].[Id]) = CAST(1 AS bit)"),
condense(_db.Sql));

Assert.Equal(new[] { HierarchyId.Parse("/1/") }, results);
Assert.Equal(new[] { "Abraham", "Isaac" }, results);
}

[Fact]
public void ToString_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id.GetLevel() == 1
select p.Id.ToString());

Assert.Equal(
condense(@"SELECT [p].[Id].ToString() FROM [Patriarchy] AS [p] WHERE [p].[Id].GetLevel() = CAST(1 AS smallint)"),
condense(_db.Sql));

Assert.Equal(new[] { "/1/" }, results);
}

[Fact]
public void ToString_can_translate_redux()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where EF.Functions.Like(p.Id.ToString(), "%/1/")
select p.Name);

Assert.Equal(
condense(@"SELECT [p].[Name] FROM [Patriarchy] AS [p] WHERE [p].[Id].ToString() LIKE N'%/1/'"),
condense(_db.Sql));

Assert.Equal(new[] { "Isaac", "Jacob", "Reuben" }, results);
}

[Fact]
public void Parse_can_translate()
{
var results = Enumerable.ToList(
from p in _db.Patriarchy
where p.Id == HierarchyId.GetRoot()
select HierarchyId.Parse(p.Id.ToString()));

Assert.Equal(
condense(@"SELECT hierarchyid::Parse([p].[Id].ToString()) FROM [Patriarchy] AS [p] WHERE [p].[Id] = '/'"),
condense(_db.Sql));

Assert.Equal(new[] { HierarchyId.Parse("/") }, results);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>Bricelam.EntityFrameworkCore.SqlServer.HierarchyId</AssemblyName>
<RootNamespace>Bricelam.EntityFrameworkCore</RootNamespace>
<Authors>Brice Lambson</Authors>
Expand All @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0-preview3-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators;
using Microsoft.EntityFrameworkCore.SqlServer.Storage;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@
using System.Linq;
using Bricelam.EntityFrameworkCore.Properties;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators;
using Microsoft.EntityFrameworkCore.SqlServer.Storage;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure
{
internal class SqlServerHierarchyIdOptionsExtension : IDbContextOptionsExtensionWithDebugInfo
internal class SqlServerHierarchyIdOptionsExtension : IDbContextOptionsExtension
{
public virtual string LogFragment
=> "using HierarchyId ";
public DbContextOptionsExtensionInfo _info;

public virtual bool ApplyServices(IServiceCollection services)
public DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this);

public virtual void ApplyServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServerHierarchyId();

return false;
}

public virtual long GetServiceProviderHashCode()
=> 0;

public virtual void PopulateDebugInfo(IDictionary<string, string> debugInfo)
=> debugInfo["SqlServer:" + nameof(SqlServerHierarchyIdDbContextOptionsBuilderExtensions.UseHierarchyId)] = "1";

public virtual void Validate(IDbContextOptions options)
{
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;
Expand All @@ -46,5 +39,25 @@ public virtual void Validate(IDbContextOptions options)
}
}
}

private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
public ExtensionInfo(IDbContextOptionsExtension extension)
: base(extension)
{
}

private new SqlServerHierarchyIdOptionsExtension Extension
=> (SqlServerHierarchyIdOptionsExtension)base.Extension;

public override bool IsDatabaseProvider => false;

public override long GetServiceProviderHashCode() => 0;

public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
=> debugInfo["SqlServer:" + nameof(SqlServerHierarchyIdDbContextOptionsBuilderExtensions.UseHierarchyId)] = "1";

public override string LogFragment => "using HierarchyId ";
}
}
}
Loading

0 comments on commit 1347b20

Please sign in to comment.