From 54766021283900bc55cd20bde940eec10579afb8 Mon Sep 17 00:00:00 2001 From: maumar Date: Tue, 4 Oct 2022 19:54:36 -0700 Subject: [PATCH] Fix to #29260 - Sqlite: AddTicks translation gives incorrect results We were using TimeSpan.TicksPerDay rather than TicksPerSecond in the translation. Fixing the typo and correcting the test. Fixes #29260 --- .../Query/Internal/SqliteDateTimeAddTranslator.cs | 2 +- .../Query/NorthwindMiscellaneousQuerySqliteTest.cs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeAddTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeAddTranslator.cs index 9af6bd69053..c827a3812a6 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeAddTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeAddTranslator.cs @@ -84,7 +84,7 @@ public SqliteDateTimeAddTranslator(SqliteSqlExpressionFactory sqlExpressionFacto _sqlExpressionFactory.Convert( _sqlExpressionFactory.Divide( arguments[0], - _sqlExpressionFactory.Constant((double)TimeSpan.TicksPerDay)), + _sqlExpressionFactory.Constant((double)TimeSpan.TicksPerSecond)), typeof(string)), _sqlExpressionFactory.Constant(" seconds")); } diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindMiscellaneousQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindMiscellaneousQuerySqliteTest.cs index cd15e05344e..9e42026809c 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindMiscellaneousQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindMiscellaneousQuerySqliteTest.cs @@ -149,14 +149,15 @@ public override async Task Select_expression_datetime_add_second(bool async) public override async Task Select_expression_datetime_add_ticks(bool async) { - // Add ticks. Issue #25851. - Assert.Equal( - "1996-07-04T00:00:00.0000000", - (await Assert.ThrowsAsync( - () => base.Select_expression_datetime_add_ticks(async))).Actual); + // modifying the original scenario - Sqlite gives inaccurate results for values of granularity less than 1 second + await AssertQuery( + async, + ss => ss.Set().Where(o => o.OrderDate != null) + .Select(o => new Order { OrderDate = o.OrderDate.Value.AddTicks(10 * TimeSpan.TicksPerSecond) }), + e => e.OrderDate); AssertSql( - @"SELECT rtrim(rtrim(strftime('%Y-%m-%d %H:%M:%f', ""o"".""OrderDate"", CAST((10000 / 864000000000) AS TEXT) || ' seconds'), '0'), '.') AS ""OrderDate"" + @"SELECT rtrim(rtrim(strftime('%Y-%m-%d %H:%M:%f', ""o"".""OrderDate"", CAST((100000000 / 10000000) AS TEXT) || ' seconds'), '0'), '.') AS ""OrderDate"" FROM ""Orders"" AS ""o"" WHERE ""o"".""OrderDate"" IS NOT NULL"); }