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

Improve test coverage fo DateTimeOffset comparer #24702

Merged
1 commit merged into from
Apr 20, 2021
Merged
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
20 changes: 16 additions & 4 deletions test/EFCore.Relational.Tests/Storage/RelationalTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
Expand Down Expand Up @@ -552,13 +553,24 @@ public virtual void Float_value_comparer_handles_NaN()
}

[ConditionalFact]
public virtual void DateTimeOffset_value_comparer_handles_different_offsets()
public virtual void DateTimeOffset_value_comparer_behaves_correctly()
{
var typeMapping = new DateTimeOffsetTypeMapping("datetimeoffset", DbType.DateTimeOffset);

Assert.False(typeMapping.Comparer.Equals(
new DateTimeOffset(2000, 1, 1, 12, 0, 0, TimeSpan.FromHours(0)),
new DateTimeOffset(2000, 1, 1, 13, 0, 0, TimeSpan.FromHours(1))));
var same1 = new DateTimeOffset(2000, 1, 1, 12, 0, 0, TimeSpan.FromHours(0));
var same2 = new DateTimeOffset(2000, 1, 1, 12, 0, 0, TimeSpan.FromHours(0));
var different = new DateTimeOffset(2000, 1, 1, 13, 0, 0, TimeSpan.FromHours(1));

// Note that a difference in offset results in inequality, unlike the .NET default comparison behavior
Assert.False(typeMapping.Comparer.Equals(same1, different));
Assert.True(typeMapping.Comparer.Equals(same1, same2));

var parameters = new[] { Expression.Parameter(typeof(DateTimeOffset)), Expression.Parameter(typeof(DateTimeOffset)) };
var equalsBody = typeMapping.Comparer.ExtractEqualsBody(parameters[0], parameters[1]);
var equalsComparer = Expression.Lambda<Func<DateTimeOffset, DateTimeOffset, bool>>(equalsBody, parameters).Compile();

Assert.False(equalsComparer(same1, different));
Assert.True(equalsComparer(same1, same2));
}

[ConditionalFact]
Expand Down