Skip to content

Commit f513f5d

Browse files
committed
DateTime/DateOnly/TimeOnly ctor usage within LINQ tests
1 parent 7ee3521 commit f513f5d

6 files changed

Lines changed: 234 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2023 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
#if NET6_0_OR_GREATER //DO_DATEONLY
6+
7+
using System;
8+
using System.Linq;
9+
using NUnit.Framework;
10+
using Xtensive.Core;
11+
using Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.Model;
12+
13+
namespace Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.DateOnlys
14+
{
15+
public class ConstructorTest : DateTimeBaseTest
16+
{
17+
[Test]
18+
public void CtorYMD()
19+
{
20+
ExecuteInsideSession((s) => {
21+
var result = s.Query.All<AllPossiblePartsEntity>()
22+
.Select(e => new { Entity = e, ConstructedDate = new DateOnly(e.Year, e.Month, e.Day) })
23+
.Where(a => a.ConstructedDate == FirstDateOnly).OrderBy(a => a.Entity.Id).ToList(3);
24+
Assert.That(result.Count, Is.EqualTo(1));
25+
});
26+
}
27+
}
28+
}
29+
#endif
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2023 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Linq;
7+
using NUnit.Framework;
8+
using Xtensive.Core;
9+
using Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.Model;
10+
11+
namespace Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.DateTimes
12+
{
13+
public class ConstructorTest : DateTimeBaseTest
14+
{
15+
[Test]
16+
public void CtorYMDHMSM()
17+
{
18+
ExecuteInsideSession((s) => {
19+
var result = s.Query.All<AllPossiblePartsEntity>()
20+
.Select(e => new {
21+
Entity = e,
22+
ConstructedDate = new DateTime(e.Year, e.Month, e.Day, e.Hour, e.Minute, e.Second, e.Millisecond) })
23+
.Where(a => a.ConstructedDate == FirstMillisecondDateTime).OrderBy(a => a.Entity.Id).ToList(3);
24+
Assert.That(result.Count, Is.EqualTo(1));
25+
});
26+
}
27+
28+
[Test]
29+
public void CtorYMDHMS()
30+
{
31+
ExecuteInsideSession((s) => {
32+
var result = s.Query.All<AllPossiblePartsEntity>()
33+
.Select(e => new {
34+
Entity = e,
35+
ConstructedDate = new DateTime(e.Year, e.Month, e.Day, e.Hour, e.Minute, e.Second)
36+
})
37+
.Where(a => a.ConstructedDate == FirstDateTime).OrderBy(a => a.Entity.Id).ToList(3);
38+
Assert.That(result.Count, Is.EqualTo(1));
39+
});
40+
}
41+
42+
[Test]
43+
public void CtorYMD()
44+
{
45+
ExecuteInsideSession((s) => {
46+
var result = s.Query.All<AllPossiblePartsEntity>()
47+
.Select(e => new { Entity = e, ConstructedDate = new DateTime(e.Year, e.Month, e.Day) })
48+
.Where(a => a.ConstructedDate == FirstDateTime.Date).OrderBy(a => a.Entity.Id).ToList(3);
49+
Assert.That(result.Count, Is.EqualTo(1));
50+
});
51+
}
52+
}
53+
}

Orm/Xtensive.Orm.Tests/Linq/DateTimeAndDateTimeOffset/DateTimeBaseTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected override void RegisterTypes(DomainConfiguration configuration)
4747
configuration.Types.Register(typeof(DateTimeEntity));
4848
configuration.Types.Register(typeof(MillisecondDateTimeEntity));
4949
configuration.Types.Register(typeof(NullableDateTimeEntity));
50+
configuration.Types.Register(typeof(AllPossiblePartsEntity));
5051
#if NET6_0_OR_GREATER
5152
configuration.Types.Register(typeof(DateOnlyEntity));
5253
configuration.Types.Register(typeof(SingleDateOnlyEntity));
@@ -202,6 +203,8 @@ protected override void PopulateEntities(Session session)
202203

203204
_ = new NullableDateTimeEntity(session) { DateTime = null };
204205
_ = new NullableDateTimeEntity(session) { DateTime = null };
206+
207+
_ = AllPossiblePartsEntity.FromDateTime(session, FirstMillisecondDateTime, 321);
205208
}
206209
}
207210
}

Orm/Xtensive.Orm.Tests/Linq/DateTimeAndDateTimeOffset/Model.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Created: 2016.08.01
66

77
using System;
8+
using System.Net.Sockets;
9+
using Org.BouncyCastle.Crypto.Digests;
810

911
namespace Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.Model
1012
{
@@ -153,6 +155,95 @@ public NullableDateTimeOffsetEntity(DateTimeOffsetEntity dateTimeOffsetEntity)
153155
}
154156

155157

