1- using System . Collections ;
21using System . Data ;
32using Xunit ;
43using Ydb . Sdk . Value ;
@@ -8,30 +7,27 @@ namespace Ydb.Sdk.Ado.Tests;
87public class YdbCommandTests : TestBase
98{
109 [ Theory ]
11- [ ClassData ( typeof ( TestDataGenerator ) ) ]
12- public async Task ExecuteScalarAsync_WhenSetYdbParameter_ReturnThisValue < T > ( Data < T > data )
10+ [ MemberData ( nameof ( DbTypeTestCases ) ) ]
11+ [ MemberData ( nameof ( DbTypeTestNullCases ) ) ]
12+ public async Task ExecuteScalarAsync_WhenSetYdbParameter_ReturnThisValue ( DbType dbType , object ? value ,
13+ bool isNullable )
1314 {
1415 await using var connection = await CreateOpenConnectionAsync ( ) ;
1516 var dbCommand = connection . CreateCommand ( ) ;
1617 dbCommand . CommandText = "SELECT @var as var;" ;
1718
1819 var dbParameter = new YdbParameter
19- {
20- ParameterName = "var" ,
21- DbType = data . DbType ,
22- Value = data . Expected ,
23- IsNullable = data . IsNullable
24- } ;
20+ { ParameterName = "var" , DbType = dbType , Value = value , IsNullable = isNullable } ;
2521
2622 dbCommand . Parameters . Add ( dbParameter ) ;
2723
28- Assert . Equal ( data . Expected == null ? DBNull . Value : data . Expected , await dbCommand . ExecuteScalarAsync ( ) ) ;
24+ Assert . Equal ( value ?? DBNull . Value , await dbCommand . ExecuteScalarAsync ( ) ) ;
2925 var ydbDataReader = await dbCommand . ExecuteReaderAsync ( ) ;
3026 Assert . Equal ( 1 , ydbDataReader . FieldCount ) ;
3127 Assert . Equal ( "var" , ydbDataReader . GetName ( 0 ) ) ;
32- if ( ! data . IsNullable )
28+ if ( value != null )
3329 {
34- Assert . Equal ( typeof ( T ) , ydbDataReader . GetFieldType ( 0 ) ) ;
30+ Assert . Equal ( value . GetType ( ) , ydbDataReader . GetFieldType ( 0 ) ) ;
3531 }
3632
3733 while ( await ydbDataReader . NextResultAsync ( ) )
@@ -40,23 +36,20 @@ public async Task ExecuteScalarAsync_WhenSetYdbParameter_ReturnThisValue<T>(Data
4036 }
4137
4238 [ Theory ]
43- [ ClassData ( typeof ( TestDataGenerator ) ) ]
44- public async Task ExecuteScalarAsync_WhenSetYdbParameterThenPrepare_ReturnThisValue < T > ( Data < T > data )
39+ [ MemberData ( nameof ( DbTypeTestCases ) ) ]
40+ [ MemberData ( nameof ( DbTypeTestNullCases ) ) ]
41+ public async Task ExecuteScalarAsync_WhenSetYdbParameterThenPrepare_ReturnThisValue ( DbType dbType , object ? value ,
42+ bool isNullable )
4543 {
4644 await using var connection = await CreateOpenConnectionAsync ( ) ;
4745 var dbCommand = connection . CreateCommand ( ) ;
4846 dbCommand . CommandText = "SELECT @var;" ;
4947
5048 var dbParameter = new YdbParameter
51- {
52- ParameterName = "@var" ,
53- DbType = data . DbType ,
54- Value = data . Expected ,
55- IsNullable = data . IsNullable
56- } ;
49+ { ParameterName = "@var" , DbType = dbType , Value = value , IsNullable = isNullable } ;
5750 dbCommand . Parameters . Add ( dbParameter ) ;
5851
59- Assert . Equal ( data . Expected == null ? DBNull . Value : data . Expected , await dbCommand . ExecuteScalarAsync ( ) ) ;
52+ Assert . Equal ( value ?? DBNull . Value , await dbCommand . ExecuteScalarAsync ( ) ) ;
6053 }
6154
6255 [ Fact ]
@@ -83,27 +76,22 @@ public async Task ExecuteReaderAsync_WhenOptionalIsNull_ThrowFieldIsNull()
8376 }
8477
8578 [ Theory ]
86- [ ClassData ( typeof ( TestDataGenerator ) ) ]
87- public async Task ExecuteScalarAsync_WhenDbTypeIsObject_ReturnThisValue < T > ( Data < T > data )
79+ [ MemberData ( nameof ( DbTypeTestCases ) ) ]
80+ public async Task ExecuteScalarAsync_WhenDbTypeIsObject_ReturnThisValue ( DbType _ , object value , bool isNullable )
8881 {
89- if ( data . IsNullable )
90- {
91- return ;
92- }
93-
9482 await using var connection = await CreateOpenConnectionAsync ( ) ;
9583 var dbCommand = connection . CreateCommand ( ) ;
9684 dbCommand . CommandText = "SELECT @var;" ;
9785
9886 var dbParameter = new YdbParameter
9987 {
10088 ParameterName = "@var" ,
101- Value = data . Expected ,
102- IsNullable = data . IsNullable
89+ Value = value ,
90+ IsNullable = isNullable
10391 } ;
10492 dbCommand . Parameters . Add ( dbParameter ) ;
10593
106- Assert . Equal ( data . Expected , await dbCommand . ExecuteScalarAsync ( ) ) ;
94+ Assert . Equal ( value , await dbCommand . ExecuteScalarAsync ( ) ) ;
10795 }
10896
10997 [ Fact ]
@@ -177,7 +165,6 @@ public void ExecuteDbDataReader_WhenPreviousIsNotClosed_ThrowException()
177165 Assert . True ( ydbDataReader . IsClosed ) ;
178166 }
179167
180-
181168 [ Fact ]
182169 public async Task ExecuteScalar_WhenSelectNull_ReturnDbNull ( )
183170 {
@@ -261,75 +248,87 @@ public class Data<T>(DbType dbType, T expected, bool isNullable = false)
261248 public T Expected { get ; } = expected ;
262249 }
263250
264- private class TestDataGenerator : IEnumerable < object [ ] >
251+
252+ public static readonly TheoryData < DbType , object , bool > DbTypeTestCases = new ( )
265253 {
266- private readonly List < object [ ] > _data =
267- [
268- new object [ ] { new Data < bool > ( DbType . Boolean , true ) } ,
269- new object [ ] { new Data < bool > ( DbType . Boolean , false ) } ,
270- new object [ ] { new Data < bool ? > ( DbType . Boolean , true , true ) } ,
271- new object [ ] { new Data < bool ? > ( DbType . Boolean , false , true ) } ,
272- new object [ ] { new Data < bool ? > ( DbType . Boolean , null ) } ,
273- new object [ ] { new Data < sbyte > ( DbType . SByte , - 1 ) } ,
274- new object [ ] { new Data < sbyte ? > ( DbType . SByte , - 2 , true ) } ,
275- new object [ ] { new Data < sbyte ? > ( DbType . SByte , null ) } ,
276- new object [ ] { new Data < byte > ( DbType . Byte , 200 ) } ,
277- new object [ ] { new Data < byte ? > ( DbType . Byte , 228 , true ) } ,
278- new object [ ] { new Data < byte ? > ( DbType . Byte , null ) } ,
279- new object [ ] { new Data < short > ( DbType . Int16 , 14000 ) } ,
280- new object [ ] { new Data < short ? > ( DbType . Int16 , 14000 , true ) } ,
281- new object [ ] { new Data < short ? > ( DbType . Int16 , null ) } ,
282- new object [ ] { new Data < ushort > ( DbType . UInt16 , 40_000 ) } ,
283- new object [ ] { new Data < ushort ? > ( DbType . UInt16 , 40_000 , true ) } ,
284- new object [ ] { new Data < ushort ? > ( DbType . UInt16 , null ) } ,
285- new object [ ] { new Data < int > ( DbType . Int32 , - 40_000 ) } ,
286- new object [ ] { new Data < int ? > ( DbType . Int32 , - 40_000 , true ) } ,
287- new object [ ] { new Data < int ? > ( DbType . Int32 , null ) } ,
288- new object [ ] { new Data < uint > ( DbType . UInt32 , 4_000_000_000 ) } ,
289- new object [ ] { new Data < uint ? > ( DbType . UInt32 , 4_000_000_000 , true ) } ,
290- new object [ ] { new Data < uint ? > ( DbType . UInt32 , null ) } ,
291- new object [ ] { new Data < long > ( DbType . Int64 , - 4_000_000_000 ) } ,
292- new object [ ] { new Data < long ? > ( DbType . Int64 , - 4_000_000_000 , true ) } ,
293- new object [ ] { new Data < long ? > ( DbType . Int64 , null ) } ,
294- new object [ ] { new Data < ulong > ( DbType . UInt64 , 10_000_000_000ul ) } ,
295- new object [ ] { new Data < ulong ? > ( DbType . UInt64 , 10_000_000_000ul , true ) } ,
296- new object [ ] { new Data < ulong ? > ( DbType . UInt64 , null ) } ,
297- new object [ ] { new Data < float > ( DbType . Single , - 1.7f ) } ,
298- new object [ ] { new Data < float ? > ( DbType . Single , - 1.7f , true ) } ,
299- new object [ ] { new Data < float ? > ( DbType . Single , null ) } ,
300- new object [ ] { new Data < double > ( DbType . Double , 123.45 ) } ,
301- new object [ ] { new Data < double ? > ( DbType . Double , 123.45 , true ) } ,
302- new object [ ] { new Data < double ? > ( DbType . Double , null ) } ,
303- new object [ ] { new Data < Guid > ( DbType . Guid , new Guid ( "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" ) ) } ,
304- new object [ ] { new Data < Guid ? > ( DbType . Guid , new Guid ( "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" ) , true ) } ,
305- new object [ ] { new Data < Guid ? > ( DbType . Guid , null ) } ,
306- new object [ ] { new Data < DateTime > ( DbType . Date , new DateTime ( 2021 , 08 , 21 ) ) } ,
307- new object [ ] { new Data < DateTime ? > ( DbType . Date , new DateTime ( 2021 , 08 , 21 ) , true ) } ,
308- new object [ ] { new Data < DateTime ? > ( DbType . Date , null ) } ,
309- new object [ ] { new Data < DateTime > ( DbType . DateTime , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 ) ) } ,
310- new object [ ] { new Data < DateTime ? > ( DbType . DateTime , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 ) , true ) } ,
311- new object [ ] { new Data < DateTime ? > ( DbType . DateTime , null ) } ,
312- new object [ ] { new Data < DateTime > ( DbType . DateTime2 , DateTime . Parse ( "2029-08-03T06:59:44.8578730Z" ) ) } ,
313- new object [ ] { new Data < DateTime > ( DbType . DateTime2 , DateTime . Parse ( "2029-08-09T17:15:29.6935850Z" ) ) } ,
314- new object [ ]
315- {
316- new Data < DateTime ? > ( DbType . DateTime2 , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 , 581 , DateTimeKind . Local ) ,
317- true )
318- } ,
319- new object [ ] { new Data < DateTime ? > ( DbType . DateTime2 , null ) } ,
320- new object [ ] { new Data < byte [ ] > ( DbType . Binary , "test str"u8 . ToArray ( ) ) } ,
321- new object [ ] { new Data < byte [ ] ? > ( DbType . Binary , "test str"u8 . ToArray ( ) , true ) } ,
322- new object [ ] { new Data < byte [ ] ? > ( DbType . Binary , null ) } ,
323- new object [ ] { new Data < string > ( DbType . String , "unicode str" ) } ,
324- new object [ ] { new Data < string ? > ( DbType . String , "unicode str" , true ) } ,
325- new object [ ] { new Data < string ? > ( DbType . String , null ) } ,
326- new object [ ] { new Data < decimal > ( DbType . Decimal , - 18446744073.709551616m ) } ,
327- new object [ ] { new Data < decimal ? > ( DbType . Decimal , - 18446744073.709551616m , true ) } ,
328- new object [ ] { new Data < decimal ? > ( DbType . Decimal , null ) }
329- ] ;
330-
331- public IEnumerator < object [ ] > GetEnumerator ( ) => _data . GetEnumerator ( ) ;
332-
333- IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
334- }
254+ { DbType . Boolean , true , false } ,
255+ { DbType . Boolean , false , false } ,
256+ { DbType . Boolean , true , true } ,
257+ { DbType . Boolean , false , true } ,
258+ { DbType . SByte , ( sbyte ) - 1 , false } ,
259+ { DbType . SByte , ( sbyte ) - 2 , true } ,
260+ { DbType . Byte , ( byte ) 200 , false } ,
261+ { DbType . Byte , ( byte ) 228 , true } ,
262+ { DbType . Int16 , ( short ) 14000 , false } ,
263+ { DbType . Int16 , ( short ) 14000 , true } ,
264+ { DbType . UInt16 , ( ushort ) 40_000 , false } ,
265+ { DbType . UInt16 , ( ushort ) 40_000 , true } ,
266+ { DbType . Int32 , - 40_000 , false } ,
267+ { DbType . Int32 , - 40_000 , true } ,
268+ { DbType . UInt32 , 4_000_000_000 , true } ,
269+ { DbType . UInt32 , 4_000_000_000 , true } ,
270+ { DbType . Int64 , - 4_000_000_000 , false } ,
271+ { DbType . Int64 , - 4_000_000_000 , true } ,
272+ { DbType . UInt64 , 10_000_000_000ul , false } ,
273+ { DbType . UInt64 , 10_000_000_000ul , true } ,
274+ { DbType . Single , - 1.7f , false } ,
275+ { DbType . Single , - 1.7f , true } ,
276+ { DbType . Double , 123.45 , false } ,
277+ { DbType . Double , 123.45 , true } ,
278+ { DbType . Guid , new Guid ( "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" ) , false } ,
279+ { DbType . Guid , new Guid ( "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" ) , true } ,
280+ { DbType . Date , new DateTime ( 2021 , 08 , 21 ) , false } ,
281+ { DbType . Date , new DateTime ( 2021 , 08 , 21 ) , true } ,
282+ { DbType . DateTime , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 ) , false } ,
283+ { DbType . DateTime , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 ) , true } ,
284+ { DbType . DateTime2 , DateTime . Parse ( "2029-08-03T06:59:44.8578730Z" ) , false } ,
285+ { DbType . DateTime2 , DateTime . Parse ( "2029-08-09T17:15:29.6935850Z" ) , false } ,
286+ { DbType . DateTime2 , new DateTime ( 2021 , 08 , 21 , 23 , 30 , 47 , 581 , DateTimeKind . Local ) , true } ,
287+ { DbType . Binary , "test str"u8 . ToArray ( ) , false } ,
288+ { DbType . Binary , "test str"u8 . ToArray ( ) , true } ,
289+ { DbType . String , "unicode str" , false } ,
290+ { DbType . String , "unicode str" , true } ,
291+ { DbType . Decimal , - 18446744073.709551616m , false } ,
292+ { DbType . Decimal , - 18446744073.709551616m , true } ,
293+ } ;
294+
295+ public static readonly TheoryData < DbType , object ? , bool > DbTypeTestNullCases = new ( )
296+ {
297+ { DbType . Boolean , null , false } ,
298+ { DbType . Boolean , null , true } ,
299+ { DbType . SByte , null , false } ,
300+ { DbType . SByte , null , true } ,
301+ { DbType . Byte , null , false } ,
302+ { DbType . Byte , null , true } ,
303+ { DbType . Int16 , null , false } ,
304+ { DbType . Int16 , null , true } ,
305+ { DbType . UInt16 , null , false } ,
306+ { DbType . UInt16 , null , true } ,
307+ { DbType . Int32 , null , false } ,
308+ { DbType . Int32 , null , true } ,
309+ { DbType . UInt32 , null , false } ,
310+ { DbType . UInt32 , null , true } ,
311+ { DbType . Int64 , null , false } ,
312+ { DbType . Int64 , null , true } ,
313+ { DbType . UInt64 , null , false } ,
314+ { DbType . UInt64 , null , true } ,
315+ { DbType . Single , null , false } ,
316+ { DbType . Single , null , true } ,
317+ { DbType . Double , null , false } ,
318+ { DbType . Double , null , true } ,
319+ { DbType . Guid , null , false } ,
320+ { DbType . Guid , null , true } ,
321+ { DbType . Date , null , false } ,
322+ { DbType . Date , null , true } ,
323+ { DbType . DateTime , null , false } ,
324+ { DbType . DateTime , null , true } ,
325+ { DbType . DateTime2 , null , false } ,
326+ { DbType . DateTime2 , null , true } ,
327+ { DbType . Binary , null , false } ,
328+ { DbType . Binary , null , true } ,
329+ { DbType . String , null , false } ,
330+ { DbType . String , null , true } ,
331+ { DbType . Decimal , null , false } ,
332+ { DbType . Decimal , null , true }
333+ } ;
335334}
0 commit comments