Skip to content

Commit ad4c501

Browse files
committed
Generate tests for GetFieldValueAsync.
1 parent 44a12f8 commit ad4c501

File tree

2 files changed

+1632
-9
lines changed

2 files changed

+1632
-9
lines changed

src/AdoNet.Specification.Tests/GetValueConversionTestBase.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data;
33
using System.Data.Common;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using Xunit;
67
using Xunit.Sdk;
78

@@ -23,9 +24,9 @@ protected GetValueConversionTestBase(TFixture fixture)
2324
protected virtual void TestGetFieldValue<T>(DbType dbType, ValueKind kind, T expected) => DoTest(dbType, kind, reader => Assert.Equal(expected, reader.GetFieldValue<T>(0)));
2425
protected virtual void TestGetValue<T>(DbType dbType, ValueKind kind, T expected) => DoTest(dbType, kind, reader => Assert.Equal(expected, reader.GetValue(0)));
2526
protected virtual void TestGetValue<T>(DbType dbType, ValueKind kind, Func<DbDataReader, T> getValue, T expected) => DoTest(dbType, kind, reader => Assert.Equal(expected, getValue(reader)));
27+
protected virtual async Task TestGetValueAsync<T>(DbType dbType, ValueKind kind, Func<DbDataReader, Task<T>> getValue, T expected) => await DoTestAsync(dbType, kind, async reader => Assert.Equal(expected, await getValue(reader)));
2628

27-
protected virtual void TestException<T>(DbType dbType, ValueKind kind, Func<DbDataReader, T> getValue, Type exceptionType)
28-
{
29+
protected virtual void TestException<T>(DbType dbType, ValueKind kind, Func<DbDataReader, T> getValue, Type exceptionType) =>
2930
DoTest(dbType, kind, reader =>
3031
{
3132
try
@@ -45,7 +46,28 @@ protected virtual void TestException<T>(DbType dbType, ValueKind kind, Func<DbDa
4546
throw new ThrowsException(exceptionType, ex);
4647
}
4748
});
48-
}
49+
50+
51+
protected virtual async Task TestExceptionAsync<T>(DbType dbType, ValueKind kind, Func<DbDataReader, Task<T>> getValue, Type exceptionType) =>
52+
await DoTestAsync(dbType, kind, async reader =>
53+
{
54+
try
55+
{
56+
var value = await getValue(reader);
57+
throw new UnexpectedValueException(value);
58+
}
59+
catch (UnexpectedValueException)
60+
{
61+
throw;
62+
}
63+
catch (Exception ex) when (ex.GetType() == exceptionType)
64+
{
65+
}
66+
catch (Exception ex)
67+
{
68+
throw new ThrowsException(exceptionType, ex);
69+
}
70+
});
4971

5072
protected virtual void DoTest(DbType dbType, ValueKind kind, Action<DbDataReader> action)
5173
{
@@ -59,5 +81,18 @@ protected virtual void DoTest(DbType dbType, ValueKind kind, Action<DbDataReader
5981
Assert.True(reader.Read());
6082
action(reader);
6183
}
84+
85+
protected virtual async Task DoTestAsync(DbType dbType, ValueKind kind, Func<DbDataReader, Task> action)
86+
{
87+
if (!Fixture.SupportedDbTypes.Contains(dbType))
88+
throw new SkipException("Database doesn't support this data type");
89+
90+
using var connection = CreateOpenConnection();
91+
using var command = connection.CreateCommand();
92+
command.CommandText = Fixture.CreateSelectSql(dbType, kind);
93+
using var reader = command.ExecuteReader();
94+
Assert.True(reader.Read());
95+
await action(reader);
96+
}
6297
}
6398
}

0 commit comments

Comments
 (0)