Skip to content

Test case for #1180 and improve NullableType.ToString #2456

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 5 commits into from
Sep 10, 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
146 changes: 146 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//------------------------------------------------------------------------------
// <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 NHibernate.Cfg.MappingSchema;
using NHibernate.Criterion;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1180
{
using System.Threading.Tasks;
[KnownBug("NH-3847 (GH-1180)")]
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name, m => { m.Length(10); });
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}

[Test]
public async Task StringTypesAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// data
await (session.SaveAsync(new Entity {Name = "Alpha"}));
await (session.SaveAsync(new Entity {Name = "Beta"}));
await (session.SaveAsync(new Entity {Name = "Gamma"}));

await (transaction.CommitAsync());
}

// whenTrue is constant, whenFalse is property -> works even before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Like(nameof(Entity.Name), "B%")),
Projections.Constant("other"),
Projections.Property(nameof(Entity.Name)));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = await (tagCriteria.ListAsync());

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
}

// whenTrue is property, whenFalse is constant -> fails before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Like(nameof(Entity.Name), "B%"),
Projections.Property(nameof(Entity.Name)),
Projections.Constant("other"));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = await (tagCriteria.ListAsync());

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
}
}

[Test]
public async Task DecimalTypesAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
await (session.SaveAsync(new Entity {Amount = 3.14m}));
await (session.SaveAsync(new Entity {Amount = 42.13m}));
await (session.SaveAsync(new Entity {Amount = 17.99m}));

await (transaction.CommitAsync());
}

// whenTrue is constant, whenFalse is property -> works even before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Ge(nameof(Entity.Amount), 20m)),
Projections.Constant(20m),
Projections.Property(nameof(Entity.Amount)));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = await (tagCriteria.ListAsync());

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
}

// whenTrue is property, whenFalse is constant -> fails before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Ge(nameof(Entity.Amount), 20m),
Projections.Property(nameof(Entity.Amount)),
Projections.Constant(20m));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = await (tagCriteria.ListAsync());

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1180/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1180
{
internal class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual decimal Amount { get; set; }
}
}
135 changes: 135 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using NHibernate.Cfg.MappingSchema;
using NHibernate.Criterion;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1180
{
[KnownBug("NH-3847 (GH-1180)")]
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name, m => { m.Length(10); });
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}

[Test]
public void StringTypes()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// data
session.Save(new Entity {Name = "Alpha"});
session.Save(new Entity {Name = "Beta"});
session.Save(new Entity {Name = "Gamma"});

transaction.Commit();
}

// whenTrue is constant, whenFalse is property -> works even before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Like(nameof(Entity.Name), "B%")),
Projections.Constant("other"),
Projections.Property(nameof(Entity.Name)));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = tagCriteria.List();

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
}

// whenTrue is property, whenFalse is constant -> fails before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Like(nameof(Entity.Name), "B%"),
Projections.Property(nameof(Entity.Name)),
Projections.Constant("other"));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = tagCriteria.List();

Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
}
}

[Test]
public void DecimalTypes()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Entity {Amount = 3.14m});
session.Save(new Entity {Amount = 42.13m});
session.Save(new Entity {Amount = 17.99m});

transaction.Commit();
}

// whenTrue is constant, whenFalse is property -> works even before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Not(
Restrictions.Ge(nameof(Entity.Amount), 20m)),
Projections.Constant(20m),
Projections.Property(nameof(Entity.Amount)));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = tagCriteria.List();

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
}

// whenTrue is property, whenFalse is constant -> fails before the fix
using (var session = OpenSession())
{
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));

var conditionalProjection = Projections.Conditional(
Restrictions.Ge(nameof(Entity.Amount), 20m),
Projections.Property(nameof(Entity.Amount)),
Projections.Constant(20m));
tagCriteria.SetProjection(conditionalProjection);

// run query
var results = tagCriteria.List();

Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
}
}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate/Type/NullableType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ public override int GetHashCode()
return (SqlType.GetHashCode() / 2) + (Name.GetHashCode() / 2);
}

/// <summary>
/// Provides a more descriptive string representation by reporting the properties that are important for equality.
/// Useful in error messages.
/// </summary>
public override string ToString()
{
return $"{base.ToString()} (SqlType: {SqlType})";
}

#endregion
}
}