Skip to content

Commit 7b6ff8e

Browse files
fixup !NH-4088 - Fix GetCastTypeName
* Oracle double special case * More tests * Tests fixes
1 parent bbd7772 commit 7b6ff8e

File tree

7 files changed

+200
-176
lines changed

7 files changed

+200
-176
lines changed

src/NHibernate.Test/Async/Criteria/ProjectionsTest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
using System;
1212
using System.Collections;
1313
using System.Collections.Generic;
14+
using System.Text.RegularExpressions;
1415
using NHibernate.Criterion;
1516
using NHibernate.Dialect;
17+
using NHibernate.SqlTypes;
1618
using NHibernate.Type;
1719
using NUnit.Framework;
1820

@@ -105,6 +107,11 @@ public async Task UsingSqlFunctions_Concat_WithCastAsync()
105107
[Test]
106108
public async Task CastWithLengthAsync()
107109
{
110+
if (Regex.IsMatch(Dialect.GetCastTypeName(SqlTypeFactory.GetString(3)), @"^[^(]*$"))
111+
{
112+
Assert.Ignore($"Dialect {Dialect} does not seem to handle string length in cast");
113+
}
114+
108115
using (var s = OpenSession())
109116
{
110117
try
@@ -120,8 +127,7 @@ public async Task CastWithLengthAsync()
120127
}
121128
catch (Exception e)
122129
{
123-
if (!e.Message.Contains("truncation") &&
124-
(e.InnerException == null || !e.InnerException.Message.Contains("truncation")))
130+
if (e.InnerException == null || !e.InnerException.Message.Contains("truncation"))
125131
throw;
126132
}
127133
}

src/NHibernate.Test/Async/DriverTest/FirebirdClientDriverFixture.cs

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,74 @@ public class FirebirdClientDriverFixtureAsync
2626
private string _connectionString;
2727
private FirebirdClientDriver _driver;
2828

29+
[OneTimeSetUp]
30+
public void OneTimeSetup()
31+
{
32+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
33+
34+
var dlct = cfg.GetProperty("dialect");
35+
if (!dlct.Contains("Firebird"))
36+
Assert.Ignore("Applies only to Firebird");
37+
38+
_driver = new FirebirdClientDriver();
39+
_driver.Configure(cfg.Properties);
40+
_connectionString = cfg.GetProperty("connection.connection_string");
41+
}
42+
2943
[Test]
3044
public async Task ConnectionPooling_OpenThenCloseThenOpenAnotherOne_OnlyOneConnectionIsPooledAsync()
3145
{
32-
MakeDriver();
33-
3446
_driver.ClearPool(_connectionString);
3547

3648
var allreadyEstablished = await (GetEstablishedConnectionsAsync());
3749

38-
var connection1 = MakeConnection();
39-
var connection2 = MakeConnection();
40-
41-
//open first connection
42-
await (connection1.OpenAsync());
43-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first open"));
50+
using (var connection1 = MakeConnection())
51+
using (var connection2 = MakeConnection())
52+
{
53+
//open first connection
54+
await (connection1.OpenAsync());
55+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first open"));
4456

45-
//return it to the pool
46-
connection1.Close();
47-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first close"));
57+
//return it to the pool
58+
connection1.Close();
59+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first close"));
4860

49-
//open the second connection
50-
await (connection2.OpenAsync());
51-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After second open"));
61+
//open the second connection
62+
await (connection2.OpenAsync());
63+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After second open"));
5264

53-
//return it to the pool
54-
connection2.Close();
55-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After second close"));
65+
//return it to the pool
66+
connection2.Close();
67+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After second close"));
68+
}
5669
}
5770

5871
[Test]
5972
public async Task ConnectionPooling_OpenThenCloseTwoAtTheSameTime_TowConnectionsArePooledAsync()
6073
{
61-
MakeDriver();
62-
6374
_driver.ClearPool(_connectionString);
6475

6576
var allreadyEstablished = await (GetEstablishedConnectionsAsync());
6677

67-
var connection1 = MakeConnection();
68-
var connection2 = MakeConnection();
69-
70-
//open first connection
71-
await (connection1.OpenAsync());
72-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first open"));
73-
74-
//open second one
75-
await (connection2.OpenAsync());
76-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After second open"));
77-
78-
//return connection1 to the pool
79-
connection1.Close();
80-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After first close"));
78+
using (var connection1 = MakeConnection())
79+
using (var connection2 = MakeConnection())
80+
{
81+
//open first connection
82+
await (connection1.OpenAsync());
83+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 1, "After first open"));
8184

82-
//return connection2 to the pool
83-
connection2.Close();
84-
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After second close"));
85-
}
85+
//open second one
86+
await (connection2.OpenAsync());
87+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After second open"));
8688

87-
private void MakeDriver()
88-
{
89-
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
90-
var dlct = cfg.GetProperty("dialect");
91-
if (!dlct.Contains("Firebird"))
92-
Assert.Ignore("Applies only to Firebird");
89+
//return connection1 to the pool
90+
connection1.Close();
91+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After first close"));
9392

94-
_driver = new FirebirdClientDriver();
95-
_connectionString = cfg.GetProperty("connection.connection_string");
93+
//return connection2 to the pool
94+
connection2.Close();
95+
await (VerifyCountOfEstablishedConnectionsIsAsync(allreadyEstablished + 2, "After second close"));
96+
}
9697
}
9798

