|
6 | 6 | using NHibernate.SqlTypes; |
7 | 7 | using NUnit.Framework; |
8 | 8 |
|
9 | | -namespace NHibernate.Test.NHSpecificTest.GH3530 |
10 | | -{ |
11 | | - [TestFixture] |
12 | | - public class Fixture : BugTestCase |
13 | | - { |
14 | | - private CultureInfo initialCulture; |
15 | | - |
16 | | - [OneTimeSetUp] |
17 | | - public void FixtureSetup() |
18 | | - { |
19 | | - initialCulture = CurrentCulture; |
20 | | - } |
| 9 | +namespace NHibernate.Test.NHSpecificTest.GH3530; |
21 | 10 |
|
22 | | - [OneTimeTearDown] |
23 | | - public void FixtureTearDown() |
24 | | - { |
25 | | - CurrentCulture = initialCulture; |
26 | | - } |
| 11 | +[TestFixture] |
| 12 | +public class Fixture : BugTestCase |
| 13 | +{ |
| 14 | + private CultureInfo initialCulture; |
27 | 15 |
|
28 | | - protected override void OnTearDown() |
29 | | - { |
30 | | - using var session = OpenSession(); |
31 | | - using var transaction = session.BeginTransaction(); |
| 16 | + [OneTimeSetUp] |
| 17 | + public void FixtureSetup() |
| 18 | + { |
| 19 | + initialCulture = CurrentCulture; |
| 20 | + } |
32 | 21 |
|
33 | | - session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 22 | + [OneTimeTearDown] |
| 23 | + public void FixtureTearDown() |
| 24 | + { |
| 25 | + CurrentCulture = initialCulture; |
| 26 | + } |
34 | 27 |
|
35 | | - transaction.Commit(); |
36 | | - } |
| 28 | + protected override void OnTearDown() |
| 29 | + { |
| 30 | + using var session = OpenSession(); |
| 31 | + using var transaction = session.BeginTransaction(); |
37 | 32 |
|
38 | | - protected override void CreateSchema() |
39 | | - { |
40 | | - CreateTable("Integer"); |
41 | | - CreateTable("DateTime"); |
42 | | - CreateTable("Double"); |
43 | | - CreateTable("Decimal"); |
| 33 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
44 | 34 |
|
45 | | - base.CreateSchema(); |
46 | | - } |
| 35 | + transaction.Commit(); |
| 36 | + } |
47 | 37 |
|
48 | | - /// <summary> |
49 | | - /// This function creates the schema for our custom entities. |
50 | | - /// If the SchemaExporter provided a mechanism to override the database |
51 | | - /// type, this method would not be required. |
52 | | - /// </summary> |
53 | | - /// <param name="name"></param> |
54 | | - private void CreateTable(string name) |
55 | | - { |
56 | | - var sb = new StringBuilder(); |
57 | | - var guidType = Dialect.GetTypeName(SqlTypeFactory.Guid); |
58 | | - var stringType = Dialect.GetTypeName(SqlTypeFactory.GetAnsiString(255)); |
| 38 | + protected override void CreateSchema() |
| 39 | + { |
| 40 | + CreateTable("Integer"); |
| 41 | + CreateTable("DateTime"); |
| 42 | + CreateTable("Double"); |
| 43 | + CreateTable("Decimal"); |
59 | 44 |
|
60 | | - var catalog = GetQuotedDefaultCatalog(); |
61 | | - var schema = GetQuotedDefaultSchema(); |
62 | | - var table = GetQualifiedName(catalog, schema, $"{name}Entity"); |
| 45 | + base.CreateSchema(); |
| 46 | + } |
63 | 47 |
|
64 | | - sb.Append($"{Dialect.CreateTableString} {table} ("); |
| 48 | + /// <summary> |
| 49 | + /// This function creates the schema for our custom entities. |
| 50 | + /// If the SchemaExporter provided a mechanism to override the database |
| 51 | + /// type, this method would not be required. |
| 52 | + /// </summary> |
| 53 | + /// <param name="name"></param> |
| 54 | + private void CreateTable(string name) |
| 55 | + { |
| 56 | + var sb = new StringBuilder(); |
| 57 | + var guidType = Dialect.GetTypeName(SqlTypeFactory.Guid); |
| 58 | + var stringType = Dialect.GetTypeName(SqlTypeFactory.GetAnsiString(255)); |
65 | 59 |
|
66 | | - // Generate columns |
67 | | - sb.Append($"Id {guidType}, "); |
68 | | - sb.Append($"DataValue {stringType}"); |
| 60 | + var catalog = GetQuotedDefaultCatalog(); |
| 61 | + var schema = GetQuotedDefaultSchema(); |
| 62 | + var table = GetQualifiedName(catalog, schema, $"{name}Entity"); |
69 | 63 |
|
70 | | - // Add the primary key contraint for the identity column |
71 | | - sb.Append($", {Dialect.PrimaryKeyString} ( Id )"); |
72 | | - sb.Append(')'); |
| 64 | + sb.Append($"{Dialect.CreateTableString} {table} ("); |
73 | 65 |
|
74 | | - using var cn = Sfi.ConnectionProvider.GetConnection(); |
75 | | - try |
76 | | - { |
77 | | - using var cmd = cn.CreateCommand(); |
| 66 | + // Generate columns |
| 67 | + sb.Append($"Id {guidType}, "); |
| 68 | + sb.Append($"DataValue {stringType}"); |
78 | 69 |
|
79 | | - cmd.CommandText = sb.ToString(); |
80 | | - cmd.ExecuteNonQuery(); |
81 | | - } |
82 | | - catch (Exception ex) |
83 | | - { |
84 | | - Assert.Warn($"Creating the schema failed, assuming it already exists. {ex}"); |
85 | | - } |
86 | | - finally |
87 | | - { |
88 | | - Sfi.ConnectionProvider.CloseConnection(cn); |
89 | | - } |
90 | | - } |
| 70 | + // Add the primary key contraint for the identity column |
| 71 | + sb.Append($", {Dialect.PrimaryKeyString} ( Id )"); |
| 72 | + sb.Append(')'); |
91 | 73 |
|
92 | | - private string GetQuotedDefaultCatalog() |
| 74 | + using var cn = Sfi.ConnectionProvider.GetConnection(); |
| 75 | + try |
93 | 76 | { |
94 | | - var t = cfg.GetType(); |
95 | | - var getQuotedDefaultCatalog = t.GetMethod("GetQuotedDefaultCatalog", BindingFlags.Instance | BindingFlags.NonPublic); |
| 77 | + using var cmd = cn.CreateCommand(); |
96 | 78 |
|
97 | | - return (string) getQuotedDefaultCatalog.Invoke(cfg, [Dialect]); |
| 79 | + cmd.CommandText = sb.ToString(); |
| 80 | + cmd.ExecuteNonQuery(); |
98 | 81 | } |
99 | | - |
100 | | - private string GetQuotedDefaultSchema() |
| 82 | + catch (Exception ex) |
101 | 83 | { |
102 | | - var t = cfg.GetType(); |
103 | | - var getQuotedDefaultSchema = t.GetMethod("GetQuotedDefaultSchema", BindingFlags.Instance | BindingFlags.NonPublic); |
104 | | - |
105 | | - return (string) getQuotedDefaultSchema.Invoke(cfg, [Dialect]); |
| 84 | + Assert.Warn($"Creating the schema failed, assuming it already exists. {ex}"); |
106 | 85 | } |
107 | | - |
108 | | - private string GetQualifiedName(string catalog, string schema, string name) |
| 86 | + finally |
109 | 87 | { |
110 | | - return Dialect.Qualify(catalog, schema, name); |
| 88 | + Sfi.ConnectionProvider.CloseConnection(cn); |
111 | 89 | } |
| 90 | + } |
112 | 91 |
|
113 | | - private void PerformTest<T, U>(CultureInfo from, CultureInfo to, T expectedValue, Action<T, T> assert) |
114 | | - where T : struct |
115 | | - where U : DataEntity<T>, new() |
116 | | - { |
117 | | - object id; |
| 92 | + private string GetQuotedDefaultCatalog() |
| 93 | + { |
| 94 | + var t = cfg.GetType(); |
| 95 | + var getQuotedDefaultCatalog = t.GetMethod("GetQuotedDefaultCatalog", BindingFlags.Instance | BindingFlags.NonPublic); |
118 | 96 |
|
119 | | - CurrentCulture = from; |
120 | | - using (var session = OpenSession()) |
121 | | - using (var tx = session.BeginTransaction()) |
122 | | - { |
123 | | - var entity = new U() |
124 | | - { |
125 | | - DataValue = expectedValue |
126 | | - }; |
127 | | - |
128 | | - id = session.Save(entity); |
129 | | - tx.Commit(); |
130 | | - } |
131 | | - |
132 | | - CurrentCulture = to; |
133 | | - using (var session = OpenSession()) |
134 | | - using (var tx = session.BeginTransaction()) |
135 | | - { |
136 | | - var entity = session.Get<U>(id); |
| 97 | + return (string) getQuotedDefaultCatalog.Invoke(cfg, [Dialect]); |
| 98 | + } |
137 | 99 |
|
138 | | - assert(expectedValue, entity.DataValue); |
139 | | - } |
140 | | - } |
| 100 | + private string GetQuotedDefaultSchema() |
| 101 | + { |
| 102 | + var t = cfg.GetType(); |
| 103 | + var getQuotedDefaultSchema = t.GetMethod("GetQuotedDefaultSchema", BindingFlags.Instance | BindingFlags.NonPublic); |
141 | 104 |
|
142 | | - [Test, TestCaseSource(nameof(GetTestCases))] |
143 | | - public void TestNHDateTime(CultureInfo from, CultureInfo to) |
144 | | - { |
145 | | - DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish)); |
| 105 | + return (string) getQuotedDefaultSchema.Invoke(cfg, [Dialect]); |
| 106 | + } |
146 | 107 |
|
147 | | - PerformTest<DateTime, NHDateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual)); |
148 | | - } |
| 108 | + private string GetQualifiedName(string catalog, string schema, string name) |
| 109 | + { |
| 110 | + return Dialect.Qualify(catalog, schema, name); |
| 111 | + } |
149 | 112 |
|
150 | | - [Test, TestCaseSource(nameof(GetTestCases))] |
151 | | - public void TestDateTime(CultureInfo from, CultureInfo to) |
| 113 | + private void PerformTest<T, U>(CultureInfo from, CultureInfo to, T expectedValue, Action<T, T> assert) |
| 114 | + where T : struct |
| 115 | + where U : DataEntity<T>, new() |
| 116 | + { |
| 117 | + object id; |
| 118 | + |
| 119 | + CurrentCulture = from; |
| 120 | + using (var session = OpenSession()) |
| 121 | + using (var tx = session.BeginTransaction()) |
152 | 122 | { |
153 | | - DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish)); |
| 123 | + var entity = new U() |
| 124 | + { |
| 125 | + DataValue = expectedValue |
| 126 | + }; |
154 | 127 |
|
155 | | - PerformTest<DateTime, DateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 128 | + id = session.Save(entity); |
| 129 | + tx.Commit(); |
156 | 130 | } |
157 | 131 |
|
158 | | - [Test, TestCaseSource(nameof(GetTestCases))] |
159 | | - public void TestDecimal(CultureInfo from, CultureInfo to) |
| 132 | + CurrentCulture = to; |
| 133 | + using (var session = OpenSession()) |
| 134 | + using (var tx = session.BeginTransaction()) |
160 | 135 | { |
161 | | - decimal decimalValue = 12.3m; |
| 136 | + var entity = session.Get<U>(id); |
162 | 137 |
|
163 | | - PerformTest<decimal, DecimalEntity>(from, to, decimalValue, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 138 | + assert(expectedValue, entity.DataValue); |
164 | 139 | } |
| 140 | + } |
165 | 141 |
|
166 | | - [Test, TestCaseSource(nameof(GetTestCases))] |
167 | | - public void TestDouble(CultureInfo from, CultureInfo to) |
168 | | - { |
169 | | - double doubleValue = 12.3d; |
| 142 | + [Test, TestCaseSource(nameof(GetTestCases))] |
| 143 | + public void TestNHDateTime(CultureInfo from, CultureInfo to) |
| 144 | + { |
| 145 | + DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish)); |
170 | 146 |
|
171 | | - PerformTest<double, DoubleEntity>(from, to, doubleValue, |
172 | | - (expected, actual) => Assert.True(Math.Abs(expected - actual) < double.Epsilon, $"Expected: {expected}\nBut was: {actual}\n") |
173 | | - ); |
174 | | - } |
| 147 | + PerformTest<DateTime, NHDateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 148 | + } |
175 | 149 |
|
176 | | - [Test, TestCaseSource(nameof(GetTestCases))] |
| 150 | + [Test, TestCaseSource(nameof(GetTestCases))] |
| 151 | + public void TestDateTime(CultureInfo from, CultureInfo to) |
| 152 | + { |
| 153 | + DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish)); |
177 | 154 |
|
178 | | - public void TestInteger(CultureInfo from, CultureInfo to) |
179 | | - { |
180 | | - int integerValue = 123; |
| 155 | + PerformTest<DateTime, DateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 156 | + } |
181 | 157 |
|
182 | | - PerformTest<int, IntegerEntity>(from, to, integerValue, (expected, actual) => Assert.AreEqual(expected, actual)); |
183 | | - } |
| 158 | + [Test, TestCaseSource(nameof(GetTestCases))] |
| 159 | + public void TestDecimal(CultureInfo from, CultureInfo to) |
| 160 | + { |
| 161 | + decimal decimalValue = 12.3m; |
184 | 162 |
|
185 | | - private CultureInfo CurrentCulture |
186 | | - { |
187 | | - get => CultureInfo.CurrentCulture; |
188 | | - set => CultureInfo.CurrentCulture = value; |
189 | | - } |
| 163 | + PerformTest<decimal, DecimalEntity>(from, to, decimalValue, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 164 | + } |
190 | 165 |
|
191 | | - public static object[][] GetTestCases() |
192 | | - { |
193 | | - return [ |
194 | | - [new CultureInfo("en-US"), new CultureInfo("de-DE")], |
195 | | - [new CultureInfo("en-US"), new CultureInfo("ar-SA", false)], |
196 | | - [new CultureInfo("en-US"), new CultureInfo("th-TH", false)], |
197 | | - ]; |
198 | | - } |
| 166 | + [Test, TestCaseSource(nameof(GetTestCases))] |
| 167 | + public void TestDouble(CultureInfo from, CultureInfo to) |
| 168 | + { |
| 169 | + double doubleValue = 12.3d; |
| 170 | + |
| 171 | + PerformTest<double, DoubleEntity>(from, to, doubleValue, |
| 172 | + (expected, actual) => Assert.True(Math.Abs(expected - actual) < double.Epsilon, $"Expected: {expected}\nBut was: {actual}\n") |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + [Test, TestCaseSource(nameof(GetTestCases))] |
| 177 | + |
| 178 | + public void TestInteger(CultureInfo from, CultureInfo to) |
| 179 | + { |
| 180 | + int integerValue = 123; |
| 181 | + |
| 182 | + PerformTest<int, IntegerEntity>(from, to, integerValue, (expected, actual) => Assert.AreEqual(expected, actual)); |
| 183 | + } |
| 184 | + |
| 185 | + private CultureInfo CurrentCulture |
| 186 | + { |
| 187 | + get => CultureInfo.CurrentCulture; |
| 188 | + set => CultureInfo.CurrentCulture = value; |
| 189 | + } |
| 190 | + |
| 191 | + public static object[][] GetTestCases() |
| 192 | + { |
| 193 | + return [ |
| 194 | + [new CultureInfo("en-US"), new CultureInfo("de-DE")], |
| 195 | + [new CultureInfo("en-US"), new CultureInfo("ar-SA", false)], |
| 196 | + [new CultureInfo("en-US"), new CultureInfo("th-TH", false)], |
| 197 | + ]; |
199 | 198 | } |
200 | 199 | } |
0 commit comments