forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialectFixture.cs
199 lines (172 loc) · 6.6 KB
/
DialectFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Data;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Test.DialectTest
{
/// <summary>
/// Summary description for DialectFixture.
/// </summary>
[TestFixture]
public class DialectFixture
{
protected Dialect.Dialect d = null;
private const int BeforeQuoteIndex = 0;
private const int AfterQuoteIndex = 1;
private const int AfterUnquoteIndex = 2;
protected string[] tableWithNothingToBeQuoted;
// simulating a string already enclosed in the Dialects quotes of Quote"d[Na$`
// being passed in that should be returned as Quote""d[Na$` - notice the "" before d
protected string[] tableAlreadyQuoted;
// simulating a string that has NOT been enclosed in the Dialects quotes and needs to
// be.
protected string[] tableThatNeedsToBeQuoted;
[SetUp]
public virtual void SetUp()
{
// Generic Dialect inherits all of the Quoting functions from
// Dialect (which is abstract)
d = new GenericDialect();
tableWithNothingToBeQuoted = new string[] { "plainname", "\"plainname\"" };
tableAlreadyQuoted = new string[] { "\"Quote\"\"d[Na$`\"", "\"Quote\"\"d[Na$`\"", "Quote\"d[Na$`" };
tableThatNeedsToBeQuoted = new string[] { "Quote\"d[Na$`", "\"Quote\"\"d[Na$`\"", "Quote\"d[Na$`" };
}
[Test]
public void IsQuotedTrue()
{
Assert.IsTrue(d.IsQuoted(tableAlreadyQuoted[BeforeQuoteIndex]));
}
/// <summary>
/// Test that only the first char identifies that the Identifier
/// is Quoted - regardless of what chars are contained in it.
/// </summary>
[Test]
public void IsQuotedFalse()
{
Assert.IsFalse(d.IsQuoted(tableThatNeedsToBeQuoted[BeforeQuoteIndex]));
}
[Test]
public void WhenNullOrEmptyIsQuotedFalse()
{
Assert.That(d.IsQuoted(null), Is.False);
Assert.That(d.IsQuoted(""), Is.False);
}
[Test]
public void QuoteTableNameNeeded()
{
Assert.AreEqual(
tableThatNeedsToBeQuoted[AfterQuoteIndex],
d.QuoteForTableName(tableThatNeedsToBeQuoted[BeforeQuoteIndex]));
}
[Test]
public void QuoteTableNameNotNeeded()
{
Assert.AreEqual(
tableWithNothingToBeQuoted[AfterQuoteIndex],
d.QuoteForTableName(tableWithNothingToBeQuoted[BeforeQuoteIndex]));
}
[Test]
public void QuoteTableNameAlreadyQuoted()
{
Assert.AreEqual(
tableAlreadyQuoted[BeforeQuoteIndex],
d.QuoteForTableName(tableAlreadyQuoted[BeforeQuoteIndex]));
}
/// <summary>
/// Test that it does not matter if the name passed in has been quoted or not
/// already. The UnQuote should take care of it and return the same result.
/// </summary>
[Test]
public void UnQuoteAlreadyQuoted()
{
Assert.AreEqual(
tableAlreadyQuoted[AfterUnquoteIndex],
d.UnQuote(tableAlreadyQuoted[BeforeQuoteIndex]));
Assert.AreEqual(
tableAlreadyQuoted[AfterUnquoteIndex],
d.UnQuote(tableAlreadyQuoted[AfterQuoteIndex]));
}
[Test]
public void UnQuoteNeedingQuote()
{
Assert.AreEqual(
tableThatNeedsToBeQuoted[AfterUnquoteIndex],
d.UnQuote(tableThatNeedsToBeQuoted[BeforeQuoteIndex]));
Assert.AreEqual(
tableThatNeedsToBeQuoted[AfterUnquoteIndex],
d.UnQuote(tableThatNeedsToBeQuoted[AfterQuoteIndex]));
}
[Test]
public void UnQuoteArray()
{
string[] actualUnquoted = new string[2];
string[] expectedUnquoted =
new string[] { tableThatNeedsToBeQuoted[AfterUnquoteIndex], tableAlreadyQuoted[AfterUnquoteIndex] };
actualUnquoted =
d.UnQuote(new string[] { tableThatNeedsToBeQuoted[BeforeQuoteIndex], tableAlreadyQuoted[BeforeQuoteIndex] });
ObjectAssert.AreEqual(expectedUnquoted, actualUnquoted, true);
}
[Test]
public void GetDialectUntrimmedName()
{
Dictionary<string, string> props = new Dictionary<string, string>();
props[Environment.Dialect] = "\r\n\t "
+ typeof(MsSql2000Dialect).AssemblyQualifiedName
+ " \t\r\n ";
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(props);
Assert.IsTrue(dialect is MsSql2000Dialect);
}
[Test]
public void CurrentTimestampSelection()
{
var conf = TestConfigurationHelper.GetDefaultConfiguration();
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(conf.Properties);
if (!dialect.SupportsCurrentTimestampSelection)
{
Assert.Ignore("This test does not apply to " + dialect.GetType().FullName);
}
var sessions = (ISessionFactoryImplementor)conf.BuildSessionFactory();
sessions.ConnectionProvider.Configure(conf.Properties);
IDriver driver = sessions.ConnectionProvider.Driver;
using (var connection = sessions.ConnectionProvider.GetConnection())
{
var statement = driver.GenerateCommand(CommandType.Text, new SqlString(dialect.CurrentTimestampSelectString), Array.Empty<SqlType>());
statement.Connection = connection;
using (var reader = statement.ExecuteReader())
{
Assert.That(reader.Read(), "should return one record");
Assert.That(reader[0], Is.InstanceOf<DateTime>());
}
}
}
[Test]
public void GetDecimalTypeName()
{
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
var dialect = Dialect.Dialect.GetDialect(cfg.Properties);
Assert.That(dialect.GetTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 40, 40)), Does.Not.Contain("40"), "oversized decimal");
// This regex tests wether the type is qualified with expected length/precision/scale or not qualified at all.
Assert.That(dialect.GetTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 3, 2)), Does.Match(@"^[^(]*(\(\s*3\s*,\s*2\s*\))?\s*$"), "small decimal");
}
[Test]
public void GetTypeCastName()
{
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
cfg.SetProperty(Environment.QueryDefaultCastLength, "20");
cfg.SetProperty(Environment.QueryDefaultCastPrecision, "10");
cfg.SetProperty(Environment.QueryDefaultCastScale, "3");
var dialect = Dialect.Dialect.GetDialect(cfg.Properties);
// Those regex test wether the type is qualified with expected length/precision/scale or not qualified at all.
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.Decimal), Does.Match(@"^[^(]*(\(\s*10\s*,\s*3\s*\))?\s*$"), "decimal");
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.GetSqlType(DbType.Decimal, 12, 4)), Does.Match(@"^[^(]*(\(\s*12\s*,\s*4\s*\))?\s*$"), "decimal(12,4)");
Assert.That(dialect.GetCastTypeName(new SqlType(DbType.String)), Does.Match(@"^[^(]*(\(\s*20\s*\))?\s*$"), "string");
Assert.That(dialect.GetCastTypeName(SqlTypeFactory.GetString(25)), Does.Match(@"^[^(]*(\(\s*25\s*\))?\s*$"), "string(25)");
}
}
}