From 9ca9e93712b085aa588d45d20c9dc8c0de1ed8fa Mon Sep 17 00:00:00 2001 From: Steven Rasmussen Date: Mon, 8 Nov 2021 09:09:28 -0700 Subject: [PATCH] Fixes #25 --- .../DateTimeQueryTests.cs | 26 +++++++++++++++++++ .../Models/Race.cs | 4 +-- .../Models/RacingContext.cs | 24 ++++++++--------- .../QueryTestBase.cs | 2 +- .../BaseNodaTimeMemberTranslator.cs | 2 -- .../NodaTimeTypesMemberTranslatorPlugin.cs | 13 +++++----- 6 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/DateTimeQueryTests.cs diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/DateTimeQueryTests.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/DateTimeQueryTests.cs new file mode 100644 index 0000000..9b4a48c --- /dev/null +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/DateTimeQueryTests.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +namespace SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests +{ + public class DateTimeQueryTests : QueryTestBase + { + public DateTimeQueryTests(DatabaseTestFixture databaseTestFixture) + : base(databaseTestFixture) { } + + [Fact] + public async Task DateTime_Date_Test() + { + var raceResults = await this.Db.Race.Where(r => r.DateTimeDate.Date >= new DateTime(2019, 7, 1)).ToListAsync(); + + Assert.Equal( + condense(@$"{RaceSelectStatement} WHERE CONVERT(date, [r].[DateTimeDate]) >= '2019-07-01T00:00:00.0000000'"), + condense(this.Db.Sql)); + + Assert.Equal(6, raceResults.Count); + } + } +} \ No newline at end of file diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/Race.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/Race.cs index 5923585..7320013 100644 --- a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/Race.cs +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/Race.cs @@ -1,7 +1,5 @@ using NodaTime; using System; -using System.Collections.Generic; -using System.Text; namespace SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests.Models { @@ -11,6 +9,8 @@ public class Race public LocalDate Date { get; set; } + public DateTime DateTimeDate { get; set; } + public LocalDateTime ScheduledStart { get; set; } public LocalTime ScheduledStartTime { get; set; } diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/RacingContext.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/RacingContext.cs index a0dcd54..71036fb 100644 --- a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/RacingContext.cs +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/Models/RacingContext.cs @@ -38,18 +38,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasData( - new Race { Id = 1, Date = new LocalDate(2019, 1, 1), ScheduledStartTime = new LocalTime(8, 0, 0, 0), ScheduledDuration = DurationExtensions.FromParts(1, 0, 0, 0), ScheduledStart = new LocalDateTime(2019, 1, 1, 8, 0, 0, 0) }, - new Race { Id = 2, Date = new LocalDate(2019, 2, 1), ScheduledStartTime = new LocalTime(9, 1, 1, 1), ScheduledDuration = DurationExtensions.FromParts(2, 1, 1, 1), ScheduledStart = new LocalDateTime(2019, 2, 1, 9, 1, 1, 1) }, - new Race { Id = 3, Date = new LocalDate(2019, 3, 1), ScheduledStartTime = new LocalTime(10, 2, 2, 2), ScheduledDuration = DurationExtensions.FromParts(3, 2, 2, 2), ScheduledStart = new LocalDateTime(2019, 3, 1, 10, 2, 2, 2) }, - new Race { Id = 4, Date = new LocalDate(2019, 4, 1), ScheduledStartTime = new LocalTime(11, 3, 3, 3), ScheduledDuration = DurationExtensions.FromParts(4, 3, 3, 3), ScheduledStart = new LocalDateTime(2019, 4, 1, 11, 3, 3, 3) }, - new Race { Id = 5, Date = new LocalDate(2019, 5, 1), ScheduledStartTime = new LocalTime(12, 4, 4, 4), ScheduledDuration = DurationExtensions.FromParts(5, 4, 4, 4), ScheduledStart = new LocalDateTime(2019, 5, 1, 12, 4, 4, 4) }, - new Race { Id = 6, Date = new LocalDate(2019, 6, 1), ScheduledStartTime = new LocalTime(13, 5, 5, 5), ScheduledDuration = DurationExtensions.FromParts(6, 5, 5, 5), ScheduledStart = new LocalDateTime(2019, 6, 1, 13, 5, 5, 5) }, - new Race { Id = 7, Date = new LocalDate(2019, 7, 1), ScheduledStartTime = new LocalTime(14, 6, 6, 6), ScheduledDuration = DurationExtensions.FromParts(7, 6, 6, 6), ScheduledStart = new LocalDateTime(2019, 7, 1, 14, 6, 6, 6) }, - new Race { Id = 8, Date = new LocalDate(2019, 8, 1), ScheduledStartTime = new LocalTime(15, 7, 7, 7), ScheduledDuration = DurationExtensions.FromParts(8, 7, 7, 7), ScheduledStart = new LocalDateTime(2019, 8, 1, 15, 7, 7, 7) }, - new Race { Id = 9, Date = new LocalDate(2019, 9, 1), ScheduledStartTime = new LocalTime(16, 8, 8, 8), ScheduledDuration = DurationExtensions.FromParts(9, 8, 8, 8), ScheduledStart = new LocalDateTime(2019, 9, 1, 16, 8, 8, 8) }, - new Race { Id = 10, Date = new LocalDate(2019, 10, 1), ScheduledStartTime = new LocalTime(17, 9, 9, 9), ScheduledDuration = DurationExtensions.FromParts(10, 9, 9, 9), ScheduledStart = new LocalDateTime(2019, 10, 1, 17, 9, 9, 9) }, - new Race { Id = 11, Date = new LocalDate(2019, 11, 1), ScheduledStartTime = new LocalTime(18, 10, 10, 10), ScheduledDuration = DurationExtensions.FromParts(11, 10, 10, 10), ScheduledStart = new LocalDateTime(2019, 11, 1, 18, 10, 10, 10) }, - new Race { Id = 12, Date = new LocalDate(2019, 12, 1), ScheduledStartTime = new LocalTime(19, 11, 11, 11), ScheduledDuration = DurationExtensions.FromParts(12, 11, 11, 11), ScheduledStart = new LocalDateTime(2019, 12, 1, 19, 11, 11, 11) } + new Race { Id = 1, Date = new LocalDate(2019, 1, 1), DateTimeDate = new DateTime(2019, 1, 1), ScheduledStartTime = new LocalTime(8, 0, 0, 0), ScheduledDuration = DurationExtensions.FromParts(1, 0, 0, 0), ScheduledStart = new LocalDateTime(2019, 1, 1, 8, 0, 0, 0) }, + new Race { Id = 2, Date = new LocalDate(2019, 2, 1), DateTimeDate = new DateTime(2019, 2, 1), ScheduledStartTime = new LocalTime(9, 1, 1, 1), ScheduledDuration = DurationExtensions.FromParts(2, 1, 1, 1), ScheduledStart = new LocalDateTime(2019, 2, 1, 9, 1, 1, 1) }, + new Race { Id = 3, Date = new LocalDate(2019, 3, 1), DateTimeDate = new DateTime(2019, 3, 1), ScheduledStartTime = new LocalTime(10, 2, 2, 2), ScheduledDuration = DurationExtensions.FromParts(3, 2, 2, 2), ScheduledStart = new LocalDateTime(2019, 3, 1, 10, 2, 2, 2) }, + new Race { Id = 4, Date = new LocalDate(2019, 4, 1), DateTimeDate = new DateTime(2019, 4, 1), ScheduledStartTime = new LocalTime(11, 3, 3, 3), ScheduledDuration = DurationExtensions.FromParts(4, 3, 3, 3), ScheduledStart = new LocalDateTime(2019, 4, 1, 11, 3, 3, 3) }, + new Race { Id = 5, Date = new LocalDate(2019, 5, 1), DateTimeDate = new DateTime(2019, 5, 1), ScheduledStartTime = new LocalTime(12, 4, 4, 4), ScheduledDuration = DurationExtensions.FromParts(5, 4, 4, 4), ScheduledStart = new LocalDateTime(2019, 5, 1, 12, 4, 4, 4) }, + new Race { Id = 6, Date = new LocalDate(2019, 6, 1), DateTimeDate = new DateTime(2019, 6, 1), ScheduledStartTime = new LocalTime(13, 5, 5, 5), ScheduledDuration = DurationExtensions.FromParts(6, 5, 5, 5), ScheduledStart = new LocalDateTime(2019, 6, 1, 13, 5, 5, 5) }, + new Race { Id = 7, Date = new LocalDate(2019, 7, 1), DateTimeDate = new DateTime(2019, 7, 1), ScheduledStartTime = new LocalTime(14, 6, 6, 6), ScheduledDuration = DurationExtensions.FromParts(7, 6, 6, 6), ScheduledStart = new LocalDateTime(2019, 7, 1, 14, 6, 6, 6) }, + new Race { Id = 8, Date = new LocalDate(2019, 8, 1), DateTimeDate = new DateTime(2019, 8, 1), ScheduledStartTime = new LocalTime(15, 7, 7, 7), ScheduledDuration = DurationExtensions.FromParts(8, 7, 7, 7), ScheduledStart = new LocalDateTime(2019, 8, 1, 15, 7, 7, 7) }, + new Race { Id = 9, Date = new LocalDate(2019, 9, 1), DateTimeDate = new DateTime(2019, 9, 1), ScheduledStartTime = new LocalTime(16, 8, 8, 8), ScheduledDuration = DurationExtensions.FromParts(9, 8, 8, 8), ScheduledStart = new LocalDateTime(2019, 9, 1, 16, 8, 8, 8) }, + new Race { Id = 10, Date = new LocalDate(2019, 10, 1), DateTimeDate = new DateTime(2019, 10, 1), ScheduledStartTime = new LocalTime(17, 9, 9, 9), ScheduledDuration = DurationExtensions.FromParts(10, 9, 9, 9), ScheduledStart = new LocalDateTime(2019, 10, 1, 17, 9, 9, 9) }, + new Race { Id = 11, Date = new LocalDate(2019, 11, 1), DateTimeDate = new DateTime(2019, 11, 1), ScheduledStartTime = new LocalTime(18, 10, 10, 10), ScheduledDuration = DurationExtensions.FromParts(11, 10, 10, 10), ScheduledStart = new LocalDateTime(2019, 11, 1, 18, 10, 10, 10) }, + new Race { Id = 12, Date = new LocalDate(2019, 12, 1), DateTimeDate = new DateTime(2019, 12, 1), ScheduledStartTime = new LocalTime(19, 11, 11, 11), ScheduledDuration = DurationExtensions.FromParts(12, 11, 11, 11), ScheduledStart = new LocalDateTime(2019, 12, 1, 19, 11, 11, 11) } ); diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/QueryTestBase.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/QueryTestBase.cs index 51faeb5..9a56ed9 100644 --- a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/QueryTestBase.cs +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests/QueryTestBase.cs @@ -22,7 +22,7 @@ public QueryTestBase(DatabaseTestFixture databaseTestFixture) protected DbFunctions Functions { get; } - protected string RaceSelectStatement => "SELECT [r].[Id], [r].[Date], [r].[ScheduledDuration], [r].[ScheduledStart], [r].[ScheduledStartTime] FROM [Race] AS [r]"; + protected string RaceSelectStatement => "SELECT [r].[Id], [r].[Date], [r].[DateTimeDate], [r].[ScheduledDuration], [r].[ScheduledStart], [r].[ScheduledStartTime] FROM [Race] AS [r]"; protected string RaceResultSelectStatement => "SELECT [r].[Id], [r].[EndTime], [r].[OffsetFromWinner], [r].[StartTime], [r].[StartTimeOffset] FROM [RaceResult] AS [r]"; diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/BaseNodaTimeMemberTranslator.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/BaseNodaTimeMemberTranslator.cs index b269194..050de92 100644 --- a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/BaseNodaTimeMemberTranslator.cs +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/BaseNodaTimeMemberTranslator.cs @@ -2,11 +2,9 @@ using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query.SqlExpressions; -using NodaTime; using System; using System.Collections.Generic; using System.Reflection; -using System.Text; namespace Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators { diff --git a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/NodaTimeTypesMemberTranslatorPlugin.cs b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/NodaTimeTypesMemberTranslatorPlugin.cs index 065897d..4bdbf08 100644 --- a/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/NodaTimeTypesMemberTranslatorPlugin.cs +++ b/src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Query/ExpressionTranslators/NodaTimeTypesMemberTranslatorPlugin.cs @@ -1,15 +1,16 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Query; -using System; -using System.Collections.Generic; -using System.Text; +using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; +using Microsoft.EntityFrameworkCore.Storage; namespace Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators { - public class NodaTimeTypesMemberTranslatorPlugin : RelationalMemberTranslatorProvider + public class NodaTimeTypesMemberTranslatorPlugin : SqlServerMemberTranslatorProvider { - public NodaTimeTypesMemberTranslatorPlugin([NotNull] RelationalMemberTranslatorProviderDependencies dependencies) - : base(dependencies) + public NodaTimeTypesMemberTranslatorPlugin( + [NotNull] RelationalMemberTranslatorProviderDependencies dependencies, + IRelationalTypeMappingSource typeMappingSource) + : base(dependencies, typeMappingSource) { var sqlExpressionFactory = dependencies.SqlExpressionFactory;