Skip to content

Commit 711a39c

Browse files
NH-3787 - SQLite requires the cast in SQL
And SQLite cannot perform the NH-3787 test with so many digits (it does not have a true decimal type anyway). To be squashed
1 parent 46d75c9 commit 711a39c

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3787/TestFixture.cs

+22-13
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010

1111
using System.Linq;
1212
using NHibernate.Criterion;
13+
using NHibernate.Linq;
1314
using NHibernate.Transform;
15+
using NHibernate.Type;
1416
using NUnit.Framework;
15-
using NHibernate.Linq;
1617

1718
namespace NHibernate.Test.NHSpecificTest.NH3787
1819
{
1920
using System.Threading.Tasks;
2021
[TestFixture]
2122
public class TestFixtureAsync : BugTestCase
2223
{
23-
private const decimal _testRate = 12345.123456789M;
24+
private const decimal _testRate = 12345.1234567890123M;
25+
26+
protected override bool AppliesTo(Dialect.Dialect dialect)
27+
{
28+
return !TestDialect.HasBrokenDecimalType;
29+
}
2430

2531
protected override void OnSetUp()
2632
{
@@ -33,7 +39,7 @@ protected override void OnSetUp()
3339
{
3440
UsePreviousRate = true,
3541
PreviousRate = _testRate,
36-
Rate = 54321.123456789M
42+
Rate = 54321.1234567890123M
3743
};
3844
s.Save(testEntity);
3945
t.Commit();
@@ -76,7 +82,7 @@ public async Task TestLinqProjectionAsync()
7682
var queryResult = await ((from test in s.Query<TestEntity>()
7783
select new RateDto { Rate = test.UsePreviousRate ? test.PreviousRate : test.Rate }).ToListAsync());
7884

79-
// Check it has not been truncated to the default 5 positions of NHibernate.
85+
// Check it has not been truncated to the default scale (10) of NHibernate.
8086
Assert.That(queryResult[0].Rate, Is.EqualTo(_testRate));
8187
await (t.CommitAsync());
8288
}
@@ -90,9 +96,11 @@ public async Task TestLinqQueryOnExpressionAsync()
9096
{
9197
var queryResult = await (s
9298
.Query<TestEntity>()
93-
.Where(e => (e.UsePreviousRate ? e.PreviousRate : e.Rate) == _testRate)
99+
.Where(
100+
// Without MappedAs, the test fails for SQL Server because it would restrict its parameter to the dialect's default scale.
101+
e => (e.UsePreviousRate ? e.PreviousRate : e.Rate) == _testRate.MappedAs(TypeFactory.Basic("decimal(18,13)")))
94102
.ToListAsync());
95-
103+
96104
Assert.That(queryResult.Count, Is.EqualTo(1));
97105
Assert.That(queryResult[0].PreviousRate, Is.EqualTo(_testRate));
98106
await (t.CommitAsync());
@@ -113,13 +121,14 @@ public async Task TestQueryOverProjectionAsync()
113121
var query = s
114122
.QueryOver(() => testEntity)
115123
.Select(
116-
Projections.Alias(
117-
Projections.Conditional(
118-
Restrictions.Eq(Projections.Property(() => testEntity.UsePreviousRate), true),
119-
Projections.Property(() => testEntity.PreviousRate),
120-
Projections.Property(() => testEntity.Rate)),
121-
"Rate")
122-
.WithAlias(() => rateDto.Rate));
124+
Projections
125+
.Alias(
126+
Projections.Conditional(
127+
Restrictions.Eq(Projections.Property(() => testEntity.UsePreviousRate), true),
128+
Projections.Property(() => testEntity.PreviousRate),
129+
Projections.Property(() => testEntity.Rate)),
130+
"Rate")
131+
.WithAlias(() => rateDto.Rate));
123132

124133
var queryResult = await (query.TransformUsing(Transformers.AliasToBean<RateDto>()).ListAsync<RateDto>());
125134

src/NHibernate.Test/NHSpecificTest/NH3787/TestFixture.cs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class TestFixture : BugTestCase
1212
{
1313
private const decimal _testRate = 12345.1234567890123M;
1414

15+
protected override bool AppliesTo(Dialect.Dialect dialect)
16+
{
17+
return !TestDialect.HasBrokenDecimalType;
18+
}
19+
1520
protected override void OnSetUp()
1621
{
1722
base.OnSetUp();

src/NHibernate/Dialect/SQLiteDialect.cs

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ protected virtual void RegisterFunctions()
8686
RegisterFunction("cast", new SQLiteCastFunction());
8787

8888
RegisterFunction("round", new StandardSQLFunction("round"));
89+
90+
// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
91+
RegisterFunction("transparentcast", new CastFunction());
8992
}
9093

9194
#region private static readonly string[] DialectKeywords = { ... }

0 commit comments

Comments
 (0)