4
4
5
5
namespace NHibernate . Test . TypesTest
6
6
{
7
- /// <summary>
8
- /// Summary description for TimestampTypeFixture.
9
- /// </summary>
10
7
[ TestFixture ]
11
- public class TimestampTypeFixture
8
+ public class TimestampTypeFixture : TypeFixtureBase
12
9
{
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
+
13
25
[ Test ]
14
26
public void Next ( )
15
27
{
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 ) ;
19
30
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" ) ;
23
36
}
24
37
25
38
[ Test ]
26
39
public void Seed ( )
27
40
{
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
+ }
30
112
}
31
113
}
32
- }
114
+ }
0 commit comments