Skip to content

Commit

Permalink
Test rig for Issue #295
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Nov 29, 2015
1 parent 23c4c53 commit e918254
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
89 changes: 89 additions & 0 deletions Dapper.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Data.SqlTypes;
using System.Diagnostics;
using Xunit;
using System.Data.Common;
#if EXTERNALS
using FirebirdSql.Data.FirebirdClient;
using System.Data.Entity.Spatial;
Expand Down Expand Up @@ -2844,7 +2845,95 @@ public void SO30435185_InvalidTypeOwner()
ex.Message.IsEqualTo("An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context");
}
}
[Fact]
public void Issue295_NullableDateTime_SqlServer()
{
TestDateTime(connection);
}
#if MYSQL
private static MySql.Data.MySqlClient.MySqlConnection GetMySqlConnection(bool open = true,
bool convertZeroDatetime = false, bool allowZeroDatetime = false)
{
const string cs = "Server=localhost;Database=tests;Uid=test;Pwd=pass;";
var csb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(cs);
csb.AllowZeroDateTime = allowZeroDatetime;
csb.ConvertZeroDateTime = convertZeroDatetime;
var conn = new MySql.Data.MySqlClient.MySqlConnection(csb.ConnectionString);
if (open) conn.Open();
return conn;
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_Default()
{
using (var conn = GetMySqlConnection(true, false, false)) { TestDateTime(connection); }
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_ConvertZeroDatetime()
{
using (var conn = GetMySqlConnection(true, true, false)) { TestDateTime(connection); }
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_AllowZeroDatetime()
{
using (var conn = GetMySqlConnection(true, false, true)) { TestDateTime(connection); }
}
[FactMySql]
public void Issue295_NullableDateTime_MySql_ConvertAllowZeroDatetime()
{
using (var conn = GetMySqlConnection(true, true, true)) { TestDateTime(connection); }
}

public class FactMySqlAttribute : FactAttribute
{
public override string Skip
{
get { return unavailable ?? base.Skip; }
set { base.Skip = value; }
}
private static string unavailable;
static FactMySqlAttribute()
{
try
{
using (GetMySqlConnection(true)) { }
}
catch(Exception ex)
{
unavailable = $"MySql is unavailable: {ex.Message}";
}
}
}
#endif
private void TestDateTime(DbConnection connection)
{
DateTime? now = DateTime.UtcNow;
try { connection.Execute("DROP TABLE Persons"); } catch { }
connection.Execute(@"CREATE TABLE Persons (id int not null, dob datetime null)");
connection.Execute(@"INSERT Persons (id, dob) values (@id, @dob)",
new { id = 7, dob = (DateTime?)null });
connection.Execute(@"INSERT Persons (id, dob) values (@id, @dob)",
new { id = 42, dob = now });

var row = connection.QueryFirstOrDefault<Issue295Person>(
"SELECT id, dob, dob as dob2 FROM Persons WHERE id=@id", new { id = 7});
row.IsNotNull();
row.Id.IsEqualTo(7);
row.DoB.IsNull();
row.DoB2.IsNull();

row = connection.QueryFirstOrDefault<Issue295Person>(
"SELECT id, dob FROM Persons WHERE id=@id", new { id = 42 });
row.IsNotNull();
row.Id.IsEqualTo(42);
row.DoB.Equals(now);
row.DoB2.Equals(now);
}
class Issue295Person
{
public int Id { get; set; }
public DateTime? DoB { get; set; }
public DateTime? DoB2 { get; set; }
}
#if EXTERNALS
[Fact(Skip="Bug in Firebird; a PR to fix it has been submitted")]
public void Issue178_Firebird()
Expand Down
16 changes: 11 additions & 5 deletions Dapper.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
},
"frameworks": {
"net40": {
"compilationOptions": {
"define": [ "MYSQL" ]
},
"frameworkAssemblies": {
"System.Configuration": "4.0.0.0",
"System.Data": "4.0.0.0",
Expand All @@ -36,12 +39,13 @@
"ServiceStack.OrmLite": "4.0.48",
"ServiceStack.OrmLite.SqlServer": "4.0.48",
"Soma": "1.8.0.7",
"xunit": "1.9.2"
"xunit": "1.9.2",
"MySql.Data": "6.9.8"
}
},
"net45": {
"compilationOptions": {
"define": [ "ASYNC" ]
"define": [ "ASYNC", "MYSQL" ]
},
"frameworkAssemblies": {
"System.Configuration": "4.0.0.0",
Expand All @@ -55,7 +59,8 @@
"ServiceStack.OrmLite": "4.0.48",
"ServiceStack.OrmLite.SqlServer": "4.0.48",
"Soma": "1.8.0.7",
"xunit": "2.1.0"
"xunit": "2.1.0",
"MySql.Data": "6.9.8"
}
},
"dotnet5.4": {
Expand All @@ -75,7 +80,7 @@
},
"dnx451": {
"compilationOptions": {
"define": [ "ASYNC", "EXTERNALS", "DNX" ]
"define": [ "ASYNC", "EXTERNALS", "DNX", "MYSQL" ]
},
"frameworkAssemblies": {
"System.Configuration": "4.0.0.0",
Expand All @@ -102,7 +107,8 @@
"FirebirdSql.Data.FirebirdClient": "4.8.1.1",
"Dapper.EntityFramework": {
"target": "project"
}
},
"MySql.Data": "6.9.8"
}

},
Expand Down

0 comments on commit e918254

Please sign in to comment.