Skip to content

Commit

Permalink
Support non primitive nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
UnoSD committed Mar 10, 2018
1 parent 52dbe86 commit 5137d07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
7 changes: 6 additions & 1 deletion Moq.Dapper.Test/DapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void QueryGenericComplexType()
IntegerProperty = 7,
GuidProperty = Guid.Parse("CF01F32D-A55B-4C4A-9B33-AAC1C20A85BB"),
DateTimeProperty = new DateTime(2000, 1, 1),
NullableDateTimeProperty = new DateTime(2000, 1, 1),
NullableIntegerProperty = 9
},
new ComplexType
Expand All @@ -79,6 +80,7 @@ public void QueryGenericComplexType()
IntegerProperty = 77,
GuidProperty = Guid.Parse("FBECE122-6E2E-4791-B781-C30843DFE343"),
DateTimeProperty = new DateTime(2000, 1, 2),
NullableDateTimeProperty = new DateTime(2000, 1, 2),
NullableIntegerProperty = 99
},
new ComplexType
Expand All @@ -87,6 +89,7 @@ public void QueryGenericComplexType()
IntegerProperty = 777,
GuidProperty = Guid.Parse("712B6DA1-71D8-4D60-8FEF-3F4800A6B04F"),
DateTimeProperty = new DateTime(2000, 1, 3),
NullableDateTimeProperty = null,
NullableIntegerProperty = null
}
};
Expand All @@ -104,7 +107,8 @@ public void QueryGenericComplexType()
co.IntegerProperty == complexObject.IntegerProperty &&
co.GuidProperty == complexObject.GuidProperty &&
co.DateTimeProperty == complexObject.DateTimeProperty &&
co.NullableIntegerProperty == complexObject.NullableIntegerProperty);
co.NullableIntegerProperty == complexObject.NullableIntegerProperty &&
co.NullableDateTimeProperty == complexObject.NullableDateTimeProperty);

Assert.That(match.Count, Is.EqualTo(1));
}
Expand Down Expand Up @@ -157,6 +161,7 @@ public class ComplexType
public string StringProperty { get; set; }
public Guid GuidProperty { get; set; }
public DateTime DateTimeProperty { get; set; }
public DateTime? NullableDateTimeProperty { get; set; }
public int? NullableIntegerProperty { get; set; }
}
}
Expand Down
21 changes: 12 additions & 9 deletions Moq.Dapper/DbConnectionInterfaceMockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ public static class DbConnectionInterfaceMockExtensions
Nullable.GetUnderlyingType(source) :
source;
bool IsMatchingType(Type t) =>
t.IsPrimitive ||
t == typeof(DateTime) ||
t == typeof(DateTimeOffset) ||
t == typeof(decimal) ||
t == typeof(Guid) ||
t == typeof(string) ||
t == typeof(TimeSpan);
var properties =
type.GetProperties()
.Where(info => info.CanRead &&
(info.PropertyType.IsPrimitive ||
info.PropertyType == typeof(DateTime) ||
info.PropertyType == typeof(DateTimeOffset) ||
info.PropertyType == typeof(decimal) ||
info.PropertyType == typeof(Guid) ||
info.PropertyType == typeof(string) ||
info.PropertyType == typeof(TimeSpan)) ||
IsNullable(info.PropertyType) &&
Nullable.GetUnderlyingType(info.PropertyType).IsPrimitive)
IsMatchingType(info.PropertyType) ||
IsNullable(info.PropertyType) &&
IsMatchingType(Nullable.GetUnderlyingType(info.PropertyType)))
.ToList();
var columns = properties.Select(property => new DataColumn(property.Name, GetDataColumnType(property.PropertyType)))
Expand Down

0 comments on commit 5137d07

Please sign in to comment.