9899
private DbConnection MakeConnection()
@@ -125,38 +126,38 @@ private DbConnection MakeConnection()
125126
private DbCommand BuildSelectCaseCommand(SqlType paramType)
126127
{
127128
var sqlString = new SqlStringBuilder()
128-
.Add("select (case when col = ")
129-
.AddParameter()
130-
.Add(" then ")
131-
.AddParameter()
132-
.Add(" else ")
133-
.AddParameter()
134-
.Add(" end) from table")
135-
.ToSqlString();
129+
.Add("select (case when col = ")
130+
.AddParameter()
131+
.Add(" then ")
132+
.AddParameter()
133+
.Add(" else ")
134+
.AddParameter()
135+
.Add(" end) from table")
136+
.ToSqlString();
136137

137138
return _driver.GenerateCommand(CommandType.Text, sqlString, new[] { paramType, paramType, paramType });
138139
}
139140

140141
private DbCommand BuildSelectConcatCommand(SqlType paramType)
141142
{
142143
var sqlString = new SqlStringBuilder()
143-
.Add("select col || ")
144-
.AddParameter()
145-
.Add(" || ")
146-
.Add("col ")
147-
.Add("from table")
148-
.ToSqlString();
144+
.Add("select col || ")
145+
.AddParameter()
146+
.Add(" || ")
147+
.Add("col ")
148+
.Add("from table")
149+
.ToSqlString();
149150

150151
return _driver.GenerateCommand(CommandType.Text, sqlString, new[] { paramType });
151152
}
152153

153154
private DbCommand BuildSelectAddCommand(SqlType paramType)
154155
{
155156
var sqlString = new SqlStringBuilder()
156-
.Add("select col + ")
157-
.AddParameter()
158-
.Add(" from table")
159-
.ToSqlString();
157+
.Add("select col + ")
158+
.AddParameter()
159+
.Add(" from table")
160+
.ToSqlString();
160161

161162
return _driver.GenerateCommand(CommandType.Text, sqlString, new[] { paramType });
162163
}
@@ -172,6 +173,7 @@ private DbCommand BuildInsertWithParamsInSelectCommand(SqlType paramType)
172173

173174
return _driver.GenerateCommand(CommandType.Text, sqlString, new[] { paramType });
174175
}
176+
175177
private DbCommand BuildInsertWithParamsInSelectCommandWithSelectInColumnName(SqlType paramType)
176178
{
177179
var sqlString = new SqlStringBuilder()
@@ -184,7 +186,7 @@ private DbCommand BuildInsertWithParamsInSelectCommandWithSelectInColumnName(Sql
184186
return _driver.GenerateCommand(CommandType.Text, sqlString, new[] { paramType });
185187
}
186188

187-
private DbCommand BuildInsertWithParamsInSelectCommandWithWhereInColumnName(SqlType paramType)
189+
private DbCommand BuildInsertWithParamsInSelectCommandWithWhereInColumnName(SqlType paramType)
188190
{
189191
var sqlString = new SqlStringBuilder()
190192
.Add("insert into table1 (col1_where_aaa) ")

src/NHibernate.Test/Criteria/ProjectionsTest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Text.RegularExpressions;
45
using NHibernate.Criterion;
56
using NHibernate.Dialect;
7+
using NHibernate.SqlTypes;
68
using NHibernate.Type;
79
using NUnit.Framework;
810

@@ -94,6 +96,11 @@ public void UsingSqlFunctions_Concat_WithCast()
9496
[Test]
9597
public void CastWithLength()
9698
{
99+
if (Regex.IsMatch(Dialect.GetCastTypeName(SqlTypeFactory.GetString(3)), @"^[^(]*$"))
100+
{
101+
Assert.Ignore($"Dialect {Dialect} does not seem to handle string length in cast");
102+
}
103+
97104
using (var s = OpenSession())
98105
{
99106
try
@@ -109,8 +116,7 @@ public void CastWithLength()
109116
}
110117
catch (Exception e)
111118
{
112-
if (!e.Message.Contains("truncation") &&
113-
(e.InnerException == null || !e.InnerException.Message.Contains("truncation")))
119+
if (e.InnerException == null || !e.InnerException.Message.Contains("truncation"))
114120
throw;
115121
}
116122
}

src/NHibernate.Test/DialectTest/DialectFixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public void GetDecimalTypeName()
178178
var dialect = Dialect.Dialect.GetDialect(cfg.Properties);
179179

180180
Assert.That(dialect.GetTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 40, 40)), Does.Not.Contain("40"), "oversized decimal");
181+
// This regex tests wether the type is qualified with expected length/precision/scale or not qualified at all.
181182
Assert.That(dialect.GetTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 3, 2)), Does.Match(@"^[^(]*(\(\s*3\s*,\s*2\s*\))?\s*$"), "small decimal");
182183
}
183184

@@ -190,8 +191,11 @@ public void GetTypeCastName()
190191
cfg.SetProperty(Environment.QueryDefaultCastScale, "3");
191192
var dialect = Dialect.Dialect.GetDialect(cfg.Properties);
192193

194+
// Those regex test wether the type is qualified with expected length/precision/scale or not qualified at all.
193195
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.Decimal), Does.Match(@"^[^(]*(\(\s*10\s*,\s*3\s*\))?\s*$"), "decimal");
196+
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 12, 4)), Does.Match(@"^[^(]*(\(\s*12\s*,\s*4\s*\))?\s*$"), "decimal(12,4)");
194197
Assert.That(dialect.GetCastTypeName(new SqlType(DbType.String)), Does.Match(@"^[^(]*(\(\s*20\s*\))?\s*$"), "string");
198+
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.GetString(25)), Does.Match(@"^[^(]*(\(\s*25\s*\))?\s*$"), "string(25)");
195199
}
196200
}
197201
}

0 commit comments

Comments
 (0)