Skip to content

Commit 583a73f

Browse files
committed
Add simple regression test for DateOnly support
1 parent e946854 commit 583a73f

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

RepoDb.Core/RepoDb/StaticType.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ internal static class StaticType
9595
/// Gets a type of the <see cref="System.DateTimeOffset"/> .NET CLR type.
9696
/// </summary>
9797
public static Type DateTimeOffset => typeof(DateTimeOffset);
98-
99-
#if NET6_0_OR_GREATER
98+
99+
#if NET6_0_OR_GREATER
100100
/// <summary>
101101
/// Gets a type of the <see cref="System.DateOnly"/> .NET CLR type.
102102
/// </summary>
@@ -105,8 +105,8 @@ internal static class StaticType
105105
/// Gets a type of the <see cref="System.TimeOnly"/> .NET CLR type.
106106
/// </summary>
107107
public static Type TimeOnly => typeof(TimeOnly);
108-
#endif
109-
108+
#endif
109+
110110
/// <summary>
111111
/// Gets a type of the <see cref="System.Data.Common.DbCommand"/> .NET CLR type.
112112
/// </summary>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Microsoft.Data.SqlClient;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using RepoDb.SqlServer.IntegrationTests.Setup;
4+
using System;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace RepoDb.SqlServer.IntegrationTests
9+
{
10+
[TestClass]
11+
public class AdditionalDbTypesTests
12+
{
13+
[TestInitialize]
14+
public void Initialize()
15+
{
16+
GlobalConfiguration
17+
.Setup(new()
18+
{
19+
ConversionType = Enumerations.ConversionType.Automatic
20+
})
21+
.UseSqlServer();
22+
23+
Database.Initialize();
24+
Cleanup();
25+
26+
using var connection = new SqlConnection(Database.ConnectionString).EnsureOpen();
27+
28+
connection.ExecuteNonQuery($@"
29+
IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = '{nameof(DateOnlyTestData)}'))
30+
BEGIN
31+
CREATE TABLE [dbo].[{nameof(DateOnlyTestData)}] (
32+
[Id] INT IDENTITY(1, 1),
33+
[DateOnly] DATE NOT NULL,
34+
[DateOnlyNullable] DATE NULL,
35+
CONSTRAINT [{nameof(DateOnlyTestData)}_Id] PRIMARY KEY
36+
(
37+
[Id] ASC
38+
)
39+
) ON [PRIMARY]
40+
END");
41+
42+
// Do this again as this is now overwritten
43+
GlobalConfiguration
44+
.Setup(new()
45+
{
46+
ConversionType = Enumerations.ConversionType.Automatic
47+
})
48+
.UseSqlServer();
49+
}
50+
51+
[TestCleanup]
52+
public void Cleanup()
53+
{
54+
Database.Cleanup();
55+
}
56+
57+
[TestMethod]
58+
public async Task TestDateTimeOnlyInsertQuery()
59+
{
60+
await using var connection = new SqlConnection(Database.ConnectionString);
61+
await connection.OpenAsync();
62+
await using var t = await connection.BeginTransactionAsync();
63+
64+
await connection.InsertAllAsync(
65+
new DateOnlyTestData[] {
66+
new()
67+
{
68+
DateOnly = new DateOnly(2024,1,1),
69+
DateOnlyNullable = null,
70+
},
71+
new ()
72+
{
73+
DateOnly = new DateOnly(2025,1,1),
74+
DateOnlyNullable = new DateOnly(2026,1,1),
75+
}
76+
},
77+
transaction: t);
78+
79+
80+
var all = await connection.QueryAllAsync<DateOnlyTestData>(transaction: t);
81+
82+
Assert.IsTrue(all.Any(x => x.DateOnly == new DateOnly(2024, 1, 1)), "Found DateOnly");
83+
Assert.IsTrue(all.Any(x => x.DateOnlyNullable == new DateOnly(2026, 1, 1)), "Found nullable DateOnly");
84+
Assert.IsTrue(all.Any(x => x.DateOnlyNullable == null), "Found null DateOnly?");
85+
86+
var cmp1 = new DateOnly(2024, 1, 1);
87+
Assert.AreEqual(1, (await connection.QueryAsync<DateOnlyTestData>(where: x => x.DateOnly == cmp1, transaction: t)).Count());
88+
Assert.IsTrue((await connection.QueryAsync<DateOnlyTestData>(where: x => x.DateOnlyNullable == new DateOnly(2026, 1, 1), transaction: t)).Count() == 1);
89+
}
90+
91+
92+
93+
public class DateOnlyTestData
94+
{
95+
public int ID { get; set; }
96+
public DateOnly DateOnly { get; set; }
97+
public DateOnly? DateOnlyNullable { get; set; }
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)