Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-reader): add support for converting DateOnly and TimeOnly types from string #249

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/EFCoreSecondLevelCacheInterceptor/EFTableRowsDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,21 @@
return (T)(object)DateOnly.FromDateTime((DateTime)value);
}

if (expectedValueType == TypeExtensions.DateOnlyType && actualValueType == TypeExtensions.StringType)
{
return (T)(object)DateOnly.Parse((string)value, CultureInfo.InvariantCulture);

Check warning

Code scanning / CodeQL

Useless upcast Warning

There is no need to upcast from
DateOnly
to
Object
- the conversion can be done implicitly.
}

if (expectedValueType == TypeExtensions.TimeOnlyType && actualValueType == TypeExtensions.TimeSpanType)
{
return (T)(object)TimeOnly.FromTimeSpan((TimeSpan)value);
}

if (expectedValueType == TypeExtensions.TimeOnlyType && actualValueType == TypeExtensions.StringType)
{
return (T)(object)TimeOnly.Parse((string)value, CultureInfo.InvariantCulture);

Check warning

Code scanning / CodeQL

Useless upcast Warning

There is no need to upcast from
TimeOnly
to
Object
- the conversion can be done implicitly.
}

if (expectedValueType == TypeExtensions.TimeOnlyType && isActualValueTypeNumber)
{
return (T)(object)TimeOnly.FromTimeSpan(new TimeSpan(Convert.ToInt64(value, CultureInfo.InvariantCulture)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ public void GetFieldValue_ShouldReturnExpectedDateOnlyValueFromDateTime()
}

[Fact]
public void GetFieldValue__ShouldReturnExpectedDateTimeOffsetValueFromString()
public void GetFieldValue_ShouldReturnExpectedDateOnlyValueFromString()
{
// Arrange
var values = new List<object> { DateOnly.MaxValue.ToString(CultureInfo.InvariantCulture) };
Expand All @@ -1580,15 +1580,42 @@ public void GetFieldValue__ShouldReturnExpectedDateTimeOffsetValueFromString()
{ 0, new EFTableColumnInfo { DbTypeName = nameof(String), Ordinal = 0 } }
}
};

var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
void Act() => dataReader.GetFieldValue<DateOnly>(0);
var actual = dataReader.GetFieldValue<DateOnly>(0);

// Assert
Assert.Throws<InvalidCastException>(Act);
Assert.Equal(DateOnly.MaxValue, actual);
}

[Fact]
public void GetFieldValue_ShouldNotThrowInvalidCastExceptionWhenValueConversionFromStringToDateOnly()
{
// Arrange
var values = new List<object> { DateOnly.MaxValue.ToString(CultureInfo.InvariantCulture) };
var tableRow = new EFTableRow(values);
var tableRows = new EFTableRows
{
Rows = new List<EFTableRow> { tableRow },
ColumnsInfo = new Dictionary<int, EFTableColumnInfo>
{
{ 0, new EFTableColumnInfo { DbTypeName = nameof(String), Ordinal = 0 } }
}
};

var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
var exception = Record.Exception(() => dataReader.GetFieldValue<DateOnly>(0));

// Assert
Assert.Null(exception);
}

[Fact]
Expand Down Expand Up @@ -1643,6 +1670,32 @@ public void GetFieldValue_ShouldReturnExpectedTimeOnlyValueFromNumber()

[Fact]
public void GetFieldValue_ShouldReturnExpectedTimeOnlyValueFromString()
{
// Arrange
var values = new List<object> { TimeOnly.MaxValue.ToString("O", CultureInfo.InvariantCulture) };
var tableRow = new EFTableRow(values);
var tableRows = new EFTableRows
{
Rows = new List<EFTableRow> { tableRow },
ColumnsInfo = new Dictionary<int, EFTableColumnInfo>
{
{ 0, new EFTableColumnInfo { DbTypeName = nameof(String), Ordinal = 0 } }
}
};

var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
var actual = dataReader.GetFieldValue<TimeOnly>(0);

// Assert
Assert.Equal(TimeOnly.MaxValue, actual);
}

[Fact]
public void GetFieldValue_ShouldNotThrowInvalidCastExceptionWhenValueConversionFromStringToTimeOnly()
{
// Arrange
var values = new List<object> { TimeOnly.MaxValue.ToString(CultureInfo.InvariantCulture) };
Expand All @@ -1652,18 +1705,19 @@ public void GetFieldValue_ShouldReturnExpectedTimeOnlyValueFromString()
Rows = new List<EFTableRow> { tableRow },
ColumnsInfo = new Dictionary<int, EFTableColumnInfo>
{
{ 0, new EFTableColumnInfo { DbTypeName = nameof(TimeSpan), Ordinal = 0 } }
{ 0, new EFTableColumnInfo { DbTypeName = nameof(String), Ordinal = 0 } }
}
};

var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
void Act() => dataReader.GetFieldValue<DateOnly>(0);
var exception = Record.Exception(() => dataReader.GetFieldValue<TimeOnly>(0));

// Assert
Assert.Throws<InvalidCastException>(Act);
Assert.Null(exception);
}

[Fact]
Expand Down
Loading