From a7dc991bf9355242038d3ed69be8d8357ca89f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Fri, 20 Sep 2024 15:26:05 +0300 Subject: [PATCH 01/13] datetime_filter_fix --- .../Edm/EdmPrimitiveHelper.cs | 5 ++--- .../Edm/EdmPrimitiveHelperTests.cs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs index 33e4cf031..5312c6336 100644 --- a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs +++ b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs @@ -101,12 +101,11 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo } else if (type == typeof(DateTime)) { - if (value is DateTimeOffset) + if (value is DateTimeOffset dateTimeOffsetValue) { - DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value; TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local; dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone); - return dateTimeOffsetValue.DateTime; + return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, DateTimeKind.Utc); } if (value is Date) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index a3f6c63d6..991e8ff09 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -94,7 +94,7 @@ public static TheoryDataSet ConvertDateTime_NonStandardPrimitive [Theory] [MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_Data))] - [MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_ExtraData))] + [MemberData(nameof(ConvertPrimitiveValue_NonStandardPrimitives_ExtraData))] public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType) { // Arrange & Act @@ -146,4 +146,20 @@ public void ConvertPrimitiveValue_Throws(object valueToConvert, Type conversionT () => EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, conversionType), exception); } + + [Theory] + [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] + public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) + { + //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc + //and do not accept Local and Unspecified in the new versions of the framework + + // Arrange & Act + object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); + + // Assert + DateTime dt = Assert.IsType(actual); + Assert.Equal(valueToConvert.LocalDateTime, dt); + Assert.Equal(DateTimeKind.Utc, dt.Kind); + } } From 7da7d308397fceb3470f61d544454a9d4fa9a537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Fri, 20 Sep 2024 15:30:16 +0300 Subject: [PATCH 02/13] comment --- .../Edm/EdmPrimitiveHelperTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index 991e8ff09..de95dbab1 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -154,6 +154,8 @@ public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc //and do not accept Local and Unspecified in the new versions of the framework + //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported + // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); From 3cd4fd4acd9f79eaf98b68eebdffff4fedd3697a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Fri, 20 Sep 2024 15:56:46 +0300 Subject: [PATCH 03/13] minor refactor --- .../Edm/EdmPrimitiveHelperTests.cs | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index de95dbab1..f864d73a9 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -124,6 +124,23 @@ public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateT Assert.Equal(valueToConvert.LocalDateTime, dt); } + [Theory] + [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] + public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) + { + //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc + //and do not accept Local and Unspecified in the new versions of the framework + + //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported + + // Arrange & Act + object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); + + // Assert + DateTime dt = Assert.IsType(actual); + Assert.Equal(DateTimeKind.Utc, dt.Kind); + } + [Theory] [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] public void ConvertDateTimeValue_NonStandardPrimitives_CustomTimeZoneInfo(DateTimeOffset valueToConvert) @@ -146,22 +163,4 @@ public void ConvertPrimitiveValue_Throws(object valueToConvert, Type conversionT () => EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, conversionType), exception); } - - [Theory] - [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] - public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) - { - //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc - //and do not accept Local and Unspecified in the new versions of the framework - - //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported - - // Arrange & Act - object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); - - // Assert - DateTime dt = Assert.IsType(actual); - Assert.Equal(valueToConvert.LocalDateTime, dt); - Assert.Equal(DateTimeKind.Utc, dt.Kind); - } } From cafa2600cf8dc5a8ee910b41b31f6886232ecab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Mon, 23 Sep 2024 16:41:37 +0300 Subject: [PATCH 04/13] minor refactor --- src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs index 5312c6336..1580ae42b 100644 --- a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs +++ b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs @@ -101,8 +101,9 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo } else if (type == typeof(DateTime)) { - if (value is DateTimeOffset dateTimeOffsetValue) + if (value is DateTimeOffset) { + DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value; TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local; dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone); return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, DateTimeKind.Utc); From 83450c03b0603e9fa1be4339d323b3bcd4485ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Mon, 23 Sep 2024 17:07:38 +0300 Subject: [PATCH 05/13] fix: set behavior depending on timeZoneInfo --- .../Edm/EdmPrimitiveHelper.cs | 19 ++++++++-- .../Edm/EdmPrimitiveHelperTests.cs | 36 ++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs index 1580ae42b..f607d600c 100644 --- a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs +++ b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs @@ -105,8 +105,12 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo { DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value; TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local; + dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone); - return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, DateTimeKind.Utc); + + var dateTimeKind = GetTargetDateTimeKind(timeZone); + + return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, dateTimeKind); } if (value is Date) @@ -183,4 +187,15 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo } } } -} + + private static DateTimeKind GetTargetDateTimeKind(TimeZoneInfo timeZone) + { + if (timeZone.Equals(TimeZoneInfo.Local)) + return DateTimeKind.Local; + + if (timeZone.Equals(TimeZoneInfo.Utc)) + return DateTimeKind.Utc; + + return DateTimeKind.Unspecified; + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index f864d73a9..6b67ca1e8 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -126,7 +126,7 @@ public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateT [Theory] [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] - public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) + public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert) { //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc //and do not accept Local and Unspecified in the new versions of the framework @@ -136,6 +136,40 @@ public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert) // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); + // Assert + DateTime dt = Assert.IsType(actual); + Assert.Equal(DateTimeKind.Local, dt.Kind); + } + + [Theory] + [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] + public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert) + { + //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc + //and do not accept Local and Unspecified in the new versions of the framework + + //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported + + // Arrange & Act + object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Local); + + // Assert + DateTime dt = Assert.IsType(actual); + Assert.Equal(DateTimeKind.Local, dt.Kind); + } + + [Theory] + [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] + public void ConvertDateTimeValue_ExplicitUtcKind(DateTimeOffset valueToConvert) + { + //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc + //and do not accept Local and Unspecified in the new versions of the framework + + //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported + + // Arrange & Act + object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Utc); + // Assert DateTime dt = Assert.IsType(actual); Assert.Equal(DateTimeKind.Utc, dt.Kind); From 46dca45059f27a6657a7ef3998d114c0acd45374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Mon, 23 Sep 2024 17:53:59 +0300 Subject: [PATCH 06/13] minor cleanup --- .../Edm/EdmPrimitiveHelperTests.cs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index 6b67ca1e8..2f8efd7d1 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -128,11 +128,6 @@ public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateT [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert) { - //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc - //and do not accept Local and Unspecified in the new versions of the framework - - //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported - // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); @@ -145,11 +140,6 @@ public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert) [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert) { - //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc - //and do not accept Local and Unspecified in the new versions of the framework - - //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported - // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Local); @@ -162,11 +152,6 @@ public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert [MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))] public void ConvertDateTimeValue_ExplicitUtcKind(DateTimeOffset valueToConvert) { - //Some databases (for example, Npgsql) require an explicit indication of Kind = Utc - //and do not accept Local and Unspecified in the new versions of the framework - - //example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported - // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Utc); @@ -197,4 +182,4 @@ public void ConvertPrimitiveValue_Throws(object valueToConvert, Type conversionT () => EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, conversionType), exception); } -} +} \ No newline at end of file From 06781004f3c2390e1b237e41d9da2302961992be Mon Sep 17 00:00:00 2001 From: VitaliyChaban <95292018+VitaliyChaban@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:14:57 +0200 Subject: [PATCH 07/13] Update src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs minor refactor Co-authored-by: Samuel Wanjohi --- src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs index f607d600c..cba371541 100644 --- a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs +++ b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs @@ -108,7 +108,7 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone); - var dateTimeKind = GetTargetDateTimeKind(timeZone); + DateTimeKind dateTimeKind = GetTargetDateTimeKind(timeZone); return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, dateTimeKind); } From 010265ca49fa9cd97cdd0bc56fd50c88037b032d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Tue, 26 Nov 2024 17:30:40 +0200 Subject: [PATCH 08/13] check utc first --- src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs index cba371541..7173891c6 100644 --- a/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs +++ b/src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs @@ -190,12 +190,12 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo private static DateTimeKind GetTargetDateTimeKind(TimeZoneInfo timeZone) { - if (timeZone.Equals(TimeZoneInfo.Local)) - return DateTimeKind.Local; - if (timeZone.Equals(TimeZoneInfo.Utc)) return DateTimeKind.Utc; + if (timeZone.Equals(TimeZoneInfo.Local)) + return DateTimeKind.Local; + return DateTimeKind.Unspecified; } } \ No newline at end of file From 41057f8c2388c339486df7964dcddb97edf481bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Tue, 26 Nov 2024 17:40:47 +0200 Subject: [PATCH 09/13] improve tests --- .../Edm/EdmPrimitiveHelperTests.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index 2f8efd7d1..ec4f3d374 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -143,9 +143,14 @@ public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Local); + //if server local time is UTC, then expect Utc Kind + DateTimeKind expectedTimeKind = TimeZoneInfo.Local.Equals(TimeZoneInfo.Utc) + ? DateTimeKind.Utc + : DateTimeKind.Local; + // Assert DateTime dt = Assert.IsType(actual); - Assert.Equal(DateTimeKind.Local, dt.Kind); + Assert.Equal(expectedTimeKind, dt.Kind); } [Theory] From 90903c19ee182b625f31deb950c2f772fbced4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Tue, 26 Nov 2024 18:09:58 +0200 Subject: [PATCH 10/13] Implicit test fix --- .../Edm/EdmPrimitiveHelperTests.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index ec4f3d374..8ed642bcf 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -131,6 +131,11 @@ public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert) // Arrange & Act object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime)); + //if server local time is UTC, then expect Utc Kind + DateTimeKind expectedTimeKind = TimeZoneInfo.Local.Equals(TimeZoneInfo.Utc) + ? DateTimeKind.Utc + : DateTimeKind.Local; + // Assert DateTime dt = Assert.IsType(actual); Assert.Equal(DateTimeKind.Local, dt.Kind); From 554b662dd77f5330a141e2c27c6cc672a0339e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Tue, 26 Nov 2024 18:19:08 +0200 Subject: [PATCH 11/13] Implicit test fix --- .../Edm/EdmPrimitiveHelperTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs index 8ed642bcf..745388fbb 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Edm/EdmPrimitiveHelperTests.cs @@ -138,7 +138,7 @@ public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert) // Assert DateTime dt = Assert.IsType(actual); - Assert.Equal(DateTimeKind.Local, dt.Kind); + Assert.Equal(expectedTimeKind, dt.Kind); } [Theory] From 34b9ef8302a291afaa2ff3dfff4baca46493ab39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Wed, 27 Nov 2024 15:11:32 +0200 Subject: [PATCH 12/13] add filter by dateTime test --- .../DateAndTimeOfDayWithEfTest.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs b/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs index d193d6c3b..3a21ed0fc 100644 --- a/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs +++ b/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs @@ -189,6 +189,7 @@ public async Task CanSelect_OnDateAndTimeOfDayProperties() [InlineData("?$filter=PublishDay ne 2016-03-22", "1,3,4,5")] [InlineData("?$filter=PublishDay lt 2016-03-22", "4")] [InlineData("?$filter=EndTime ne null", "1,3,5")] + [InlineData($"?$filter=LastAction eq {DateAndTimeOfDayWithEfTestConstants.DefaultLastActionValue}", "1,2,3,4,5")] // [InlineData("?$filter=CreatedTime eq 04:03:05.0790000", "4")] // EFCore could not be translated. // [InlineData("?$filter=hour(EndTime) eq 11", "1")] // EFCore could not be translated. // [InlineData("?$filter=minute(EndTime) eq 06", "3")] // EFCore could not be translated. @@ -308,7 +309,9 @@ public class DateAndTimeOfDayModelsController : ODataController public DateAndTimeOfDayModelsController(EfDateAndTimeOfDayModelContext context) { + context.Database.EnsureDeleted(); context.Database.EnsureCreated(); + if (!context.DateTimes.Any()) { DateTime dt = new DateTime(2015, 12, 22); @@ -318,6 +321,7 @@ public DateAndTimeOfDayModelsController(EfDateAndTimeOfDayModelContext context) { // Id = i, Birthday = dt.AddYears(i), + LastAction = DateTime.Parse(DateAndTimeOfDayWithEfTestConstants.DefaultLastActionValue), EndDay = dt.AddDays(i), DeliverDay = i % 2 == 0 ? (DateTime?)null : dt.AddYears(5 - i), PublishDay = i % 2 != 0 ? (DateTime?)null : dt.AddMonths(5 - i), @@ -355,7 +359,7 @@ public IActionResult Get(int key) return Ok(dtm); } - public IActionResult Post([FromBody]DateAndTimeOfDayModel dt) + public IActionResult Post([FromBody] DateAndTimeOfDayModel dt) { Assert.NotNull(dt); @@ -367,7 +371,7 @@ public IActionResult Post([FromBody]DateAndTimeOfDayModel dt) return Created(dt); } - public IActionResult Put(int key, [FromBody]Delta dt) + public IActionResult Put(int key, [FromBody] Delta dt) { Assert.Equal(new[] { "Birthday", "CreatedTime" }, dt.GetChangedPropertyNames()); @@ -411,6 +415,9 @@ public class DateAndTimeOfDayModel [Column(TypeName = "date")] public DateTime Birthday { get; set; } + [Column(TypeName = "datetime")] + public DateTime LastAction { get; set; } + [Column(TypeName = "DaTe")] public DateTime? PublishDay { get; set; } @@ -426,3 +433,8 @@ public class DateAndTimeOfDayModel public TimeSpan ResumeTime { get; set; } // will use the Fluent API } + +internal static class DateAndTimeOfDayWithEfTestConstants +{ + internal const string DefaultLastActionValue = "2024-11-27T15:06:35Z"; +} \ No newline at end of file From 5a8fa03dd9106b680ff3b6c7b6294ad84630730a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD?= Date: Wed, 27 Nov 2024 15:45:01 +0200 Subject: [PATCH 13/13] Revert "add filter by dateTime test" This reverts commit 34b9ef8302a291afaa2ff3dfff4baca46493ab39. --- .../DateAndTimeOfDayWithEfTest.cs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs b/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs index 3a21ed0fc..d193d6c3b 100644 --- a/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs +++ b/test/Microsoft.AspNetCore.OData.E2E.Tests/DateAndTimeOfDay/DateAndTimeOfDayWithEfTest.cs @@ -189,7 +189,6 @@ public async Task CanSelect_OnDateAndTimeOfDayProperties() [InlineData("?$filter=PublishDay ne 2016-03-22", "1,3,4,5")] [InlineData("?$filter=PublishDay lt 2016-03-22", "4")] [InlineData("?$filter=EndTime ne null", "1,3,5")] - [InlineData($"?$filter=LastAction eq {DateAndTimeOfDayWithEfTestConstants.DefaultLastActionValue}", "1,2,3,4,5")] // [InlineData("?$filter=CreatedTime eq 04:03:05.0790000", "4")] // EFCore could not be translated. // [InlineData("?$filter=hour(EndTime) eq 11", "1")] // EFCore could not be translated. // [InlineData("?$filter=minute(EndTime) eq 06", "3")] // EFCore could not be translated. @@ -309,9 +308,7 @@ public class DateAndTimeOfDayModelsController : ODataController public DateAndTimeOfDayModelsController(EfDateAndTimeOfDayModelContext context) { - context.Database.EnsureDeleted(); context.Database.EnsureCreated(); - if (!context.DateTimes.Any()) { DateTime dt = new DateTime(2015, 12, 22); @@ -321,7 +318,6 @@ public DateAndTimeOfDayModelsController(EfDateAndTimeOfDayModelContext context) { // Id = i, Birthday = dt.AddYears(i), - LastAction = DateTime.Parse(DateAndTimeOfDayWithEfTestConstants.DefaultLastActionValue), EndDay = dt.AddDays(i), DeliverDay = i % 2 == 0 ? (DateTime?)null : dt.AddYears(5 - i), PublishDay = i % 2 != 0 ? (DateTime?)null : dt.AddMonths(5 - i), @@ -359,7 +355,7 @@ public IActionResult Get(int key) return Ok(dtm); } - public IActionResult Post([FromBody] DateAndTimeOfDayModel dt) + public IActionResult Post([FromBody]DateAndTimeOfDayModel dt) { Assert.NotNull(dt); @@ -371,7 +367,7 @@ public IActionResult Post([FromBody] DateAndTimeOfDayModel dt) return Created(dt); } - public IActionResult Put(int key, [FromBody] Delta dt) + public IActionResult Put(int key, [FromBody]Delta dt) { Assert.Equal(new[] { "Birthday", "CreatedTime" }, dt.GetChangedPropertyNames()); @@ -415,9 +411,6 @@ public class DateAndTimeOfDayModel [Column(TypeName = "date")] public DateTime Birthday { get; set; } - [Column(TypeName = "datetime")] - public DateTime LastAction { get; set; } - [Column(TypeName = "DaTe")] public DateTime? PublishDay { get; set; } @@ -433,8 +426,3 @@ public class DateAndTimeOfDayModel public TimeSpan ResumeTime { get; set; } // will use the Fluent API } - -internal static class DateAndTimeOfDayWithEfTestConstants -{ - internal const string DefaultLastActionValue = "2024-11-27T15:06:35Z"; -} \ No newline at end of file