Skip to content

Commit e918254

Browse files
committed
Test rig for Issue #295
1 parent 23c4c53 commit e918254

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

Dapper.Tests/Tests.cs

+89
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Data.SqlTypes;
2626
using System.Diagnostics;
2727
using Xunit;
28+
using System.Data.Common;
2829
#if EXTERNALS
2930
using FirebirdSql.Data.FirebirdClient;
3031
using System.Data.Entity.Spatial;
@@ -2844,7 +2845,95 @@ public void SO30435185_InvalidTypeOwner()
28442845
ex.Message.IsEqualTo("An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context");
28452846
}
28462847
}
2848+
[Fact]
2849+
public void Issue295_NullableDateTime_SqlServer()
2850+
{
2851+
TestDateTime(connection);
2852+
}
2853+
#if MYSQL
2854+
private static MySql.Data.MySqlClient.MySqlConnection GetMySqlConnection(bool open = true,
2855+
bool convertZeroDatetime = false, bool allowZeroDatetime = false)
2856+
{
2857+
const string cs = "Server=localhost;Database=tests;Uid=test;Pwd=pass;";
2858+
var csb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(cs);
2859+
csb.AllowZeroDateTime = allowZeroDatetime;
2860+
csb.ConvertZeroDateTime = convertZeroDatetime;
2861+
var conn = new MySql.Data.MySqlClient.MySqlConnection(csb.ConnectionString);
2862+
if (open) conn.Open();
2863+
return conn;
2864+
}
2865+
[FactMySql]
2866+
public void Issue295_NullableDateTime_MySql_Default()
2867+
{
2868+
using (var conn = GetMySqlConnection(true, false, false)) { TestDateTime(connection); }
2869+
}
2870+
[FactMySql]
2871+
public void Issue295_NullableDateTime_MySql_ConvertZeroDatetime()
2872+
{
2873+
using (var conn = GetMySqlConnection(true, true, false)) { TestDateTime(connection); }
2874+
}
2875+
[FactMySql]
2876+
public void Issue295_NullableDateTime_MySql_AllowZeroDatetime()
2877+
{
2878+
using (var conn = GetMySqlConnection(true, false, true)) { TestDateTime(connection); }
2879+
}
2880+
[FactMySql]
2881+
public void Issue295_NullableDateTime_MySql_ConvertAllowZeroDatetime()
2882+
{
2883+
using (var conn = GetMySqlConnection(true, true, true)) { TestDateTime(connection); }
2884+
}
28472885

2886+
public class FactMySqlAttribute : FactAttribute
2887+
{
2888+
public override string Skip
2889+
{
2890+
get { return unavailable ?? base.Skip; }
2891+
set { base.Skip = value; }
2892+
}
2893+
private static string unavailable;
2894+
static FactMySqlAttribute()
2895+
{
2896+
try
2897+
{
2898+
using (GetMySqlConnection(true)) { }
2899+
}
2900+
catch(Exception ex)
2901+
{
2902+
unavailable = $"MySql is unavailable: {ex.Message}";
2903+
}
2904+
}
2905+
}
2906+
#endif
2907+
private void TestDateTime(DbConnection connection)
2908+
{
2909+
DateTime? now = DateTime.UtcNow;
2910+
try { connection.Execute("DROP TABLE Persons"); } catch { }
2911+
connection.Execute(@"CREATE TABLE Persons (id int not null, dob datetime null)");
2912+
connection.Execute(@"INSERT Persons (id, dob) values (@id, @dob)",
2913+
new { id = 7, dob = (DateTime?)null });
2914+
connection.Execute(@"INSERT Persons (id, dob) values (@id, @dob)",
2915+
new { id = 42, dob = now });
2916+
2917+
var row = connection.QueryFirstOrDefault<Issue295Person>(
2918+
"SELECT id, dob, dob as dob2 FROM Persons WHERE id=@id", new { id = 7});
2919+
row.IsNotNull();
2920+
row.Id.IsEqualTo(7);
2921+
row.DoB.IsNull();
2922+
row.DoB2.IsNull();
2923+
2924+
row = connection.QueryFirstOrDefault<Issue295Person>(
2925+
"SELECT id, dob FROM Persons WHERE id=@id", new { id = 42 });
2926+
row.IsNotNull();
2927+
row.Id.IsEqualTo(42);
2928+
row.DoB.Equals(now);
2929+
row.DoB2.Equals(now);
2930+
}
2931+
class Issue295Person
2932+
{
2933+
public int Id { get; set; }
2934+
public DateTime? DoB { get; set; }
2935+
public DateTime? DoB2 { get; set; }
2936+
}
28482937
#if EXTERNALS
28492938
[Fact(Skip="Bug in Firebird; a PR to fix it has been submitted")]
28502939
public void Issue178_Firebird()

Dapper.Tests/project.json

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
},
2626
"frameworks": {
2727
"net40": {
28+
"compilationOptions": {
29+
"define": [ "MYSQL" ]
30+
},
2831
"frameworkAssemblies": {
2932
"System.Configuration": "4.0.0.0",
3033
"System.Data": "4.0.0.0",
@@ -36,12 +39,13 @@
3639
"ServiceStack.OrmLite": "4.0.48",
3740
"ServiceStack.OrmLite.SqlServer": "4.0.48",
3841
"Soma": "1.8.0.7",
39-
"xunit": "1.9.2"
42+
"xunit": "1.9.2",
43+
"MySql.Data": "6.9.8"
4044
}
4145
},
4246
"net45": {
4347
"compilationOptions": {
44-
"define": [ "ASYNC" ]
48+
"define": [ "ASYNC", "MYSQL" ]
4549
},
4650
"frameworkAssemblies": {
4751
"System.Configuration": "4.0.0.0",
@@ -55,7 +59,8 @@
5559
"ServiceStack.OrmLite": "4.0.48",
5660
"ServiceStack.OrmLite.SqlServer": "4.0.48",
5761
"Soma": "1.8.0.7",
58-
"xunit": "2.1.0"
62+
"xunit": "2.1.0",
63+
"MySql.Data": "6.9.8"
5964
}
6065
},
6166
"dotnet5.4": {
@@ -75,7 +80,7 @@
7580
},
7681
"dnx451": {
7782
"compilationOptions": {
78-
"define": [ "ASYNC", "EXTERNALS", "DNX" ]
83+
"define": [ "ASYNC", "EXTERNALS", "DNX", "MYSQL" ]
7984
},
8085
"frameworkAssemblies": {
8186
"System.Configuration": "4.0.0.0",
@@ -102,7 +107,8 @@
102107
"FirebirdSql.Data.FirebirdClient": "4.8.1.1",
103108
"Dapper.EntityFramework": {
104109
"target": "project"
105-
}
110+
},
111+
"MySql.Data": "6.9.8"
106112
}
107113

108114
},

0 commit comments

Comments
 (0)