Skip to content

Commit

Permalink
Merge pull request #249 from dmitrii-kiselev/feature/date-time-only-f…
Browse files Browse the repository at this point in the history
…rom-string

feat(data-reader): add support for converting `DateOnly` and `TimeOnly` types from string
  • Loading branch information
VahidN authored Sep 27, 2024
2 parents 57f5908 + 580aecb commit a0698ea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
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 @@ public override T GetFieldValue<T>(int ordinal)
return (T)(object)DateOnly.FromDateTime((DateTime)value);
}

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

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);
}

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

0 comments on commit a0698ea

Please sign in to comment.