Skip to content

Commit 312f8d0

Browse files
committed
Test for DateTime and TimeOnly values with microseconds
1 parent 2a3f91d commit 312f8d0

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.Orm.Configuration;
9+
using Xtensive.Orm.Tests.Storage.DateTimeStoragePrecisionTestModel;
10+
11+
namespace Xtensive.Orm.Tests.Storage.DateTimeStoragePrecisionTestModel
12+
{
13+
[HierarchyRoot]
14+
[KeyGenerator(KeyGeneratorKind.None)]
15+
public class TestEntity : Entity
16+
{
17+
[Field, Key]
18+
public long Id { get; private set; }
19+
20+
[Field]
21+
public DateTime FDateTime { get; set; }
22+
#if NET6_0_OR_GREATER //DO_DATEONLY
23+
24+
[Field]
25+
public TimeOnly FTimeOnly { get; set; }
26+
#endif
27+
28+
public TestEntity(Session session, long idValue)
29+
: base(session, idValue)
30+
{
31+
var dateTime = new DateTime(idValue, DateTimeKind.Utc);
32+
FDateTime = dateTime;
33+
#if NET6_0_OR_GREATER
34+
FTimeOnly = TimeOnly.FromDateTime(dateTime);
35+
#endif
36+
}
37+
}
38+
}
39+
40+
namespace Xtensive.Orm.Tests.Storage
41+
{
42+
// Each storage is capable of storing certain amount of fractions of second.
43+
// This test writes a value with 7 fractional points to storage and,
44+
// depeding on storage, expects value of precision the storage have.
45+
public class DateTimeStoragePrecisionTest : AutoBuildTest
46+
{
47+
private const long TestDateTimeTicks = 638130658792224229L; //2023-02-27 03:37:59.2224229 UTC
48+
49+
protected override DomainConfiguration BuildConfiguration()
50+
{
51+
var configuration = DomainConfigurationFactory.Create();
52+
configuration.UpgradeMode = DomainUpgradeMode.Recreate;
53+
configuration.Types.Register(typeof(TestEntity));
54+
return configuration;
55+
}
56+
57+
protected override void PopulateData()
58+
{
59+
using (var session = Domain.OpenSession())
60+
using (var tx = session.OpenTransaction()) {
61+
_ = new TestEntity(session, TestDateTimeTicks);
62+
tx.Complete();
63+
}
64+
}
65+
66+
[Test]
67+
public void DateTimeTest()
68+
{
69+
var dateTime = new DateTime(TestDateTimeTicks, DateTimeKind.Utc);
70+
71+
using (var session = Domain.OpenSession())
72+
using (var tx = session.OpenTransaction()) {
73+
var entity = session.Query.All<TestEntity>().First(e => e.Id == TestDateTimeTicks);
74+
Assert.That(entity.FDateTime, Is.EqualTo(GetExpectedValue(dateTime)));
75+
}
76+
}
77+
78+
#if NET6_0_OR_GREATER //DO_DATEONLY
79+
[Test]
80+
public void TimeOnlyTest()
81+
{
82+
var dateTime = new DateTime(TestDateTimeTicks, DateTimeKind.Utc);
83+
var timeOnly = TimeOnly.FromDateTime(dateTime);
84+
85+
using (var session = Domain.OpenSession())
86+
using (var tx = session.OpenTransaction()) {
87+
var entity = session.Query.All<TestEntity>().First(e => e.Id == TestDateTimeTicks);
88+
Assert.That(entity.FTimeOnly, Is.EqualTo(GetExpectedValue(timeOnly)));
89+
}
90+
}
91+
92+
private static TimeOnly GetExpectedValue(in TimeOnly baseTimeOnly) => baseTimeOnly.FixTimeOnlyForProvider(StorageProviderInfo.Instance);
93+
94+
#endif
95+
private static DateTime GetExpectedValue(in DateTime baseDateTime) => baseDateTime.FixDateTimeForProvider(StorageProviderInfo.Instance);
96+
}
97+
}

0 commit comments

Comments
 (0)