Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for SqlBuilder with Dapper query #1369

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}
}