2
2
using System . Data ;
3
3
using System . Data . Common ;
4
4
using System . Linq ;
5
+ using System . Threading . Tasks ;
5
6
using Xunit ;
6
7
using Xunit . Sdk ;
7
8
@@ -23,9 +24,9 @@ protected GetValueConversionTestBase(TFixture fixture)
23
24
protected virtual void TestGetFieldValue < T > ( DbType dbType , ValueKind kind , T expected ) => DoTest ( dbType , kind , reader => Assert . Equal ( expected , reader . GetFieldValue < T > ( 0 ) ) ) ;
24
25
protected virtual void TestGetValue < T > ( DbType dbType , ValueKind kind , T expected ) => DoTest ( dbType , kind , reader => Assert . Equal ( expected , reader . GetValue ( 0 ) ) ) ;
25
26
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 ) ) ) ;
26
28
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 ) =>
29
30
DoTest ( dbType , kind , reader =>
30
31
{
31
32
try
@@ -45,7 +46,28 @@ protected virtual void TestException<T>(DbType dbType, ValueKind kind, Func<DbDa
45
46
throw new ThrowsException ( exceptionType , ex ) ;
46
47
}
47
48
} ) ;
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
+ } ) ;
49
71
50
72
protected virtual void DoTest ( DbType dbType , ValueKind kind , Action < DbDataReader > action )
51
73
{
@@ -59,5 +81,18 @@ protected virtual void DoTest(DbType dbType, ValueKind kind, Action<DbDataReader
59
81
Assert . True ( reader . Read ( ) ) ;
60
82
action ( reader ) ;
61
83
}
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
+ }
62
97
}
63
98
}
0 commit comments