diff --git a/src/EFCoreSecondLevelCacheInterceptor/TypeExtensions.cs b/src/EFCoreSecondLevelCacheInterceptor/TypeExtensions.cs index e392466..7cf0e16 100644 --- a/src/EFCoreSecondLevelCacheInterceptor/TypeExtensions.cs +++ b/src/EFCoreSecondLevelCacheInterceptor/TypeExtensions.cs @@ -28,6 +28,11 @@ public static class TypeExtensions /// public static readonly Type ByteType = typeof(byte); + /// + /// Cached version of typeof(sbyte) + /// + public static readonly Type SByteType = typeof(sbyte); + /// /// Cached version of typeof(string) /// @@ -63,6 +68,11 @@ public static class TypeExtensions /// public static readonly Type ShortType = typeof(short); + /// + /// Cached version of typeof(ushort) + /// + public static readonly Type UShortType = typeof(ushort); + /// /// Cached version of typeof(int) /// @@ -112,7 +122,10 @@ public static bool IsArrayOrGenericList(Type? expectedValueType) => public static bool IsNumber(Type type) => type == UintType || type == IntType || type == UlongTYpe || type == LongType || - type == ShortType || type == ByteType || type == CharType; + type == UShortType || type == ShortType || + type == SByteType || type == ByteType || + type == FloatType || type == DoubleType || type == DecimalType || + type == CharType; #if NET8_0 || NET7_0 || NET6_0 /// diff --git a/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFTableRowsDataReaderTests.cs b/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFTableRowsDataReaderTests.cs index c1952e8..059d814 100644 --- a/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFTableRowsDataReaderTests.cs +++ b/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFTableRowsDataReaderTests.cs @@ -1386,17 +1386,22 @@ public void GetFieldValue_ShouldReturnExpectedTimeSpanValueFromNumber() public static IEnumerable ValidNumberData => new List { + new object[] { (sbyte)1 }, new object[] { (byte)1 }, + new object[] { (short)1 }, + new object[] { (ushort)1 }, new object[] { 1 }, - new object[] { (uint)1 }, + new object[] { 1U }, new object[] { 1L }, new object[] { 1UL }, - new object[] { (short)1 } + new object[] { 1F }, + new object[] { 1D }, + new object[] { 1M } }; - + [Theory] [MemberData(nameof(ValidNumberData))] - public void GetFieldValue_ShouldReturnExpectedNumber(object value) + public void GetFieldValue_ShouldReturnExpectedDecimalNumber(object value) { // Arrange var values = new List { value }; @@ -1414,23 +1419,69 @@ public void GetFieldValue_ShouldReturnExpectedNumber(object value) dataReader.Read(); // Act - var actual = dataReader.GetFieldValue(0); + var actual = dataReader.GetFieldValue(0); // Assert - Assert.Equal(1L, actual); + Assert.Equal(1M, actual); + } + + [Fact] + public void GetFieldValue_ShouldReturnExpectedByteNumberFromChar() + { + // Arrange + var values = new List { '1' }; + var tableRow = new EFTableRow(values); + var tableRows = new EFTableRows + { + Rows = new List { tableRow }, + ColumnsInfo = new Dictionary + { + { 0, new EFTableColumnInfo { DbTypeName = nameof(Char), Ordinal = 0 } } + } + }; + var dataReader = new EFTableRowsDataReader(tableRows); + + dataReader.Read(); + + // Act + var actual = dataReader.GetFieldValue(0); + + // Assert + Assert.Equal(49, actual); + } + + [Theory] + [MemberData(nameof(ValidNumberData))] + public void GetFieldValue_ShouldNotThrowInvalidCastExceptionWhenConvertToDecimal(object value) + { + // Arrange + var values = new List { value }; + var tableRow = new EFTableRow(values); + var tableRows = new EFTableRows + { + Rows = new List { tableRow }, + ColumnsInfo = new Dictionary + { + { 0, new EFTableColumnInfo { DbTypeName = value.GetType().Name, Ordinal = 0 } } + } + }; + var dataReader = new EFTableRowsDataReader(tableRows); + + dataReader.Read(); + + // Act + var exception = Record.Exception(() => dataReader.GetFieldValue(0)); + + // Assert + Assert.Null(exception); } public static IEnumerable InvalidNumberData => new List { - new object[] { (sbyte)1 }, - new object[] { "1" }, - new object[] { 1M }, - new object[] { 1.0 }, - new object[] { 1.0F }, new object[] { (nint)1 }, new object[] { (nuint)1 }, - new object[] { (ushort)1 } + new object[] { "1" } }; [Theory] @@ -1453,7 +1504,7 @@ public void GetFieldValue_ThrowsInvalidCastException_WhenValueIsNotNumber(object dataReader.Read(); // Act - void Act() => dataReader.GetFieldValue(0); + void Act() => dataReader.GetFieldValue(0); // Assert Assert.Throws(Act); diff --git a/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/TypeExtensionsTests.cs b/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/TypeExtensionsTests.cs index 1e38f62..f441e7a 100644 --- a/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/TypeExtensionsTests.cs +++ b/src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/TypeExtensionsTests.cs @@ -64,11 +64,11 @@ public void IsArrayOrGenericList_ShouldReturnExpectedResult(Type type, bool expe [InlineData(null, false)] [InlineData(typeof(bool), false)] [InlineData(typeof(byte), true)] - [InlineData(typeof(sbyte), false)] + [InlineData(typeof(sbyte), true)] [InlineData(typeof(char), true)] - [InlineData(typeof(decimal), false)] - [InlineData(typeof(double), false)] - [InlineData(typeof(float), false)] + [InlineData(typeof(decimal), true)] + [InlineData(typeof(double), true)] + [InlineData(typeof(float), true)] [InlineData(typeof(int), true)] [InlineData(typeof(uint), true)] [InlineData(typeof(nint), false)] @@ -76,7 +76,7 @@ public void IsArrayOrGenericList_ShouldReturnExpectedResult(Type type, bool expe [InlineData(typeof(long), true)] [InlineData(typeof(ulong), true)] [InlineData(typeof(short), true)] - [InlineData(typeof(ushort), false)] + [InlineData(typeof(ushort), true)] [InlineData(typeof(DateOnly), false)] [InlineData(typeof(DateTime), false)] [InlineData(typeof(DateTimeOffset), false)]