Skip to content

Commit

Permalink
Add unit test for SqlBuilder with Dapper query (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 authored and NickCraver committed Dec 24, 2019
1 parent 1922ef4 commit 61fa083
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dapper.Tests/Dapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Dapper.SqlBuilder\Dapper.SqlBuilder.csproj" />
<ProjectReference Include="..\Dapper\Dapper.csproj" />
<ProjectReference Include="..\Dapper.Contrib\Dapper.Contrib.csproj" />
<ProjectReference Include="..\Dapper.ProviderTools\Dapper.ProviderTools.csproj" />
Expand Down
46 changes: 46 additions & 0 deletions Dapper.Tests/SqlBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using Xunit;

namespace Dapper.Tests
{
[Collection("SqlBuilderTests")]
public sealed class SystemSqlClientSqlBuilderTests : SqlBuilderTests<SystemSqlClientProvider> { }
#if MSSQLCLIENT
[Collection("SqlBuilderTests")]
public sealed class MicrosoftSqlClientSqlBuilderTests : SqlBuilderTests<MicrosoftSqlClientProvider> { }
#endif
public abstract class SqlBuilderTests<TProvider> : TestBase<TProvider> where TProvider : DatabaseProvider
{
[Fact]
public void TestSqlBuilderWithDapperQuery()
{
var sb = new SqlBuilder();
var template = sb.AddTemplate("SELECT /**select**/ FROM #Users /**where**/");
sb.Where("Age <= @Age", new { Age = 18 })
.Where("Country = @Country", new { Country = "USA" })
.Select("Name,Age,Country");

const string createSql = @"
create table #Users (Name varchar(20),Age int,Country nvarchar(5));
insert #Users values('Sam',16,'USA'),('Tom',25,'UK'),('Henry',14,'UK')";
try
{
connection.Execute(createSql);

var result = connection.Query(template.RawSql,template.Parameters).ToArray();

Assert.Equal("SELECT Name,Age,Country\n FROM #Users WHERE Age <= @Age AND Country = @Country\n", template.RawSql);

Assert.Single(result);

Assert.Equal(16, (int)result[0].Age);
Assert.Equal("Sam", (string)result[0].Name);
Assert.Equal("USA", (string)result[0].Country);
}
finally
{
connection.Execute("drop table #Users");
}
}
}
}

0 comments on commit 61fa083

Please sign in to comment.