Skip to content

Async #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using NHibernate.AspNet.Identity;
using NHibernate.AspNet.Identity;

namespace NHibernate.AspNet.Identity.Tests.Models
{
public class ApplicationUser : IdentityUser
{
}

public class ApplicationRole : IdentityRole
{
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props')" />
<Import Project="..\..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\..\packages\NUnit.3.10.1\build\NUnit.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -19,6 +21,8 @@
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -70,8 +74,8 @@
<Reference Include="NHibernate, Version=5.0.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<HintPath>..\..\packages\NHibernate.5.0.0\lib\net461\NHibernate.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\..\packages\NUnit.3.10.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="Remotion.Linq, Version=2.1.0.0, Culture=neutral, PublicKeyToken=fee00910d6e5f53b, processorArchitecture=MSIL">
<HintPath>..\..\packages\Remotion.Linq.2.1.2\lib\net45\Remotion.Linq.dll</HintPath>
Expand Down Expand Up @@ -104,6 +108,7 @@
<ItemGroup>
<Compile Include="Models\Foo.cs" />
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="RoleStoreTest.cs" />
<Compile Include="UserStoreTest.cs" />
<Compile Include="MapTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -149,6 +154,13 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\System.Data.SQLite.Core.1.0.96.0\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\..\packages\System.Data.SQLite.Core.1.0.96.0\build\net451\System.Data.SQLite.Core.targets')" />
<Import Project="..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets" Condition="Exists('..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit.3.10.1\build\NUnit.props'))" />
<Error Condition="!Exists('..\..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
123 changes: 123 additions & 0 deletions source/NHibernate.AspNet.Identity.Tests/RoleStoreTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Microsoft.AspNet.Identity;
using NHibernate.AspNet.Identity.Tests.Models;
using NUnit.Framework;
using System;
using System.Linq;
using System.Security.Claims;
using System.Transactions;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestMethod = NUnit.Framework.TestAttribute;

namespace NHibernate.AspNet.Identity.Tests
{
[TestClass]
public class RoleStoreTest
{
ISession _session;

[TestInitialize]
public void Initialize()
{
var factory = SessionFactoryProvider.Instance.SessionFactory;
_session = factory.OpenSession();
SessionFactoryProvider.Instance.BuildSchema();
}

[TestCleanup]
public void Cleanup()
{
_session.Close();
_session.Dispose();
}

[TestMethod]
public void WhenCreateRole()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
var role = new ApplicationRole() { Name = "RealRoleName" };

using (var transaction = new TransactionScope())
{
var result = roleManager.CreateAsync(role).GetAwaiter().GetResult();
transaction.Complete();
Assert.AreEqual(0, result.Errors.Count());
}

var actual = _session.Query<ApplicationRole>().FirstOrDefault(x => x.Name == role.Name);

Assert.IsNotNull(actual);
Assert.AreEqual(role.Name, actual.Name);
}

[TestMethod]
public void WhenDeleteRole()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
var role = new ApplicationRole() { Name = "RealRoleName" };

using (var transaction = new TransactionScope())
{
var result = roleManager.CreateAsync(role).GetAwaiter().GetResult();
transaction.Complete();
Assert.AreEqual(0, result.Errors.Count());
}

using (var transaction = new TransactionScope())
{
var result = roleManager.DeleteAsync(role).GetAwaiter().GetResult();
transaction.Complete();
Assert.AreEqual(0, result.Errors.Count());
}

var actual = _session.Query<ApplicationRole>().FirstOrDefault(x => x.Name == role.Name);

Assert.IsNull(actual);
}

[TestMethod]
public void FindByName()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
roleManager.Create(new ApplicationRole() { Name = "test" });
var x = roleManager.FindByName("test");
Assert.IsNotNull(x);
}

[TestMethod]
public void FindByNameIsCaseInsensitive()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
roleManager.Create(new ApplicationRole() { Name = "test" });
var x = roleManager.FindByName("TeSt");
Assert.IsNotNull(x);
}

[TestMethod]
public void FindByNameNotExisting()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
var x = roleManager.FindByName("xxx");
Assert.IsNull(x);
}

[TestMethod]
public void FindById()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
var role = new ApplicationRole() { Name = "test" };
roleManager.Create(role);
var x = roleManager.FindById(role.Id);
Assert.IsNotNull(x);
}

[TestMethod]
public void FindByIdNotExisting()
{
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
var x = roleManager.FindById(new Guid().ToString());
Assert.IsNull(x);
}
}
}
31 changes: 17 additions & 14 deletions source/NHibernate.AspNet.Identity.Tests/SessionFactoryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System;
using System.IO;
using System.Linq;
using NHibernate.AspNet.Identity.DomainModel;
using NHibernate.AspNet.Identity.Tests.Models;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

namespace NHibernate.AspNet.Identity.Tests
{
public sealed class SessionFactoryProvider
{
private static volatile SessionFactoryProvider _instance;
private static object _syncRoot = new Object();
private static object _syncRoot = new object();

private Configuration _configuration;

Expand All @@ -26,19 +27,20 @@ private SessionFactoryProvider()
{
Name = "NHibernate.AspNet.Identity";

var baseEntityToIgnore = new[] {
typeof(SharpArch.Domain.DomainModel.Entity),
typeof(EntityWithTypedId<int>),
typeof(EntityWithTypedId<string>),
var baseEntityToIgnore = new[] {
typeof(SharpArch.Domain.DomainModel.Entity),
typeof(EntityWithTypedId<int>),
typeof(EntityWithTypedId<string>),
};

var allEntities = new[] {
typeof(IdentityUser),
typeof(ApplicationUser),
typeof(IdentityRole),
typeof(IdentityUserLogin),
typeof(IdentityUserClaim),
typeof(Foo),
var allEntities = new[] {
typeof(IdentityUser),
typeof(ApplicationUser),
typeof(ApplicationRole),
typeof(IdentityRole),
typeof(IdentityUserLogin),
typeof(IdentityUserClaim),
typeof(Foo),
};

var mapper = new ConventionModelMapper();
Expand All @@ -52,7 +54,8 @@ private SessionFactoryProvider()
var mapping = mapper.CompileMappingForEach(allEntities);

_configuration = new Configuration();
_configuration.Configure("sqlite-nhibernate-config.xml");
// nunit3 change: the directory is not set by default and must be retrieved from TestContext
_configuration.Configure(Path.Combine(TestContext.CurrentContext.TestDirectory, "sqlite-nhibernate-config.xml"));
foreach (var map in mapping)
{
Console.WriteLine(map.AsString());
Expand Down
Loading