forked from DapperLib/Dapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumTests.cs
129 lines (112 loc) · 4.04 KB
/
EnumTests.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Data;
using System.Linq;
using Xunit;
namespace Dapper.Tests
{
public class EnumTests : TestBase
{
[Fact]
public void TestEnumWeirdness()
{
connection.Query<TestEnumClass>("select null as [EnumEnum]").First().EnumEnum.IsEqualTo(null);
connection.Query<TestEnumClass>("select cast(1 as tinyint) as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
}
[Fact]
public void TestEnumStrings()
{
connection.Query<TestEnumClassNoNull>("select 'BLA' as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
connection.Query<TestEnumClassNoNull>("select 'bla' as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
connection.Query<TestEnumClass>("select 'BLA' as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
connection.Query<TestEnumClass>("select 'bla' as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
}
[Fact]
public void TestEnumParamsWithNullable()
{
EnumParam a = EnumParam.A;
EnumParam? b = EnumParam.B, c = null;
var obj = connection.Query<EnumParamObject>("select @a as A, @b as B, @c as C",
new { a, b, c }).Single();
obj.A.IsEqualTo(EnumParam.A);
obj.B.IsEqualTo(EnumParam.B);
obj.C.IsEqualTo(null);
}
[Fact]
public void TestEnumParamsWithoutNullable()
{
EnumParam a = EnumParam.A;
EnumParam b = EnumParam.B, c = 0;
var obj = connection.Query<EnumParamObjectNonNullable>("select @a as A, @b as B, @c as C",
new { a, b, c }).Single();
obj.A.IsEqualTo(EnumParam.A);
obj.B.IsEqualTo(EnumParam.B);
obj.C.IsEqualTo((EnumParam)0);
}
private enum EnumParam : short
{
None = 0,
A = 1,
B = 2
}
private class EnumParamObject
{
public EnumParam A { get; set; }
public EnumParam? B { get; set; }
public EnumParam? C { get; set; }
}
private class EnumParamObjectNonNullable
{
public EnumParam A { get; set; }
public EnumParam? B { get; set; }
public EnumParam? C { get; set; }
}
private enum TestEnum : byte
{
Bla = 1
}
private class TestEnumClass
{
public TestEnum? EnumEnum { get; set; }
}
private class TestEnumClassNoNull
{
public TestEnum EnumEnum { get; set; }
}
[Fact]
public void AdoNetEnumValue()
{
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "select @foo";
var p = cmd.CreateParameter();
p.ParameterName = "@foo";
p.DbType = DbType.Int32; // it turns out that this is the key piece; setting the DbType
p.Value = AnEnum.B;
cmd.Parameters.Add(p);
object value = cmd.ExecuteScalar();
AnEnum val = (AnEnum)value;
val.IsEqualTo(AnEnum.B);
}
}
[Fact]
public void DapperEnumValue_SqlServer() => Common.DapperEnumValue(connection);
private enum SO27024806Enum
{
Foo = 0,
Bar = 1
}
private class SO27024806Class
{
public SO27024806Class(SO27024806Enum myField)
{
MyField = myField;
}
public SO27024806Enum MyField { get; set; }
}
[Fact]
public void SO27024806_TestVarcharEnumMemberWithExplicitConstructor()
{
var foo = connection.Query<SO27024806Class>("SELECT 'Foo' AS myField").Single();
foo.MyField.IsEqualTo(SO27024806Enum.Foo);
}
}
}