|
| 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