varstr=awaitconnection.QueryFirstAsync<string>(newCommandDefinition("select 'abc' as [Value] union all select @txt",new{txt="def"})).ConfigureAwait(false);
varstr=awaitconnection.QueryFirstOrDefaultAsync<string>(newCommandDefinition("select null as [Value] union all select @txt",new{txt="def"})).ConfigureAwait(false);
varstr=awaitconnection.QuerySingleOrDefaultAsync("select null as [Value]").ConfigureAwait(false);
Assert.Null(str.Value);
}
[Fact]
...
...
@@ -55,7 +83,7 @@ public async Task TestBasicStringUsageAsyncNonBuffered()
{
varquery=awaitconnection.QueryAsync<string>(newCommandDefinition("select 'abc' as [Value] union all select @txt",new{txt="def"},flags:CommandFlags.None)).ConfigureAwait(false);
vararr=query.ToArray();
arr.IsSequenceEqualTo(new[]{"abc","def"});
Assert.Equal(new[]{"abc","def"},arr);
}
[Fact]
...
...
@@ -72,7 +100,7 @@ public void TestLongOperationWithCancellation()
}
catch(AggregateExceptionagg)
{
(agg.InnerExceptionisSqlException).IsTrue();
Assert.True(agg.InnerExceptionisSqlException);
}
}
...
...
@@ -81,7 +109,7 @@ public async Task TestBasicStringUsageClosedAsync()
{
varquery=awaitconnection.QueryAsync<string>("select 'abc' as [Value] union all select @txt",new{txt="def"}).ConfigureAwait(false);
vararr=query.ToArray();
arr.IsSequenceEqualTo(new[]{"abc","def"});
Assert.Equal(new[]{"abc","def"},arr);
}
[Fact]
...
...
@@ -89,7 +117,7 @@ public async Task TestQueryDynamicAsync()
{
varrow=(awaitconnection.QueryAsync("select 'abc' as [Value]").ConfigureAwait(false)).Single();
stringvalue=row.Value;
value.IsEqualTo("abc");
Assert.Equal("abc",value);
}
[Fact]
...
...
@@ -97,7 +125,7 @@ public async Task TestClassWithStringUsageAsync()
{
varquery=awaitconnection.QueryAsync<BasicType>("select 'abc' as [Value] union all select @txt",new{txt="def"}).ConfigureAwait(false);
vardata0=(awaitconnection.QueryAsync<AsyncFoo0>("select 1 as [Id] where 1 = 0").ConfigureAwait(false)).ToList();
data0.Count.IsEqualTo(0);
Assert.Empty(data0);
vardata1=(awaitconnection.QueryAsync<AsyncFoo1>(newCommandDefinition("select 1 as [Id] where 1 = 0",flags:CommandFlags.Buffered)).ConfigureAwait(false)).ToList();
data1.Count.IsEqualTo(0);
Assert.Empty(data1);
vardata2=(awaitconnection.QueryAsync<AsyncFoo2>(newCommandDefinition("select 1 as [Id] where 1 = 0",flags:CommandFlags.None)).ConfigureAwait(false)).ToList();
data2.Count.IsEqualTo(0);
Assert.Empty(data2);
data0=(awaitconnection.QueryAsync<AsyncFoo0>("select 1 as [Id] where 1 = 0").ConfigureAwait(false)).ToList();
data0.Count.IsEqualTo(0);
Assert.Empty(data0);
data1=(awaitconnection.QueryAsync<AsyncFoo1>(newCommandDefinition("select 1 as [Id] where 1 = 0",flags:CommandFlags.Buffered)).ConfigureAwait(false)).ToList();
data1.Count.IsEqualTo(0);
Assert.Empty(data1);
data2=(awaitconnection.QueryAsync<AsyncFoo2>(newCommandDefinition("select 1 as [Id] where 1 = 0",flags:CommandFlags.None)).ConfigureAwait(false)).ToList();
data2.Count.IsEqualTo(0);
Assert.Empty(data2);
}
privateclassAsyncFoo0{publicintId{get;set;}}
...
...
@@ -651,12 +679,12 @@ public async Task TestSchemaChangedViaFirstOrDefaultAsync()
try
{
vard=awaitconnection.QueryFirstOrDefaultAsync<Dog>("select * from #dog").ConfigureAwait(false);
d.Name.IsEqualTo("Alf");
d.Age.IsEqualTo(1);
Assert.Equal("Alf",d.Name);
Assert.Equal(1,d.Age);
connection.Execute("alter table #dog drop column Name");
d=awaitconnection.QueryFirstOrDefaultAsync<Dog>("select * from #dog").ConfigureAwait(false);
d.Name.IsNull();
d.Age.IsEqualTo(1);
Assert.Null(d.Name);
Assert.Equal(1,d.Age);
}
finally
{
...
...
@@ -723,26 +751,26 @@ public async Task TestMultiMapArbitraryMapsAsync()
NoDefaultConstructorWithEnumnodef=connection.Query<NoDefaultConstructorWithEnum>("select cast(2 as smallint) E1, cast(5 as smallint) n1, cast(null as smallint) n2").First();
nodef.E.IsEqualTo(ShortEnum.Two);
nodef.NE1.IsEqualTo(ShortEnum.Five);
nodef.NE2.IsEqualTo(null);
Assert.Equal(ShortEnum.Two,nodef.E);
Assert.Equal(ShortEnum.Five,nodef.NE1);
Assert.Null(nodef.NE2);
}
[Fact]
...
...
@@ -77,10 +77,10 @@ public void ExplicitConstructors()
connection.Query<int>("select 1 union all select 2 union all select 3")
.IsSequenceEqualTo(new[]{1,2,3});
Assert.Equal(new[]{1,2,3},connection.Query<int>("select 1 union all select 2 union all select 3"));
}
[Fact]
...
...
@@ -125,12 +124,12 @@ public void TestSchemaChanged()
try
{
vard=connection.Query<Dog>("select * from #dog").Single();
d.Name.IsEqualTo("Alf");
d.Age.IsEqualTo(1);
Assert.Equal("Alf",d.Name);
Assert.Equal(1,d.Age);
connection.Execute("alter table #dog drop column Name");
d=connection.Query<Dog>("select * from #dog").Single();
d.Name.IsNull();
d.Age.IsEqualTo(1);
Assert.Null(d.Name);
Assert.Equal(1,d.Age);
}
finally
{
...
...
@@ -145,12 +144,12 @@ public void TestSchemaChangedViaFirstOrDefault()
try
{
vard=connection.QueryFirstOrDefault<Dog>("select * from #dog");
d.Name.IsEqualTo("Alf");
d.Age.IsEqualTo(1);
Assert.Equal("Alf",d.Name);
Assert.Equal(1,d.Age);
connection.Execute("alter table #dog drop column Name");
d=connection.QueryFirstOrDefault<Dog>("select * from #dog");
d.Name.IsNull();
d.Age.IsEqualTo(1);
Assert.Null(d.Name);
Assert.Equal(1,d.Age);
}
finally
{
...
...
@@ -162,29 +161,38 @@ public void TestSchemaChangedViaFirstOrDefault()
publicvoidTest_Single_First_Default()
{
varsql="select 0 where 1 = 0;";// no rows
try{connection.QueryFirst<int>(sql);Assert.Fail("QueryFirst, 0");}catch(InvalidOperationExceptionex){ex.Message.IsEqualTo("Sequence contains no elements");}
try{connection.QuerySingle<int>(sql);Assert.Fail("QuerySingle, 0");}catch(InvalidOperationExceptionex){ex.Message.IsEqualTo("Sequence contains no elements");}
sql="select 2 union select 3 order by 1;";// two rows
connection.QueryFirst<int>(sql).IsEqualTo(2);
try{connection.QuerySingle<int>(sql);Assert.Fail("QuerySingle, 2");}catch(InvalidOperationExceptionex){ex.Message.IsEqualTo("Sequence contains more than one element");}
try{connection.QuerySingleOrDefault<int>(sql);Assert.Fail("QuerySingleOrDefault, 2");}catch(InvalidOperationExceptionex){ex.Message.IsEqualTo("Sequence contains more than one element");}
varrows=connection.Query("select 1 A, 2 B union all select 3, 4").ToList();
((int)rows[0].A).IsEqualTo(1);
((int)rows[0].B).IsEqualTo(2);
((int)rows[1].A).IsEqualTo(3);
((int)rows[1].B).IsEqualTo(4);
Assert.Equal(1,(int)rows[0].A);
Assert.Equal(2,(int)rows[0].B);
Assert.Equal(3,(int)rows[1].A);
Assert.Equal(4,(int)rows[1].B);
}
[Fact]
publicvoidTestStringList()
{
Assert.Equal(
new[]{"a","b","c"},
connection.Query<string>("select * from (select 'a' as x union all select 'b' union all select 'c') as T where x in @strings",new{strings=new[]{"a","b","c"}})
.IsSequenceEqualTo(new[]{"a","b","c"});
);
Assert.Equal(
newstring[0],
connection.Query<string>("select * from (select 'a' as x union all select 'b' union all select 'c') as T where x in @strings",new{strings=newstring[0]})
.IsSequenceEqualTo(newstring[0]);
);
}
[Fact]
publicvoidTestExecuteCommand()
{
connection.Execute(@"
Assert.Equal(2,connection.Execute(@"
set nocount on
create table #t(i int)
set nocount off
insert #t
select @a a union all select @b
set nocount on
drop table #t",new{a=1,b=2}).IsEqualTo(2);
drop table #t",new{a=1,b=2}));
}
[Fact]
...
...
@@ -289,10 +300,10 @@ public void TestExecuteMultipleCommand()
varobj=connection.Query<WithCharValue>("select @ValueNullable as ValueNullable",newWithCharValue{ValueNullable=c}).Single();
obj.ValueNullable.IsEqualTo(test);
Assert.Equal(obj.ValueNullable,test);
}
[Fact]
...
...
@@ -675,61 +686,61 @@ private struct CanHazInt
[Fact]
publicvoidTestInt16Usage()
{
connection.Query<short>("select cast(42 as smallint)").Single().IsEqualTo((short)42);
connection.Query<short?>("select cast(42 as smallint)").Single().IsEqualTo((short?)42);
connection.Query<short?>("select cast(null as smallint)").Single().IsEqualTo((short?)null);
Assert.Equal(connection.Query<short>("select cast(42 as smallint)").Single(),(short)42);
Assert.Equal(connection.Query<short?>("select cast(42 as smallint)").Single(),(short?)42);
Assert.Equal(connection.Query<short?>("select cast(null as smallint)").Single(),(short?)null);
connection.Query<ShortEnum>("select cast(42 as smallint)").Single().IsEqualTo((ShortEnum)42);
connection.Query<ShortEnum?>("select cast(42 as smallint)").Single().IsEqualTo((ShortEnum?)42);
connection.Query<ShortEnum?>("select cast(null as smallint)").Single().IsEqualTo((ShortEnum?)null);
Assert.Equal(connection.Query<ShortEnum>("select cast(42 as smallint)").Single(),(ShortEnum)42);
Assert.Equal(connection.Query<ShortEnum?>("select cast(42 as smallint)").Single(),(ShortEnum?)42);
Assert.Equal(connection.Query<ShortEnum?>("select cast(null as smallint)").Single(),(ShortEnum?)null);
varrow=
connection.Query<WithInt16Values>(
"select cast(1 as smallint) as NonNullableInt16, cast(2 as smallint) as NullableInt16, cast(3 as smallint) as NonNullableInt16Enum, cast(4 as smallint) as NullableInt16Enum")
"select cast(5 as smallint) as NonNullableInt16, cast(null as smallint) as NullableInt16, cast(6 as smallint) as NonNullableInt16Enum, cast(null as smallint) as NullableInt16Enum")
connection.Query<int>("select cast(42 as int)").Single().IsEqualTo((int)42);
connection.Query<int?>("select cast(42 as int)").Single().IsEqualTo((int?)42);
connection.Query<int?>("select cast(null as int)").Single().IsEqualTo((int?)null);
Assert.Equal(connection.Query<int>("select cast(42 as int)").Single(),(int)42);
Assert.Equal(connection.Query<int?>("select cast(42 as int)").Single(),(int?)42);
Assert.Equal(connection.Query<int?>("select cast(null as int)").Single(),(int?)null);
connection.Query<IntEnum>("select cast(42 as int)").Single().IsEqualTo((IntEnum)42);
connection.Query<IntEnum?>("select cast(42 as int)").Single().IsEqualTo((IntEnum?)42);
connection.Query<IntEnum?>("select cast(null as int)").Single().IsEqualTo((IntEnum?)null);
Assert.Equal(connection.Query<IntEnum>("select cast(42 as int)").Single(),(IntEnum)42);
Assert.Equal(connection.Query<IntEnum?>("select cast(42 as int)").Single(),(IntEnum?)42);
Assert.Equal(connection.Query<IntEnum?>("select cast(null as int)").Single(),(IntEnum?)null);
varrow=
connection.Query<WithInt32Values>(
"select cast(1 as int) as NonNullableInt32, cast(2 as int) as NullableInt32, cast(3 as int) as NonNullableInt32Enum, cast(4 as int) as NullableInt32Enum")
"select cast(5 as int) as NonNullableInt32, cast(null as int) as NullableInt32, cast(6 as int) as NonNullableInt32Enum, cast(null as int) as NullableInt32Enum")
@@ -12,7 +12,7 @@ public class MultiMapTests : TestBase
publicvoidParentChildIdentityAssociations()
{
varlookup=newDictionary<int,Parent>();
varparents=connection.Query<Parent,Child,Parent>(@"select 1 as [Id], 1 as [Id] union all select 1,2 union all select 2,3 union all select 1,4 union all select 3,5",
varparents=connection.Query<Parent,Child,Parent>("select 1 as [Id], 1 as [Id] union all select 1,2 union all select 2,3 union all select 1,4 union all select 3,5",
(parent,child)=>
{
if(!lookup.TryGetValue(parent.Id,outParentfound))
...
...
@@ -22,10 +22,10 @@ public void ParentChildIdentityAssociations()
connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids",new{Ids=newint[]{1,2,3}.AsEnumerable()})
.IsSequenceEqualTo(new[]{1,2,3});
);
}
[Fact]
publicvoidPassInEmptyIntArray()
{
Assert.Equal(
newint[0],
connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids",new{Ids=newint[0]})
.IsSequenceEqualTo(newint[0]);
);
}
[Fact]
...
...
@@ -157,8 +158,8 @@ public void TestExecuteCommandWithHybridParameters()
vardata=cnn.Query<PostCE,AuthorCE,PostCE>(@"select * from Posts p left join Authors a on a.ID = p.AuthorID",(post,author)=>{post.Author=author;returnpost;}).ToList();
vardata=cnn.Query<PostCE,AuthorCE,PostCE>("select * from Posts p left join Authors a on a.ID = p.AuthorID",(post,author)=>{post.Author=author;returnpost;}).ToList();
ex.Message.IsEqualTo("The reader has been disposed; this can happen after all data has been consumed\r\nObject name: 'Dapper.SqlMapper+GridReader'.");
Assert.Equal("The reader has been disposed; this can happen after all data has been consumed\r\nObject name: 'Dapper.SqlMapper+GridReader'.",ex.Message);
}
one.Length.IsEqualTo(1);
one[0].IsEqualTo(1);
two.Length.IsEqualTo(0);
three.Length.IsEqualTo(0);
four.Length.IsEqualTo(1);
four[0].IsEqualTo(4);
Assert.Single(one);
Assert.Equal(1,one[0]);
Assert.Empty(two);
Assert.Empty(three);
Assert.Single(four);
Assert.Equal(4,four[0]);
}
}
...
...
@@ -261,17 +254,17 @@ public void TypeBasedViaTypeMulti()
ex.Message.IsEqualTo("ValueTuple should not be used for parameters - the language-level names are not available to use as parameter names, and it adds unnecessary boxing");
Assert.Equal("ValueTuple should not be used for parameters - the language-level names are not available to use as parameter names, and it adds unnecessary boxing",ex.Message);