Skip to content

Commit c06e510

Browse files
NH-3919 - More timestamp tests
1 parent 53ad3bb commit c06e510

File tree

4 files changed

+129
-27
lines changed

4 files changed

+129
-27
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace NHibernate.Test.TypesTest
4+
{
5+
public class TimestampClass
6+
{
7+
public int Id { get; set; }
8+
public DateTime Value { get; set; }
9+
public DateTime Revision { get; protected set; }
10+
public DateTime? NullableValue { get; set; }
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
4+
<class
5+
name="NHibernate.Test.TypesTest.TimestampClass, NHibernate.Test"
6+
table="bc_timestamp">
7+
<id name="Id" column="id">
8+
<generator class="assigned"/>
9+
</id>
10+
<version name="Revision" column="`Revision`" type="Timestamp"/>
11+
<property name="Value" column="`Value`" type="Timestamp"/>
12+
<property name="NullableValue" type="Timestamp"/>
13+
</class>
14+
</hibernate-mapping>

src/NHibernate.Test/TypesTest/TimestampTypeFixture.cs

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,111 @@
44

55
namespace NHibernate.Test.TypesTest
66
{
7-
/// <summary>
8-
/// Summary description for TimestampTypeFixture.
9-
/// </summary>
107
[TestFixture]
11-
public class TimestampTypeFixture
8+
public class TimestampTypeFixture : TypeFixtureBase
129
{
10+
protected override string TypeName => "Timestamp";
11+
private readonly TimestampType _type = NHibernateUtil.Timestamp;
12+
13+
protected override void OnTearDown()
14+
{
15+
base.OnTearDown();
16+
17+
using (var s = OpenSession())
18+
using (var t = s.BeginTransaction())
19+
{
20+
s.CreateQuery("delete from TimestampClass").ExecuteUpdate();
21+
t.Commit();
22+
}
23+
}
24+
1325
[Test]
1426
public void Next()
1527
{
16-
TimestampType type = (TimestampType) NHibernateUtil.Timestamp;
17-
object current = DateTime.Parse("2004-01-01");
18-
object next = type.Next(current, null);
28+
var current = DateTime.Parse("2004-01-01");
29+
var next = _type.Next(current, null);
1930

20-
Assert.IsTrue(next is DateTime, "Next should be DateTime");
21-
Assert.IsTrue((DateTime) next > (DateTime) current,
22-
"next should be greater than current (could be equal depending on how quickly this occurs)");
31+
Assert.That(next, Is.TypeOf<DateTime>(), "Next should be DateTime");
32+
Assert.That(
33+
next,
34+
Is.GreaterThan(current),
35+
"next should be greater than current");
2336
}
2437

2538
[Test]
2639
public void Seed()
2740
{
28-
TimestampType type = (TimestampType) NHibernateUtil.Timestamp;
29-
Assert.IsTrue(type.Seed(null) is DateTime, "seed should be DateTime");
41+
Assert.That(_type.Seed(null), Is.TypeOf<DateTime>(), "seed should be DateTime");
42+
}
43+
44+
[Test]
45+
[TestCase(DateTimeKind.Unspecified)]
46+
[TestCase(DateTimeKind.Local)]
47+
[TestCase(DateTimeKind.Utc)]
48+
public void ReadWrite(DateTimeKind kind)
49+
{
50+
var entity = new TimestampClass
51+
{
52+
Id = 1,
53+
Value = RoundForDialect(
54+
kind == DateTimeKind.Utc ? DateTime.UtcNow : DateTime.SpecifyKind(DateTime.Now, kind))
55+
// Take another date than now for checking the value do not get overridden by seeding.
56+
.AddDays(1)
57+
};
58+
59+
DateTime beforeNow, afterNow;
60+
61+
// Save
62+
using (var s = OpenSession())
63+
using (var t = s.BeginTransaction())
64+
{
65+
// Account db accuracy
66+
beforeNow = DateTime.Now.AddTicks(-Dialect.TimestampResolutionInTicks);
67+
s.Save(entity);
68+
t.Commit();
69+
afterNow = DateTime.Now.AddTicks(Dialect.TimestampResolutionInTicks);
70+
}
71+
72+
Assert.That(entity.Revision, Is.GreaterThan(beforeNow).And.LessThan(afterNow), "Revision not correctly seeded.");
73+
Assert.That(entity.NullableValue, Is.Null, "NullableValue unexpectedly seeded.");
74+
75+
// Retrieve, compare then update
76+
TimestampClass retrieved;
77+
using (var s = OpenSession())
78+
using (var t = s.BeginTransaction())
79+
{
80+
retrieved = s.Get<TimestampClass>(entity.Id);
81+
82+
Assert.That(retrieved, Is.Not.Null, "Entity not saved or cannot be retrieved by its key.");
83+
Assert.That(retrieved.Value, Is.EqualTo(entity.Value), "Value should be the same.");
84+
Assert.That(retrieved.Revision, Is.EqualTo(entity.Revision), "Revision should be the same.");
85+
Assert.That(retrieved.NullableValue, Is.EqualTo(entity.NullableValue), "NullableValue should be the same.");
86+
87+
retrieved.NullableValue = retrieved.Value;
88+
retrieved.Value = retrieved.Value.AddMonths(-1);
89+
90+
beforeNow = DateTime.Now.AddTicks(-Dialect.TimestampResolutionInTicks);
91+
t.Commit();
92+
afterNow = DateTime.Now.AddTicks(Dialect.TimestampResolutionInTicks);
93+
}
94+
95+
Assert.That(
96+
retrieved.Revision,
97+
Is.GreaterThan(beforeNow).And.LessThan(afterNow).And.GreaterThanOrEqualTo(entity.Revision),
98+
"Revision not correctly incremented.");
99+
100+
// Retrieve and compare again
101+
using (var s = OpenSession())
102+
using (var t = s.BeginTransaction())
103+
{
104+
var retrievedAgain = s.Get<TimestampClass>(entity.Id);
105+
106+
Assert.That(retrievedAgain, Is.Not.Null, "Entity deleted or cannot be retrieved again by its key.");
107+
Assert.That(retrievedAgain.Value, Is.EqualTo(retrieved.Value), "Value should be the same again.");
108+
Assert.That(retrievedAgain.Revision, Is.EqualTo(retrieved.Revision), "Revision should be the same again.");
109+
Assert.That(retrievedAgain.NullableValue, Is.EqualTo(retrieved.NullableValue), "NullableValue should be the same again.");
110+
t.Commit();
111+
}
30112
}
31113
}
32-
}
114+
}

src/NHibernate.Test/TypesTest/TimestampUtcTypeFixture.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ public class TimestampUtcTypeFixture : TypeFixtureBase
1414
readonly DateTime _utc = new DateTime(1976, 11, 30, 10, 0, 0, 300, DateTimeKind.Utc);
1515
readonly DateTime _local = new DateTime(1976, 11, 30, 10, 0, 0, 300, DateTimeKind.Local);
1616
readonly DateTime _unspecified = new DateTime(1976, 11, 30, 10, 0, 0, 300, DateTimeKind.Unspecified);
17-
18-
/// <summary>
19-
/// 1976-11-30T10:00:00.3000000
20-
/// </summary>
21-
const long DateInTicks = 623537928003000000;
2217

2318
protected override string TypeName => "TimestampUtc";
2419

2520
[Test]
2621
public void Next()
2722
{
2823
var current = DateTime.Parse("2004-01-01");
29-
var next = (DateTime)_type.Next(current, null);
24+
var next = (DateTime) _type.Next(current, null);
3025

31-
Assert.AreEqual(DateTimeKind.Utc, next.Kind, "Kind is not Utc");
32-
Assert.IsTrue(next > current, "next should be greater than current (could be equal depending on how quickly this occurs)");
26+
Assert.That(next.Kind, Is.EqualTo(DateTimeKind.Utc), "Kind is not Utc");
27+
Assert.That(next, Is.GreaterThan(current), "next should be greater than current");
3328
}
3429

3530
/// <summary>
@@ -38,11 +33,10 @@ public void Next()
3833
[Test]
3934
public void Seed()
4035
{
41-
var type = NHibernateUtil.TimestampUtc;
42-
Assert.IsTrue(type.Seed(null) is DateTime, "Seed should be DateTime");
36+
Assert.That(_type.Seed(null), Is.TypeOf<DateTime>(), "Seed should be DateTime");
4337

44-
var value = (DateTime)type.Seed(null);
45-
Assert.AreEqual(DateTimeKind.Utc, value.Kind, "Kind should be Utc");
38+
var value = (DateTime) _type.Seed(null);
39+
Assert.That(value.Kind, Is.EqualTo(DateTimeKind.Utc), "Kind should be Utc");
4640
}
4741

4842
/// <summary>
@@ -83,8 +77,8 @@ public void UtcReadWrite_Success()
8377
using(var tx = session.BeginTransaction())
8478
{
8579
// Create a new datetime value and round it to the precision that the database supports. This
86-
// code basically the same as in the implementation but here to guard posible changes.
87-
var resolution = session.GetSessionImplementation().Factory.Dialect.TimestampResolutionInTicks;
80+
// code basically the same as in the implementation but here to guard possible changes.
81+
var resolution = Dialect.TimestampResolutionInTicks;
8882
var next = DateTime.UtcNow;
8983
next = next.AddTicks(-(next.Ticks % resolution));
9084

0 commit comments

Comments
 (0)