From 92b44b0a09b8698a2bd996ebdfa5c482b6127692 Mon Sep 17 00:00:00 2001 From: Emil Tzvetkov Date: Tue, 28 Jul 2020 13:17:17 +0300 Subject: [PATCH 1/5] GH-1180 add descriptive NullableType.ToString --- src/NHibernate/Type/NullableType.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/NHibernate/Type/NullableType.cs b/src/NHibernate/Type/NullableType.cs index e84b6df48da..e51849d2185 100644 --- a/src/NHibernate/Type/NullableType.cs +++ b/src/NHibernate/Type/NullableType.cs @@ -377,6 +377,15 @@ public override int GetHashCode() return (SqlType.GetHashCode() / 2) + (Name.GetHashCode() / 2); } + /// + /// Provides a more descriptive string representation by reporting the properties that are important for equality. + /// Useful in error messages. + /// + public override string ToString() + { + return string.Format("{0}(SqlType: {1})", Name, SqlType); + } + #endregion } } From 79c3469e82e7630d7ca2d949b36deb652833a3a8 Mon Sep 17 00:00:00 2001 From: Emil Tzvetkov Date: Tue, 28 Jul 2020 13:17:37 +0300 Subject: [PATCH 2/5] GH-1180 make SqlType Equals symmetric --- .../NHSpecificTest/GH1180/FixtureByCode.cs | 140 ++++++++++++++++++ .../NHSpecificTest/GH1180/Entity.cs | 11 ++ .../NHSpecificTest/GH1180/FixtureByCode.cs | 129 ++++++++++++++++ src/NHibernate/SqlTypes/SqlType.cs | 6 +- 4 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs create mode 100644 src/NHibernate.Test/NHSpecificTest/GH1180/Entity.cs create mode 100644 src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs new file mode 100644 index 00000000000..be1b8b40995 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs @@ -0,0 +1,140 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using NHibernate.Cfg.MappingSchema; +using NHibernate.Criterion; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1180 +{ + using System.Threading.Tasks; + [TestFixture] + public class ByCodeFixtureAsync : TestCaseMappingByCode + { + protected override HbmMapping GetMappings() + { + var mapper = new ModelMapper(); + + mapper.Class(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(); + } + + [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 (session.FlushAsync()); + + // whenTrue is constant, whenFalse is property -> works even before the fix + { + 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 + { + 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()) + { + // data + { + 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 (session.FlushAsync()); + + // whenTrue is constant, whenFalse is property -> works even before the fix + { + 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 + { + 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 })); + } + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1180/Entity.cs b/src/NHibernate.Test/NHSpecificTest/GH1180/Entity.cs new file mode 100644 index 00000000000..bf16619beb8 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1180/Entity.cs @@ -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; } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs b/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs new file mode 100644 index 00000000000..18eb826ca07 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs @@ -0,0 +1,129 @@ +using NHibernate.Cfg.MappingSchema; +using NHibernate.Criterion; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1180 +{ + [TestFixture] + public class ByCodeFixture : TestCaseMappingByCode + { + protected override HbmMapping GetMappings() + { + var mapper = new ModelMapper(); + + mapper.Class(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(); + } + + [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" }); + } + + session.Flush(); + + // whenTrue is constant, whenFalse is property -> works even before the fix + { + 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 + { + 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()) + { + // data + { + session.Save(new Entity { Amount = 3.14m }); + session.Save(new Entity { Amount = 42.13m }); + session.Save(new Entity { Amount = 17.99m }); + } + + session.Flush(); + + // whenTrue is constant, whenFalse is property -> works even before the fix + { + 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 + { + 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 })); + } + } + } + } +} diff --git a/src/NHibernate/SqlTypes/SqlType.cs b/src/NHibernate/SqlTypes/SqlType.cs index c32c377723c..55f758f7ebb 100644 --- a/src/NHibernate/SqlTypes/SqlType.cs +++ b/src/NHibernate/SqlTypes/SqlType.cs @@ -130,15 +130,15 @@ public bool Equals(SqlType rhsSqlType) return false; } - if (LengthDefined) + if (LengthDefined && rhsSqlType.LengthDefined) { return (DbType.Equals(rhsSqlType.DbType)) && (Length == rhsSqlType.Length); } - if (PrecisionDefined) + if (PrecisionDefined && rhsSqlType.PrecisionDefined) { return (DbType.Equals(rhsSqlType.DbType)) && (Precision == rhsSqlType.Precision) && (Scale == rhsSqlType.Scale); } - if (ScaleDefined) + if (ScaleDefined && rhsSqlType.ScaleDefined) { return DbType.Equals(rhsSqlType.DbType) && Scale == rhsSqlType.Scale; } From 81b8c86e29f5a704f2acdfbbb541cc4830f885e7 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Thu, 10 Sep 2020 11:52:39 +0300 Subject: [PATCH 3/5] Revert SqlType changes --- src/NHibernate/SqlTypes/SqlType.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NHibernate/SqlTypes/SqlType.cs b/src/NHibernate/SqlTypes/SqlType.cs index 55f758f7ebb..c32c377723c 100644 --- a/src/NHibernate/SqlTypes/SqlType.cs +++ b/src/NHibernate/SqlTypes/SqlType.cs @@ -130,15 +130,15 @@ public bool Equals(SqlType rhsSqlType) return false; } - if (LengthDefined && rhsSqlType.LengthDefined) + if (LengthDefined) { return (DbType.Equals(rhsSqlType.DbType)) && (Length == rhsSqlType.Length); } - if (PrecisionDefined && rhsSqlType.PrecisionDefined) + if (PrecisionDefined) { return (DbType.Equals(rhsSqlType.DbType)) && (Precision == rhsSqlType.Precision) && (Scale == rhsSqlType.Scale); } - if (ScaleDefined && rhsSqlType.ScaleDefined) + if (ScaleDefined) { return DbType.Equals(rhsSqlType.DbType) && Scale == rhsSqlType.Scale; } From 3440be0b43f770610ff21fa2de8f8207b6f28a81 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Thu, 10 Sep 2020 11:56:52 +0300 Subject: [PATCH 4/5] Keep full type name in ToString --- src/NHibernate/Type/NullableType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NHibernate/Type/NullableType.cs b/src/NHibernate/Type/NullableType.cs index e51849d2185..883894ee971 100644 --- a/src/NHibernate/Type/NullableType.cs +++ b/src/NHibernate/Type/NullableType.cs @@ -383,7 +383,7 @@ public override int GetHashCode() /// public override string ToString() { - return string.Format("{0}(SqlType: {1})", Name, SqlType); + return $"{base.ToString()} (SqlType: {SqlType})"; } #endregion From c03b144a84124377acbfd3cb27ed12d898d6cd6b Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Thu, 10 Sep 2020 12:03:50 +0300 Subject: [PATCH 5/5] Tests Clean up --- .../NHSpecificTest/GH1180/FixtureByCode.cs | 140 +++++++++--------- .../NHSpecificTest/GH1180/FixtureByCode.cs | 140 +++++++++--------- 2 files changed, 146 insertions(+), 134 deletions(-) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs index be1b8b40995..baf5bbacdbb 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1180/FixtureByCode.cs @@ -16,6 +16,7 @@ namespace NHibernate.Test.NHSpecificTest.GH1180 { using System.Threading.Tasks; + [KnownBug("NH-3847 (GH-1180)")] [TestFixture] public class ByCodeFixtureAsync : TestCaseMappingByCode { @@ -33,6 +34,16 @@ protected override HbmMapping GetMappings() 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() { @@ -40,48 +51,46 @@ public async Task StringTypesAsync() 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 (session.FlushAsync()); - - // whenTrue is constant, whenFalse is property -> works even before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + await (session.SaveAsync(new Entity {Name = "Alpha"})); + await (session.SaveAsync(new Entity {Name = "Beta"})); + await (session.SaveAsync(new Entity {Name = "Gamma"})); - var conditionalProjection = Projections.Conditional( - Restrictions.Not( - Restrictions.Like(nameof(Entity.Name), "B%")), - Projections.Constant("other"), - Projections.Property(nameof(Entity.Name))); - tagCriteria.SetProjection(conditionalProjection); + await (transaction.CommitAsync()); + } - // run query + // whenTrue is constant, whenFalse is property -> works even before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - var results = await (tagCriteria.ListAsync()); + var conditionalProjection = Projections.Conditional( + Restrictions.Not( + Restrictions.Like(nameof(Entity.Name), "B%")), + Projections.Constant("other"), + Projections.Property(nameof(Entity.Name))); + tagCriteria.SetProjection(conditionalProjection); - Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" })); - } + // run query + var results = await (tagCriteria.ListAsync()); - // whenTrue is property, whenFalse is constant -> fails before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"})); + } - var conditionalProjection = Projections.Conditional( - Restrictions.Like(nameof(Entity.Name), "B%"), - Projections.Property(nameof(Entity.Name)), - Projections.Constant("other")); - tagCriteria.SetProjection(conditionalProjection); + // whenTrue is property, whenFalse is constant -> fails before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - // run query + var conditionalProjection = Projections.Conditional( + Restrictions.Like(nameof(Entity.Name), "B%"), + Projections.Property(nameof(Entity.Name)), + Projections.Constant("other")); + tagCriteria.SetProjection(conditionalProjection); - var results = await (tagCriteria.ListAsync()); + // run query + var results = await (tagCriteria.ListAsync()); - Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" })); - } + Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"})); } } @@ -91,49 +100,46 @@ public async Task DecimalTypesAsync() using (var session = OpenSession()) using (var transaction = session.BeginTransaction()) { - // data - { - 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 (session.FlushAsync()); - - // whenTrue is constant, whenFalse is property -> works even before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + await (session.SaveAsync(new Entity {Amount = 3.14m})); + await (session.SaveAsync(new Entity {Amount = 42.13m})); + await (session.SaveAsync(new Entity {Amount = 17.99m})); - var conditionalProjection = Projections.Conditional( - Restrictions.Not( - Restrictions.Ge(nameof(Entity.Amount), 20m)), - Projections.Constant(20m), - Projections.Property(nameof(Entity.Amount))); - tagCriteria.SetProjection(conditionalProjection); + await (transaction.CommitAsync()); + } - // run query + // whenTrue is constant, whenFalse is property -> works even before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - var results = await (tagCriteria.ListAsync()); + var conditionalProjection = Projections.Conditional( + Restrictions.Not( + Restrictions.Ge(nameof(Entity.Amount), 20m)), + Projections.Constant(20m), + Projections.Property(nameof(Entity.Amount))); + tagCriteria.SetProjection(conditionalProjection); - Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m })); - } + // run query + var results = await (tagCriteria.ListAsync()); - // whenTrue is property, whenFalse is constant -> fails before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m})); + } - var conditionalProjection = Projections.Conditional( - Restrictions.Ge(nameof(Entity.Amount), 20m), - Projections.Property(nameof(Entity.Amount)), - Projections.Constant(20m)); - tagCriteria.SetProjection(conditionalProjection); + // whenTrue is property, whenFalse is constant -> fails before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - // run query + var conditionalProjection = Projections.Conditional( + Restrictions.Ge(nameof(Entity.Amount), 20m), + Projections.Property(nameof(Entity.Amount)), + Projections.Constant(20m)); + tagCriteria.SetProjection(conditionalProjection); - var results = await (tagCriteria.ListAsync()); + // run query + var results = await (tagCriteria.ListAsync()); - Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m })); - } + Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m})); } } } diff --git a/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs b/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs index 18eb826ca07..a7590840459 100644 --- a/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs +++ b/src/NHibernate.Test/NHSpecificTest/GH1180/FixtureByCode.cs @@ -5,6 +5,7 @@ namespace NHibernate.Test.NHSpecificTest.GH1180 { + [KnownBug("NH-3847 (GH-1180)")] [TestFixture] public class ByCodeFixture : TestCaseMappingByCode { @@ -22,6 +23,16 @@ protected override HbmMapping GetMappings() 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() { @@ -29,48 +40,46 @@ public void StringTypes() using (var transaction = session.BeginTransaction()) { // data - { - session.Save(new Entity { Name = "Alpha" }); - session.Save(new Entity { Name = "Beta" }); - session.Save(new Entity { Name = "Gamma" }); - } - - session.Flush(); - - // whenTrue is constant, whenFalse is property -> works even before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + session.Save(new Entity {Name = "Alpha"}); + session.Save(new Entity {Name = "Beta"}); + session.Save(new Entity {Name = "Gamma"}); - var conditionalProjection = Projections.Conditional( - Restrictions.Not( - Restrictions.Like(nameof(Entity.Name), "B%")), - Projections.Constant("other"), - Projections.Property(nameof(Entity.Name))); - tagCriteria.SetProjection(conditionalProjection); + transaction.Commit(); + } - // run query + // whenTrue is constant, whenFalse is property -> works even before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - var results = tagCriteria.List(); + var conditionalProjection = Projections.Conditional( + Restrictions.Not( + Restrictions.Like(nameof(Entity.Name), "B%")), + Projections.Constant("other"), + Projections.Property(nameof(Entity.Name))); + tagCriteria.SetProjection(conditionalProjection); - Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" })); - } + // run query + var results = tagCriteria.List(); - // whenTrue is property, whenFalse is constant -> fails before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"})); + } - var conditionalProjection = Projections.Conditional( - Restrictions.Like(nameof(Entity.Name), "B%"), - Projections.Property(nameof(Entity.Name)), - Projections.Constant("other")); - tagCriteria.SetProjection(conditionalProjection); + // whenTrue is property, whenFalse is constant -> fails before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - // run query + var conditionalProjection = Projections.Conditional( + Restrictions.Like(nameof(Entity.Name), "B%"), + Projections.Property(nameof(Entity.Name)), + Projections.Constant("other")); + tagCriteria.SetProjection(conditionalProjection); - var results = tagCriteria.List(); + // run query + var results = tagCriteria.List(); - Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" })); - } + Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"})); } } @@ -80,49 +89,46 @@ public void DecimalTypes() using (var session = OpenSession()) using (var transaction = session.BeginTransaction()) { - // data - { - session.Save(new Entity { Amount = 3.14m }); - session.Save(new Entity { Amount = 42.13m }); - session.Save(new Entity { Amount = 17.99m }); - } - - session.Flush(); - - // whenTrue is constant, whenFalse is property -> works even before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + session.Save(new Entity {Amount = 3.14m}); + session.Save(new Entity {Amount = 42.13m}); + session.Save(new Entity {Amount = 17.99m}); - var conditionalProjection = Projections.Conditional( - Restrictions.Not( - Restrictions.Ge(nameof(Entity.Amount), 20m)), - Projections.Constant(20m), - Projections.Property(nameof(Entity.Amount))); - tagCriteria.SetProjection(conditionalProjection); + transaction.Commit(); + } - // run query + // whenTrue is constant, whenFalse is property -> works even before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - var results = tagCriteria.List(); + var conditionalProjection = Projections.Conditional( + Restrictions.Not( + Restrictions.Ge(nameof(Entity.Amount), 20m)), + Projections.Constant(20m), + Projections.Property(nameof(Entity.Amount))); + tagCriteria.SetProjection(conditionalProjection); - Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m })); - } + // run query + var results = tagCriteria.List(); - // whenTrue is property, whenFalse is constant -> fails before the fix - { - ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); + Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m})); + } - var conditionalProjection = Projections.Conditional( - Restrictions.Ge(nameof(Entity.Amount), 20m), - Projections.Property(nameof(Entity.Amount)), - Projections.Constant(20m)); - tagCriteria.SetProjection(conditionalProjection); + // whenTrue is property, whenFalse is constant -> fails before the fix + using (var session = OpenSession()) + { + ICriteria tagCriteria = session.CreateCriteria(typeof(Entity)); - // run query + var conditionalProjection = Projections.Conditional( + Restrictions.Ge(nameof(Entity.Amount), 20m), + Projections.Property(nameof(Entity.Amount)), + Projections.Constant(20m)); + tagCriteria.SetProjection(conditionalProjection); - var results = tagCriteria.List(); + // run query + var results = tagCriteria.List(); - Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m })); - } + Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m})); } } }