Skip to content

Commit

Permalink
Support nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
UnoSD committed Mar 9, 2018
1 parent 62fdd1c commit 52dbe86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 9 additions & 4 deletions Moq.Dapper.Test/DapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,24 @@ public void QueryGenericComplexType()
StringProperty = "String1",
IntegerProperty = 7,
GuidProperty = Guid.Parse("CF01F32D-A55B-4C4A-9B33-AAC1C20A85BB"),
DateTimeProperty = new DateTime(2000, 1, 1)
DateTimeProperty = new DateTime(2000, 1, 1),
NullableIntegerProperty = 9
},
new ComplexType
{
StringProperty = "String2",
IntegerProperty = 77,
GuidProperty = Guid.Parse("FBECE122-6E2E-4791-B781-C30843DFE343"),
DateTimeProperty = new DateTime(2000, 1, 2)
DateTimeProperty = new DateTime(2000, 1, 2),
NullableIntegerProperty = 99
},
new ComplexType
{
StringProperty = "String3",
IntegerProperty = 777,
GuidProperty = Guid.Parse("712B6DA1-71D8-4D60-8FEF-3F4800A6B04F"),
DateTimeProperty = new DateTime(2000, 1, 3)
DateTimeProperty = new DateTime(2000, 1, 3),
NullableIntegerProperty = null
}
};

Expand All @@ -100,7 +103,8 @@ public void QueryGenericComplexType()
var match = actual.Where(co => co.StringProperty == complexObject.StringProperty &&
co.IntegerProperty == complexObject.IntegerProperty &&
co.GuidProperty == complexObject.GuidProperty &&
co.DateTimeProperty == complexObject.DateTimeProperty);
co.DateTimeProperty == complexObject.DateTimeProperty &&
co.NullableIntegerProperty == complexObject.NullableIntegerProperty);

Assert.That(match.Count, Is.EqualTo(1));
}
Expand Down Expand Up @@ -153,6 +157,7 @@ public class ComplexType
public string StringProperty { get; set; }
public Guid GuidProperty { get; set; }
public DateTime DateTimeProperty { get; set; }
public int? NullableIntegerProperty { get; set; }
}
}
}
15 changes: 13 additions & 2 deletions Moq.Dapper/DbConnectionInterfaceMockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public static class DbConnectionInterfaceMockExtensions
}
else
{
bool IsNullable(Type t) =>
t.IsGenericType &&
t.GetGenericTypeDefinition() == typeof(Nullable<>);
Type GetDataColumnType(Type source) =>
IsNullable(source) ?
Nullable.GetUnderlyingType(source) :
source;
var properties =
type.GetProperties()
.Where(info => info.CanRead &&
Expand All @@ -65,10 +74,12 @@ public static class DbConnectionInterfaceMockExtensions
info.PropertyType == typeof(decimal) ||
info.PropertyType == typeof(Guid) ||
info.PropertyType == typeof(string) ||
info.PropertyType == typeof(TimeSpan)))
info.PropertyType == typeof(TimeSpan)) ||
IsNullable(info.PropertyType) &&
Nullable.GetUnderlyingType(info.PropertyType).IsPrimitive)
.ToList();
var columns = properties.Select(property => new DataColumn(property.Name, property.PropertyType))
var columns = properties.Select(property => new DataColumn(property.Name, GetDataColumnType(property.PropertyType)))
.ToArray();
dataTable.Columns.AddRange(columns);
Expand Down

0 comments on commit 52dbe86

Please sign in to comment.