Skip to content

Commit 1fa50d9

Browse files
delta-emilbahusoid
andauthored
Add test case for #1180 and improve NullableType.ToString (#2456)
Co-authored-by: Roman Artiukhin <bahusdrive@gmail.com>
1 parent 3a5f949 commit 1fa50d9

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Cfg.MappingSchema;
12+
using NHibernate.Criterion;
13+
using NHibernate.Mapping.ByCode;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.NHSpecificTest.GH1180
17+
{
18+
using System.Threading.Tasks;
19+
[KnownBug("NH-3847 (GH-1180)")]
20+
[TestFixture]
21+
public class ByCodeFixtureAsync : TestCaseMappingByCode
22+
{
23+
protected override HbmMapping GetMappings()
24+
{
25+
var mapper = new ModelMapper();
26+
27+
mapper.Class<Entity>(rc =>
28+
{
29+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
30+
rc.Property(x => x.Name, m => { m.Length(10); });
31+
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
32+
});
33+
34+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
35+
}
36+
37+
protected override void OnTearDown()
38+
{
39+
using (var session = OpenSession())
40+
using (var transaction = session.BeginTransaction())
41+
{
42+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
43+
transaction.Commit();
44+
}
45+
}
46+
47+
[Test]
48+
public async Task StringTypesAsync()
49+
{
50+
using (var session = OpenSession())
51+
using (var transaction = session.BeginTransaction())
52+
{
53+
// data
54+
await (session.SaveAsync(new Entity {Name = "Alpha"}));
55+
await (session.SaveAsync(new Entity {Name = "Beta"}));
56+
await (session.SaveAsync(new Entity {Name = "Gamma"}));
57+
58+
await (transaction.CommitAsync());
59+
}
60+
61+
// whenTrue is constant, whenFalse is property -> works even before the fix
62+
using (var session = OpenSession())
63+
{
64+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
65+
66+
var conditionalProjection = Projections.Conditional(
67+
Restrictions.Not(
68+
Restrictions.Like(nameof(Entity.Name), "B%")),
69+
Projections.Constant("other"),
70+
Projections.Property(nameof(Entity.Name)));
71+
tagCriteria.SetProjection(conditionalProjection);
72+
73+
// run query
74+
var results = await (tagCriteria.ListAsync());
75+
76+
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
77+
}
78+
79+
// whenTrue is property, whenFalse is constant -> fails before the fix
80+
using (var session = OpenSession())
81+
{
82+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
83+
84+
var conditionalProjection = Projections.Conditional(
85+
Restrictions.Like(nameof(Entity.Name), "B%"),
86+
Projections.Property(nameof(Entity.Name)),
87+
Projections.Constant("other"));
88+
tagCriteria.SetProjection(conditionalProjection);
89+
90+
// run query
91+
var results = await (tagCriteria.ListAsync());
92+
93+
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
94+
}
95+
}
96+
97+
[Test]
98+
public async Task DecimalTypesAsync()
99+
{
100+
using (var session = OpenSession())
101+
using (var transaction = session.BeginTransaction())
102+
{
103+
await (session.SaveAsync(new Entity {Amount = 3.14m}));
104+
await (session.SaveAsync(new Entity {Amount = 42.13m}));
105+
await (session.SaveAsync(new Entity {Amount = 17.99m}));
106+
107+
await (transaction.CommitAsync());
108+
}
109+
110+
// whenTrue is constant, whenFalse is property -> works even before the fix
111+
using (var session = OpenSession())
112+
{
113+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
114+
115+
var conditionalProjection = Projections.Conditional(
116+
Restrictions.Not(
117+
Restrictions.Ge(nameof(Entity.Amount), 20m)),
118+
Projections.Constant(20m),
119+
Projections.Property(nameof(Entity.Amount)));
120+
tagCriteria.SetProjection(conditionalProjection);
121+
122+
// run query
123+
var results = await (tagCriteria.ListAsync());
124+
125+
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
126+
}
127+
128+
// whenTrue is property, whenFalse is constant -> fails before the fix
129+
using (var session = OpenSession())
130+
{
131+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
132+
133+
var conditionalProjection = Projections.Conditional(
134+
Restrictions.Ge(nameof(Entity.Amount), 20m),
135+
Projections.Property(nameof(Entity.Amount)),
136+
Projections.Constant(20m));
137+
tagCriteria.SetProjection(conditionalProjection);
138+
139+
// run query
140+
var results = await (tagCriteria.ListAsync());
141+
142+
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
143+
}
144+
}
145+
}
146+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1180
4+
{
5+
internal class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual decimal Amount { get; set; }
10+
}
11+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Criterion;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.GH1180
7+
{
8+
[KnownBug("NH-3847 (GH-1180)")]
9+
[TestFixture]
10+
public class ByCodeFixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
16+
mapper.Class<Entity>(rc =>
17+
{
18+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
19+
rc.Property(x => x.Name, m => { m.Length(10); });
20+
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
21+
});
22+
23+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
24+
}
25+
26+
protected override void OnTearDown()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public void StringTypes()
38+
{
39+
using (var session = OpenSession())
40+
using (var transaction = session.BeginTransaction())
41+
{
42+
// data
43+
session.Save(new Entity {Name = "Alpha"});
44+
session.Save(new Entity {Name = "Beta"});
45+
session.Save(new Entity {Name = "Gamma"});
46+
47+
transaction.Commit();
48+
}
49+
50+
// whenTrue is constant, whenFalse is property -> works even before the fix
51+
using (var session = OpenSession())
52+
{
53+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
54+
55+
var conditionalProjection = Projections.Conditional(
56+
Restrictions.Not(
57+
Restrictions.Like(nameof(Entity.Name), "B%")),
58+
Projections.Constant("other"),
59+
Projections.Property(nameof(Entity.Name)));
60+
tagCriteria.SetProjection(conditionalProjection);
61+
62+
// run query
63+
var results = tagCriteria.List();
64+
65+
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
66+
}
67+
68+
// whenTrue is property, whenFalse is constant -> fails before the fix
69+
using (var session = OpenSession())
70+
{
71+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
72+
73+
var conditionalProjection = Projections.Conditional(
74+
Restrictions.Like(nameof(Entity.Name), "B%"),
75+
Projections.Property(nameof(Entity.Name)),
76+
Projections.Constant("other"));
77+
tagCriteria.SetProjection(conditionalProjection);
78+
79+
// run query
80+
var results = tagCriteria.List();
81+
82+
Assert.That(results, Is.EquivalentTo(new[] {"other", "Beta", "other"}));
83+
}
84+
}
85+
86+
[Test]
87+
public void DecimalTypes()
88+
{
89+
using (var session = OpenSession())
90+
using (var transaction = session.BeginTransaction())
91+
{
92+
session.Save(new Entity {Amount = 3.14m});
93+
session.Save(new Entity {Amount = 42.13m});
94+
session.Save(new Entity {Amount = 17.99m});
95+
96+
transaction.Commit();
97+
}
98+
99+
// whenTrue is constant, whenFalse is property -> works even before the fix
100+
using (var session = OpenSession())
101+
{
102+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
103+
104+
var conditionalProjection = Projections.Conditional(
105+
Restrictions.Not(
106+
Restrictions.Ge(nameof(Entity.Amount), 20m)),
107+
Projections.Constant(20m),
108+
Projections.Property(nameof(Entity.Amount)));
109+
tagCriteria.SetProjection(conditionalProjection);
110+
111+
// run query
112+
var results = tagCriteria.List();
113+
114+
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
115+
}
116+
117+
// whenTrue is property, whenFalse is constant -> fails before the fix
118+
using (var session = OpenSession())
119+
{
120+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
121+
122+
var conditionalProjection = Projections.Conditional(
123+
Restrictions.Ge(nameof(Entity.Amount), 20m),
124+
Projections.Property(nameof(Entity.Amount)),
125+
Projections.Constant(20m));
126+
tagCriteria.SetProjection(conditionalProjection);
127+
128+
// run query
129+
var results = tagCriteria.List();
130+
131+
Assert.That(results, Is.EquivalentTo(new[] {20m, 42.13m, 20m}));
132+
}
133+
}
134+
}
135+
}

src/NHibernate/Type/NullableType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ public override int GetHashCode()
377377
return (SqlType.GetHashCode() / 2) + (Name.GetHashCode() / 2);
378378
}
379379

380+
/// <summary>
381+
/// Provides a more descriptive string representation by reporting the properties that are important for equality.
382+
/// Useful in error messages.
383+
/// </summary>
384+
public override string ToString()
385+
{
386+
return $"{base.ToString()} (SqlType: {SqlType})";
387+
}
388+
380389
#endregion
381390
}
382391
}

0 commit comments

Comments
 (0)