From 61fa083ca3b711067b6b2a0448b156836d696794 Mon Sep 17 00:00:00 2001 From: Wei Date: Wed, 25 Dec 2019 03:14:13 +0800 Subject: [PATCH] Add unit test for SqlBuilder with Dapper query (#1369) --- Dapper.Tests/Dapper.Tests.csproj | 1 + Dapper.Tests/SqlBuilderTests.cs | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Dapper.Tests/SqlBuilderTests.cs diff --git a/Dapper.Tests/Dapper.Tests.csproj b/Dapper.Tests/Dapper.Tests.csproj index f25f3288e..30df68cc5 100644 --- a/Dapper.Tests/Dapper.Tests.csproj +++ b/Dapper.Tests/Dapper.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/Dapper.Tests/SqlBuilderTests.cs b/Dapper.Tests/SqlBuilderTests.cs new file mode 100644 index 000000000..ab448242e --- /dev/null +++ b/Dapper.Tests/SqlBuilderTests.cs @@ -0,0 +1,46 @@ +using System.Linq; +using Xunit; + +namespace Dapper.Tests +{ + [Collection("SqlBuilderTests")] + public sealed class SystemSqlClientSqlBuilderTests : SqlBuilderTests { } +#if MSSQLCLIENT + [Collection("SqlBuilderTests")] + public sealed class MicrosoftSqlClientSqlBuilderTests : SqlBuilderTests { } +#endif + public abstract class SqlBuilderTests : TestBase 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"); + } + } + } +}