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