158+
[HierarchyRoot]
159+
public class AllPossiblePartsEntity : Entity
160+
{
161+
[Field, Key]
162+
public long Id { get; private set; }
163+
164+
[Field]
165+
[Validation.RangeConstraint(Min = 0, Max = 3000)]
166+
public int Year { get; set; }
167+
168+
[Field]
169+
[Validation.RangeConstraint(Min = 1, Max = 12)]
170+
public int Month { get; set; }
171+
172+
[Field]
173+
[Validation.RangeConstraint(Min = 1, Max = 31)]
174+
public int Day { get; set; }
175+
176+
[Field]
177+
[Validation.RangeConstraint(Min = 0, Max = 23)]
178+
public int Hour { get; set; }
179+
180+
[Field]
181+
[Validation.RangeConstraint(Min = 0, Max = 59)]
182+
public int Minute { get; set; }
183+
184+
[Field]
185+
[Validation.RangeConstraint(Min = 0, Max = 59)]
186+
public int Second { get; set; }
187+
188+
[Field]
189+
[Validation.RangeConstraint(Min = 0, Max = 999)]
190+
public int Millisecond { get; set; }
191+
192+
[Field]
193+
[Validation.RangeConstraint(Min = 0, Max = 999)]
194+
public int Microsecond { get; set; }
195+
196+
[Field]
197+
public long Ticks { get; set; }
198+
199+
[Field]
200+
[Validation.RangeConstraint(Min = -23, Max = 23)]
201+
public int OffsetHour { get; set; }
202+
203+
[Field]
204+
[Validation.RangeConstraint(Min = 0, Max = 59)]
205+
public int OffsetMinute { get; set; }
206+
207+
public static AllPossiblePartsEntity FromDateTime(Session session, DateTime dateTime, int microsecond)
208+
{
209+
return new AllPossiblePartsEntity(session) {
210+
Year = dateTime.Year,
211+
Month = dateTime.Month,
212+
Day = dateTime.Day,
213+
Hour = dateTime.Hour,
214+
Minute = dateTime.Minute,
215+
Second = dateTime.Second,
216+
Millisecond = dateTime.Millisecond,
217+
Microsecond = microsecond,
218+
OffsetHour = 0,
219+
OffsetMinute = 0,
220+
Ticks = dateTime.Ticks
221+
};
222+
}
223+
224+
public static AllPossiblePartsEntity FromDateTimeOffset(Session session, DateTimeOffset dateTimeOffset, int microsecond)
225+
{
226+
return new AllPossiblePartsEntity(session) {
227+
Year = dateTimeOffset.Year,
228+
Month = dateTimeOffset.Month,
229+
Day = dateTimeOffset.Day,
230+
Hour = dateTimeOffset.Hour,
231+
Minute = dateTimeOffset.Minute,
232+
Second = dateTimeOffset.Second,
233+
Millisecond = dateTimeOffset.Millisecond,
234+
Microsecond = microsecond,
235+
OffsetHour = dateTimeOffset.Offset.Hours,
236+
OffsetMinute = dateTimeOffset.Offset.Minutes,
237+
Ticks = dateTimeOffset.Ticks
238+
};
239+
}
240+
241+
private AllPossiblePartsEntity(Session session)
242+
: base(session)
243+
{
244+
}
245+
}
246+
156247
#if NET6_0_OR_GREATER //DO_DATEONLY
157248
[HierarchyRoot]
158249
public class DateOnlyEntity : Entity
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2023 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
#if NET6_0_OR_GREATER //DO_DATEONLY
6+
7+
using System;
8+
using System.Linq;
9+
using NUnit.Framework;
10+
using Xtensive.Core;
11+
using Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.Model;
12+
13+
namespace Xtensive.Orm.Tests.Linq.DateTimeAndDateTimeOffset.TimeOnlys
14+
{
15+
public class ConstructorTest : DateTimeBaseTest
16+
{
17+
[Test]
18+
public void CtorHMSM()
19+
{
20+
ExecuteInsideSession((s) => {
21+
var result = s.Query.All<AllPossiblePartsEntity>()
22+
.Select(e => new {
23+
Entity = e,
24+
ConstructedTime = new TimeOnly(e.Hour, e.Minute, e.Second, e.Millisecond) })
25+
.Where(a => a.ConstructedTime == FirstMillisecondTimeOnly).OrderBy(a => a.Entity.Id).ToList(3);
26+
Assert.That(result.Count, Is.EqualTo(1));
27+
});
28+
}
29+
30+
[Test]
31+
public void CtorHMS()
32+
{
33+
ExecuteInsideSession((s) => {
34+
var result = s.Query.All<AllPossiblePartsEntity>()
35+
.Select(e => new {
36+
Entity = e,
37+
ConstructedTime = new TimeOnly(e.Hour, e.Minute, e.Second)
38+
})
39+
.Where(a => a.ConstructedTime == FirstTimeOnly).OrderBy(a => a.Entity.Id).ToList(3);
40+
Assert.That(result.Count, Is.EqualTo(1));
41+
});
42+
}
43+
44+
[Test]
45+
public void CtorHM()
46+
{
47+
ExecuteInsideSession((s) => {
48+
var result = s.Query.All<AllPossiblePartsEntity>()
49+
.Select(e => new { Entity = e, ConstructedTime = new TimeOnly(e.Hour, e.Minute) })
50+
.Where(a => a.ConstructedTime == FirstTimeOnly.Add(TimeSpan.FromSeconds(-5)))
51+
.OrderBy(a => a.Entity.Id).ToList(3);
52+
Assert.That(result.Count, Is.EqualTo(1));
53+
});
54+
}
55+
}
56+
}
57+
#endif

Orm/Xtensive.Orm.Tests/Linq/DateTimeAndDateTimeOffset/TimeOnly/OperationsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void AddMinutesTest()
4040
});
4141
}
4242

43-
[Test]
43+
[Test, Ignore("Compiler's not implemented yet")]
4444
public void AddTimeSpanTest()
4545
{
4646
ExecuteInsideSession((s) => {

0 commit comments

Comments
 (0)