Skip to content

Commit 6759a66

Browse files
author
Emil Tzvetkov
committed
GH-1180 make SqlType Equals symmetric
1 parent d361456 commit 6759a66

File tree

4 files changed

+283
-3
lines changed

4 files changed

+283
-3
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
[TestFixture]
20+
public class ByCodeFixtureAsync : TestCaseMappingByCode
21+
{
22+
protected override HbmMapping GetMappings()
23+
{
24+
var mapper = new ModelMapper();
25+
26+
mapper.Class<Entity>(rc =>
27+
{
28+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
29+
rc.Property(x => x.Name, m => { m.Length(10); });
30+
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
31+
});
32+
33+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
34+
}
35+
36+
[Test]
37+
public async Task StringTypesAsync()
38+
{
39+
using (var session = OpenSession())
40+
using (var transaction = session.BeginTransaction())
41+
{
42+
// data
43+
{
44+
await (session.SaveAsync(new Entity { Name = "Alpha" }));
45+
await (session.SaveAsync(new Entity { Name = "Beta" }));
46+
await (session.SaveAsync(new Entity { Name = "Gamma" }));
47+
}
48+
49+
await (session.FlushAsync());
50+
51+
// whenTrue is constant, whenFalse is property -> works even before the fix
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+
64+
var results = await (tagCriteria.ListAsync());
65+
66+
Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" }));
67+
}
68+
69+
// whenTrue is property, whenFalse is constant -> fails before the fix
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+
81+
var results = await (tagCriteria.ListAsync());
82+
83+
Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" }));
84+
}
85+
}
86+
}
87+
88+
[Test]
89+
public async Task DecimalTypesAsync()
90+
{
91+
using (var session = OpenSession())
92+
using (var transaction = session.BeginTransaction())
93+
{
94+
// data
95+
{
96+
await (session.SaveAsync(new Entity { Amount = 3.14m }));
97+
await (session.SaveAsync(new Entity { Amount = 42.13m }));
98+
await (session.SaveAsync(new Entity { Amount = 17.99m }));
99+
}
100+
101+
await (session.FlushAsync());
102+
103+
// whenTrue is constant, whenFalse is property -> works even before the fix
104+
{
105+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
106+
107+
var conditionalProjection = Projections.Conditional(
108+
Restrictions.Not(
109+
Restrictions.Ge(nameof(Entity.Amount), 20m)),
110+
Projections.Constant(20m),
111+
Projections.Property(nameof(Entity.Amount)));
112+
tagCriteria.SetProjection(conditionalProjection);
113+
114+
// run query
115+
116+
var results = await (tagCriteria.ListAsync());
117+
118+
Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m }));
119+
}
120+
121+
// whenTrue is property, whenFalse is constant -> fails before the fix
122+
{
123+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
124+
125+
var conditionalProjection = Projections.Conditional(
126+
Restrictions.Ge(nameof(Entity.Amount), 20m),
127+
Projections.Property(nameof(Entity.Amount)),
128+
Projections.Constant(20m));
129+
tagCriteria.SetProjection(conditionalProjection);
130+
131+
// run query
132+
133+
var results = await (tagCriteria.ListAsync());
134+
135+
Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m }));
136+
}
137+
}
138+
}
139+
}
140+
}
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: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
[TestFixture]
9+
public class ByCodeFixture : TestCaseMappingByCode
10+
{
11+
protected override HbmMapping GetMappings()
12+
{
13+
var mapper = new ModelMapper();
14+
15+
mapper.Class<Entity>(rc =>
16+
{
17+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
18+
rc.Property(x => x.Name, m => { m.Length(10); });
19+
rc.Property(x => x.Amount, m => { m.Precision(8); m.Scale(2); });
20+
});
21+
22+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
23+
}
24+
25+
[Test]
26+
public void StringTypes()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
// data
32+
{
33+
session.Save(new Entity { Name = "Alpha" });
34+
session.Save(new Entity { Name = "Beta" });
35+
session.Save(new Entity { Name = "Gamma" });
36+
}
37+
38+
session.Flush();
39+
40+
// whenTrue is constant, whenFalse is property -> works even before the fix
41+
{
42+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
43+
44+
var conditionalProjection = Projections.Conditional(
45+
Restrictions.Not(
46+
Restrictions.Like(nameof(Entity.Name), "B%")),
47+
Projections.Constant("other"),
48+
Projections.Property(nameof(Entity.Name)));
49+
tagCriteria.SetProjection(conditionalProjection);
50+
51+
// run query
52+
53+
var results = tagCriteria.List();
54+
55+
Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" }));
56+
}
57+
58+
// whenTrue is property, whenFalse is constant -> fails before the fix
59+
{
60+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
61+
62+
var conditionalProjection = Projections.Conditional(
63+
Restrictions.Like(nameof(Entity.Name), "B%"),
64+
Projections.Property(nameof(Entity.Name)),
65+
Projections.Constant("other"));
66+
tagCriteria.SetProjection(conditionalProjection);
67+
68+
// run query
69+
70+
var results = tagCriteria.List();
71+
72+
Assert.That(results, Is.EquivalentTo(new[] { "other", "Beta", "other" }));
73+
}
74+
}
75+
}
76+
77+
[Test]
78+
public void DecimalTypes()
79+
{
80+
using (var session = OpenSession())
81+
using (var transaction = session.BeginTransaction())
82+
{
83+
// data
84+
{
85+
session.Save(new Entity { Amount = 3.14m });
86+
session.Save(new Entity { Amount = 42.13m });
87+
session.Save(new Entity { Amount = 17.99m });
88+
}
89+
90+
session.Flush();
91+
92+
// whenTrue is constant, whenFalse is property -> works even before the fix
93+
{
94+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
95+
96+
var conditionalProjection = Projections.Conditional(
97+
Restrictions.Not(
98+
Restrictions.Ge(nameof(Entity.Amount), 20m)),
99+
Projections.Constant(20m),
100+
Projections.Property(nameof(Entity.Amount)));
101+
tagCriteria.SetProjection(conditionalProjection);
102+
103+
// run query
104+
105+
var results = tagCriteria.List();
106+
107+
Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m }));
108+
}
109+
110+
// whenTrue is property, whenFalse is constant -> fails before the fix
111+
{
112+
ICriteria tagCriteria = session.CreateCriteria(typeof(Entity));
113+
114+
var conditionalProjection = Projections.Conditional(
115+
Restrictions.Ge(nameof(Entity.Amount), 20m),
116+
Projections.Property(nameof(Entity.Amount)),
117+
Projections.Constant(20m));
118+
tagCriteria.SetProjection(conditionalProjection);
119+
120+
// run query
121+
122+
var results = tagCriteria.List();
123+
124+
Assert.That(results, Is.EquivalentTo(new[] { 20m, 42.13m, 20m }));
125+
}
126+
}
127+
}
128+
}
129+
}

src/NHibernate/SqlTypes/SqlType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ public bool Equals(SqlType rhsSqlType)
130130
return false;
131131
}
132132

133-
if (LengthDefined)
133+
if (LengthDefined && rhsSqlType.LengthDefined)
134134
{
135135
return (DbType.Equals(rhsSqlType.DbType)) && (Length == rhsSqlType.Length);
136136
}
137-
if (PrecisionDefined)
137+
if (PrecisionDefined && rhsSqlType.PrecisionDefined)
138138
{
139139
return (DbType.Equals(rhsSqlType.DbType)) && (Precision == rhsSqlType.Precision) && (Scale == rhsSqlType.Scale);
140140
}
141-
if (ScaleDefined)
141+
if (ScaleDefined && rhsSqlType.ScaleDefined)
142142
{
143143
return DbType.Equals(rhsSqlType.DbType) && Scale == rhsSqlType.Scale;
144144
}

0 commit comments

Comments
 (0)