-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
MySQLTests.cs
241 lines (216 loc) · 9.27 KB
/
MySQLTests.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Dapper.Tests
{
public sealed class MySqlProvider : DatabaseProvider
{
public override DbProviderFactory Factory => MySql.Data.MySqlClient.MySqlClientFactory.Instance;
public override string GetConnectionString() =>
GetConnectionString("MySqlConnectionString", "Server=localhost;Database=tests;Uid=test;Pwd=pass;");
public DbConnection GetMySqlConnection(bool open = true,
bool convertZeroDatetime = false, bool allowZeroDatetime = false)
{
string cs = GetConnectionString();
var csb = Factory.CreateConnectionStringBuilder();
csb.ConnectionString = cs;
((dynamic)csb).AllowZeroDateTime = allowZeroDatetime;
((dynamic)csb).ConvertZeroDateTime = convertZeroDatetime;
var conn = Factory.CreateConnection();
conn.ConnectionString = csb.ConnectionString;
if (open) conn.Open();
return conn;
}
}
public class MySQLTests : TestBase<MySqlProvider>
{
[FactMySql]
public void DapperEnumValue_Mysql()
{
using (var conn = Provider.GetMySqlConnection())
{
Common.DapperEnumValue(conn);
}
}
[FactMySql(Skip = "See https://github.com/StackExchange/Dapper/issues/552, not resolved on the MySQL end.")]
public void Issue552_SignedUnsignedBooleans()
{
using (var conn = Provider.GetMySqlConnection(true, false, false))
{
conn.Execute(@"
CREATE TEMPORARY TABLE IF NOT EXISTS `bar` (
`id` INT NOT NULL,
`bool_val` BOOL NULL,
PRIMARY KEY (`id`));
truncate table bar;
insert bar (id, bool_val) values (1, null);
insert bar (id, bool_val) values (2, 0);
insert bar (id, bool_val) values (3, 1);
insert bar (id, bool_val) values (4, null);
insert bar (id, bool_val) values (5, 1);
insert bar (id, bool_val) values (6, 0);
insert bar (id, bool_val) values (7, null);
insert bar (id, bool_val) values (8, 1);");
var rows = conn.Query<MySqlHasBool>("select * from bar;").ToDictionary(x => x.Id);
Assert.Null(rows[1].Bool_Val);
Assert.False(rows[2].Bool_Val);
Assert.True(rows[3].Bool_Val);
Assert.Null(rows[4].Bool_Val);
Assert.True(rows[5].Bool_Val);
Assert.False(rows[6].Bool_Val);
Assert.Null(rows[7].Bool_Val);
Assert.True(rows[8].Bool_Val);
}
}
private class MySqlHasBool
{
public int Id { get; set; }
public bool? Bool_Val { get; set; }
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_Default()
{
using (var conn = Provider.GetMySqlConnection(true, false, false))
{
Common.TestDateTime(conn);
}
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_ConvertZeroDatetime()
{
using (var conn = Provider.GetMySqlConnection(true, true, false))
{
Common.TestDateTime(conn);
}
}
[FactMySql(Skip = "See https://github.com/StackExchange/Dapper/issues/295, AllowZeroDateTime=True is not supported")]
public void Issue295_NullableDateTime_MySql_AllowZeroDatetime()
{
using (var conn = Provider.GetMySqlConnection(true, false, true))
{
Common.TestDateTime(conn);
}
}
[FactMySql(Skip = "See https://github.com/StackExchange/Dapper/issues/295, AllowZeroDateTime=True is not supported")]
public void Issue295_NullableDateTime_MySql_ConvertAllowZeroDatetime()
{
using (var conn = Provider.GetMySqlConnection(true, true, true))
{
Common.TestDateTime(conn);
}
}
[FactMySql]
public void Issue426_SO34439033_DateTimeGainsTicks()
{
using (var conn = Provider.GetMySqlConnection(true, true, true))
{
try { conn.Execute("drop table Issue426_Test"); } catch { /* don't care */ }
try { conn.Execute("create table Issue426_Test (Id int not null, Time time not null)"); } catch { /* don't care */ }
const long ticks = 553440000000;
const int Id = 426;
var localObj = new Issue426_Test
{
Id = Id,
Time = TimeSpan.FromTicks(ticks) // from code example
};
conn.Execute("replace into Issue426_Test values (@Id,@Time)", localObj);
var dbObj = conn.Query<Issue426_Test>("select * from Issue426_Test where Id = @id", new { id = Id }).Single();
Assert.Equal(Id, dbObj.Id);
Assert.Equal(ticks, dbObj.Time.Value.Ticks);
}
}
[FactMySql]
public void SO36303462_Tinyint_Bools()
{
using (var conn = Provider.GetMySqlConnection(true, true, true))
{
try { conn.Execute("drop table SO36303462_Test"); } catch { /* don't care */ }
conn.Execute("create table SO36303462_Test (Id int not null, IsBold tinyint not null);");
conn.Execute("insert SO36303462_Test (Id, IsBold) values (1,1);");
conn.Execute("insert SO36303462_Test (Id, IsBold) values (2,0);");
conn.Execute("insert SO36303462_Test (Id, IsBold) values (3,1);");
var rows = conn.Query<SO36303462>("select * from SO36303462_Test").ToDictionary(x => x.Id);
Assert.Equal(3, rows.Count);
Assert.True(rows[1].IsBold);
Assert.False(rows[2].IsBold);
Assert.True(rows[3].IsBold);
}
}
[FactMySql]
public void Issue1277_ReaderSync()
{
using (var conn = Provider.GetMySqlConnection())
{
try { conn.Execute("drop table Issue1277_Test"); } catch { /* don't care */ }
conn.Execute("create table Issue1277_Test (Id int not null, IsBold tinyint not null);");
conn.Execute("insert Issue1277_Test (Id, IsBold) values (1,1);");
conn.Execute("insert Issue1277_Test (Id, IsBold) values (2,0);");
conn.Execute("insert Issue1277_Test (Id, IsBold) values (3,1);");
using (var reader = conn.ExecuteReader(
"select * from Issue1277_Test where Id < @id",
new { id = 42 }))
{
var table = new DataTable();
table.Load(reader);
Assert.Equal(2, table.Columns.Count);
Assert.Equal(3, table.Rows.Count);
}
}
}
[FactMySql]
public async Task Issue1277_ReaderAsync()
{
using (var conn = Provider.GetMySqlConnection())
{
try { await conn.ExecuteAsync("drop table Issue1277_Test"); } catch { /* don't care */ }
await conn.ExecuteAsync("create table Issue1277_Test (Id int not null, IsBold tinyint not null);");
await conn.ExecuteAsync("insert Issue1277_Test (Id, IsBold) values (1,1);");
await conn.ExecuteAsync("insert Issue1277_Test (Id, IsBold) values (2,0);");
await conn.ExecuteAsync("insert Issue1277_Test (Id, IsBold) values (3,1);");
using (var reader = await conn.ExecuteReaderAsync(
"select * from Issue1277_Test where Id < @id",
new { id = 42 }))
{
var table = new DataTable();
table.Load(reader);
Assert.Equal(2, table.Columns.Count);
Assert.Equal(3, table.Rows.Count);
}
}
}
private class SO36303462
{
public int Id { get; set; }
public bool IsBold { get; set; }
}
public class Issue426_Test
{
public long Id { get; set; }
public TimeSpan? Time { get; set; }
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class FactMySqlAttribute : FactAttribute
{
public override string Skip
{
get { return unavailable ?? base.Skip; }
set { base.Skip = value; }
}
private static readonly string unavailable;
static FactMySqlAttribute()
{
try
{
using (DatabaseProvider<MySqlProvider>.Instance.GetMySqlConnection(true)) { /* just trying to see if it works */ }
}
catch (Exception ex)
{
unavailable = $"MySql is unavailable: {ex.Message}";
}
}
}
}
}