-
Notifications
You must be signed in to change notification settings - Fork 0
/
NullFirstRecordTests.cs
64 lines (53 loc) · 1.84 KB
/
NullFirstRecordTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Data;
using System.Linq;
using Dapper;
using Xunit;
namespace DapperTests
{
/// <summary>
/// Demonstrates a bug where Dapper assumes the wrong data type for a column whose first record is NULL
/// </summary>
public class NullFirstRecordTests
{
private static IDbConnection GetDb()
{
var cn = Database.GetConnection();
cn.Execute("CREATE TABLE Foo ( Value INT )");
cn.Execute("INSERT INTO Foo VALUES ( NULL )");
cn.Execute("INSERT INTO Foo VALUES ( 1 )");
return cn;
}
public class Foo
{
public int Value { get; set; }
}
public class FooNullable
{
public int? Value { get; set; }
}
[Fact]
// assumes column is int64 when source column is NULL and destination field is non-nullable int
public void NullFirstValueNoCast()
{
var cn = GetDb();
Assert.Throws<DataException>(() => cn.Query<Foo>("SELECT Value FROM Foo"));
}
[Fact]
// assumes column is int64 when first record is NULL and destination field is nullable int
public void NullFirstValueNullableDestination()
{
var cn = GetDb();
Assert.Throws<DataException>(() => cn.Query<FooNullable>("SELECT Value FROM Foo").ToList());
}
[Fact]
// detects correct data type when first record is NULL and CAST(... AS INTEGER) is used
public void NullFirstValueWithCast()
{
var cn = GetDb();
// does not throw when NULL is casted to INTEGER first
var result = cn.Query<Foo>("SELECT CAST(Value AS INTEGER) FROM Foo").ToList();
Assert.Equal(0, result.First().Value);
Assert.Equal(2, result.Count);
}
}
}