Skip to content

Fix invalid cast on nullable custom type with Linq #2438

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

Merged
merged 3 commits into from
Jul 22, 2020
Merged
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
144 changes: 144 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2437/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2437
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<UserMapping>();
mapper.AddMapping<UserSessionMapping>();
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var user = new User { UserCode = "gokhanabatay", IsOpen = true, UserName = "Gökhan Abatay" };
session.Save(user);

for (var i = 0; i < 10; i++)
{
var userSession = new UserSession
{
Claims = "My Claims",
ExpireDateTime = DateTime.Now.AddDays(1),
MbrId = 1,
OpenDate = DateTime.Now,
LocalIpAddress = "192.168.1.1",
RemoteIpAddress = "127.0.0.1",
LocalPort = 80 + i.ToString(),
RemotePort = 80 + i.ToString(),
DeviceId = "None",
UserCode = "gokhanabatay",
IsOpen = true
};

session.Save(userSession);
}

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from UserSession").ExecuteUpdate();
session.CreateQuery("delete from User").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task Get_DateCustomType_NullableDateValueEqualsAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = await (session.Query<UserSession>().Where(x => x.OpenDate.Value == DateTime.Now).ToListAsync());

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public async Task Get_DateTimeCustomType_NullableDateValueEqualsAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = await (session.Query<UserSession>().Where(x => x.ExpireDateTime.Value > DateTime.Now).ToListAsync());

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public async Task Get_DateCustomType_DateEqualsAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = await (session.Query<UserSession>().Where(x => x.OpenDate == DateTime.Now).ToListAsync());

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public async Task Get_DateTimeCustomType_DateEqualsAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = await (session.Query<UserSession>().Where(x => x.ExpireDateTime > DateTime.Now).ToListAsync());

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public async Task Get_BooleanCustomTypeAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var results = await (session.Query<UserSession>()
.Where(x => x.OpenDate == DateTime.Now)
.Select(x => new NullableBooleanResult() {IsOpen = x.User.IsOpen})
.ToListAsync());

Assert.That(results, Has.Count.EqualTo(10));
}
}

public class NullableBooleanResult
{
public bool? IsOpen { get; set; }
}
}
}
132 changes: 132 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2437/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2437
{
[TestFixture]
public class Fixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<UserMapping>();
mapper.AddMapping<UserSessionMapping>();
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var user = new User { UserCode = "gokhanabatay", IsOpen = true, UserName = "Gökhan Abatay" };
session.Save(user);

for (var i = 0; i < 10; i++)
{
var userSession = new UserSession
{
Claims = "My Claims",
ExpireDateTime = DateTime.Now.AddDays(1),
MbrId = 1,
OpenDate = DateTime.Now,
LocalIpAddress = "192.168.1.1",
RemoteIpAddress = "127.0.0.1",
LocalPort = 80 + i.ToString(),
RemotePort = 80 + i.ToString(),
DeviceId = "None",
UserCode = "gokhanabatay",
IsOpen = true
};

session.Save(userSession);
}

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from UserSession").ExecuteUpdate();
session.CreateQuery("delete from User").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void Get_DateCustomType_NullableDateValueEquals()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = session.Query<UserSession>().Where(x => x.OpenDate.Value == DateTime.Now).ToList();

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public void Get_DateTimeCustomType_NullableDateValueEquals()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = session.Query<UserSession>().Where(x => x.ExpireDateTime.Value > DateTime.Now).ToList();

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public void Get_DateCustomType_DateEquals()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = session.Query<UserSession>().Where(x => x.OpenDate == DateTime.Now).ToList();

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public void Get_DateTimeCustomType_DateEquals()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = session.Query<UserSession>().Where(x => x.ExpireDateTime > DateTime.Now).ToList();

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public void Get_BooleanCustomType()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var results = session.Query<UserSession>()
.Where(x => x.OpenDate == DateTime.Now)
.Select(x => new NullableBooleanResult() {IsOpen = x.User.IsOpen})
.ToList();

Assert.That(results, Has.Count.EqualTo(10));
}
}

public class NullableBooleanResult
{
public bool? IsOpen { get; set; }
}
}
}
Loading