mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-21 16:26:16 +08:00
37329c8dd6
- huge thanks to bkaid
67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using System.Linq;
|
|
using Dapper;
|
|
using SqlMapper;
|
|
|
|
namespace DapperTests_NET45
|
|
{
|
|
public class Tests
|
|
{
|
|
public void TestBasicStringUsageAsync()
|
|
{
|
|
using (var connection = Program.GetOpenConnection())
|
|
{
|
|
var query = connection.QueryAsync<string>("select 'abc' as [Value] union all select @txt", new { txt = "def" });
|
|
var arr = query.Result.ToArray();
|
|
arr.IsSequenceEqualTo(new[] { "abc", "def" });
|
|
}
|
|
}
|
|
|
|
public void TestClassWithStringUsageAsync()
|
|
{
|
|
using (var connection = Program.GetOpenConnection())
|
|
{
|
|
var query = connection.QueryAsync<BasicType>("select 'abc' as [Value] union all select @txt", new { txt = "def" });
|
|
var arr = query.Result.ToArray();
|
|
arr.Select(x => x.Value).IsSequenceEqualTo(new[] { "abc", "def" });
|
|
}
|
|
}
|
|
|
|
public void TestMultiMapWithSplitAsync()
|
|
{
|
|
var sql = @"select 1 as id, 'abc' as name, 2 as id, 'def' as name";
|
|
using (var connection = Program.GetOpenConnection())
|
|
{
|
|
var productQuery = connection.QueryAsync<Product, Category, Product>(sql, (prod, cat) =>
|
|
{
|
|
prod.Category = cat;
|
|
return prod;
|
|
});
|
|
|
|
var product = productQuery.Result.First();
|
|
// assertions
|
|
product.Id.IsEqualTo(1);
|
|
product.Name.IsEqualTo("abc");
|
|
product.Category.Id.IsEqualTo(2);
|
|
product.Category.Name.IsEqualTo("def");
|
|
}
|
|
}
|
|
|
|
class Product
|
|
{
|
|
public int Id { get; set; }
|
|
public string Name { get; set; }
|
|
public Category Category { get; set; }
|
|
}
|
|
class Category
|
|
{
|
|
public int Id { get; set; }
|
|
public string Name { get; set; }
|
|
public string Description { get; set; }
|
|
}
|
|
|
|
class BasicType
|
|
{
|
|
public string Value { get; set; }
|
|
}
|
|
}
|
|
} |