Skip to content

Commit

Permalink
Generate async files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed May 15, 2024
1 parent 6f7c2e9 commit db08376
Showing 1 changed file with 36 additions and 82 deletions.
118 changes: 36 additions & 82 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3530/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,28 @@ protected override void OnTearDown()
}

protected override void CreateSchema()
{
CreateTable("Integer");
CreateTable("DateTime");
CreateTable("Double");
CreateTable("Decimal");
}

private void CreateTable(string name)
{
var sb = new StringBuilder();
var guidType = Dialect.GetTypeName(SqlTypeFactory.Guid);
var stringType = Dialect.GetTypeName(SqlTypeFactory.GetAnsiString(255));

var catalog = GetQuotedDefaultCatalog();
var schema = GetQuotedDefaultSchema();
var table = GetQualifiedName(catalog, schema, "LocaleEntity");
var table = GetQualifiedName(catalog, schema, $"{name}Entity");

sb.Append($"{Dialect.CreateTableString} {table} (");

// Generate columns
sb.Append($"Id {guidType}, ");
sb.Append($"IntegerValue {stringType}, ");
sb.Append($"DateTimeValue {stringType}, ");
sb.Append($"DoubleValue {stringType}, ");
sb.Append($"DecimalValue {stringType}");
sb.Append($"Value {stringType}, ");

// Add the primary key contraint for the identity column
sb.Append($", {Dialect.PrimaryKeyString} ( Id )");
Expand Down Expand Up @@ -93,7 +98,7 @@ private string GetQuotedDefaultCatalog()
var t = cfg.GetType();
var getQuotedDefaultCatalog = t.GetMethod("GetQuotedDefaultCatalog", BindingFlags.Instance | BindingFlags.NonPublic);

return (string)getQuotedDefaultCatalog.Invoke(cfg, [Dialect]);
return (string) getQuotedDefaultCatalog.Invoke(cfg, [Dialect]);
}

private string GetQuotedDefaultSchema()
Expand All @@ -109,119 +114,68 @@ private string GetQualifiedName(string catalog, string schema, string name)
return Dialect.Qualify(catalog, schema, name);
}

[Test, TestCaseSource(nameof(GetTestCases))]
public async Task TestDateTimeAsync(CultureInfo from, CultureInfo to)
private async Task PerformTestAsync<T, U>(CultureInfo from, CultureInfo to, T expectedValue, Action<T, T> assert, CancellationToken cancellationToken = default(CancellationToken))
where T : struct
where U : Entity<T>, new()
{
DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish));
object id;

CurrentCulture = from;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = new LocaleEntity()
var entity = new U()
{
DateTimeValue = leapDay
Value = expectedValue
};

id = await (session.SaveAsync(entity));
await (tx.CommitAsync());
id = await (session.SaveAsync(entity, cancellationToken));
await (tx.CommitAsync(cancellationToken));
}

CurrentCulture = to;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = await (session.GetAsync<LocaleEntity>(id));
var entity = await (session.GetAsync<U>(id, cancellationToken));

Assert.AreEqual(leapDay, entity.DateTimeValue);
assert(expectedValue, entity.Value);
}
}

[Test, TestCaseSource(nameof(GetTestCases))]
public async Task TestDecimalAsync(CultureInfo from, CultureInfo to)
public async Task TestDateTimeAsync(CultureInfo from, CultureInfo to)
{
decimal decimalValue = 12.3m;
object id;

CurrentCulture = from;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = new LocaleEntity()
{
DecimalValue = decimalValue
};
DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish));

id = await (session.SaveAsync(entity));
await (tx.CommitAsync());
}
await (PerformTestAsync<DateTime, DateTimeEntity>(from, to, leapDay, (expected, actual) => Assert.AreEqual(expected, actual)));
}

CurrentCulture = to;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = await (session.GetAsync<LocaleEntity>(id));
[Test, TestCaseSource(nameof(GetTestCases))]
public async Task TestDecimalAsync(CultureInfo from, CultureInfo to)
{
decimal decimalValue = 12.3m;

Assert.AreEqual(decimalValue, entity.DecimalValue);
}
await (PerformTestAsync<decimal, DecimalEntity>(from, to, decimalValue, (expected, actual) => Assert.AreEqual(expected, actual)));
}

[Test, TestCaseSource(nameof(GetTestCases))]
public async Task TestDoubleAsync(CultureInfo from, CultureInfo to)
{
double doubleValue = 12.3d;
object id;

CurrentCulture = from;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = new LocaleEntity()
{
DoubleValue = doubleValue
};

id = await (session.SaveAsync(entity));
await (tx.CommitAsync());
}

CurrentCulture = to;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = await (session.GetAsync<LocaleEntity>(id));

Assert.True(Math.Abs(doubleValue - entity.DoubleValue) < double.Epsilon, $"Expected: {doubleValue}\nBut was: {entity.DoubleValue}\n");
}
await (PerformTestAsync<double, DoubleEntity>(from, to, doubleValue,
(expected, actual) => Assert.True(Math.Abs(expected - actual) < double.Epsilon, $"Expected: {expected}\nBut was: {actual}\n")
));
}

public async Task TestIntegerAsync(CultureInfo from, CultureInfo to, CancellationToken cancellationToken = default(CancellationToken))
[Test, TestCaseSource(nameof(GetTestCases))]

public async Task TestIntegerAsync(CultureInfo from, CultureInfo to)
{
int integerValue = 123;
object id;

CurrentCulture = from;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = new LocaleEntity()
{
IntegerValue = integerValue
};

id = await (session.SaveAsync(entity, cancellationToken));
await (tx.CommitAsync(cancellationToken));
}

CurrentCulture = to;
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var entity = await (session.GetAsync<LocaleEntity>(id, cancellationToken));

Assert.AreEqual(integerValue, entity.IntegerValue);
}
await (PerformTestAsync<int, IntegerEntity>(from, to, integerValue, (expected, actual) => Assert.AreEqual(expected, actual)));
}

private CultureInfo CurrentCulture
Expand Down

0 comments on commit db08376

Please sign in to comment.