9
9
10
10
11
11
using System ;
12
+ using System . Collections . Generic ;
13
+ using System . Data ;
14
+ using System . Data . Common ;
15
+ using System . Linq ;
16
+ using NHibernate . Cfg ;
17
+ using NHibernate . Driver ;
18
+ using NHibernate . Engine ;
19
+ using NHibernate . SqlCommand ;
20
+ using NHibernate . SqlTypes ;
21
+ using NHibernate . Tool . hbm2ddl ;
12
22
using NHibernate . Type ;
23
+ using NHibernate . Util ;
13
24
using NUnit . Framework ;
14
25
15
26
namespace NHibernate . Test . TypesTest
@@ -25,20 +36,193 @@ public class DateTimeTypeFixtureAsync
25
36
[ Test ]
26
37
public async Task NextAsync ( )
27
38
{
28
- DateTimeType type = ( DateTimeType ) NHibernateUtil . DateTime ;
39
+ var type = NHibernateUtil . DateTime ;
29
40
object current = DateTime . Parse ( "2004-01-01" ) ;
30
41
object next = await ( type . NextAsync ( current , null , CancellationToken . None ) ) ;
31
42
32
- Assert . IsTrue ( next is DateTime , "Next should be DateTime" ) ;
33
- Assert . IsTrue ( ( DateTime ) next > ( DateTime ) current ,
34
- "next should be greater than current (could be equal depending on how quickly this occurs)" ) ;
43
+ Assert . That ( next , Is . TypeOf < DateTime > ( ) , "next should be DateTime" ) ;
44
+ Assert . That ( next , Is . GreaterThan ( current ) , "next should be greater than current" ) ;
35
45
}
36
46
37
47
[ Test ]
38
48
public async Task SeedAsync ( )
39
49
{
40
- DateTimeType type = ( DateTimeType ) NHibernateUtil . DateTime ;
41
- Assert . IsTrue ( await ( type . SeedAsync ( null , CancellationToken . None ) ) is DateTime , "seed should be DateTime" ) ;
50
+ var type = NHibernateUtil . DateTime ;
51
+ Assert . That ( await ( type . SeedAsync ( null , CancellationToken . None ) ) , Is . TypeOf < DateTime > ( ) , "seed should be DateTime" ) ;
42
52
}
43
53
}
44
- }
54
+
55
+ [ TestFixture ]
56
+ public class DateTimeSqlTypeFixtureAsync : TypeFixtureBase
57
+ {
58
+ protected override string TypeName => "DateTime" ;
59
+ private const int _dateId = 1 ;
60
+
61
+ protected override void Configure ( Configuration configuration )
62
+ {
63
+ base . Configure ( configuration ) ;
64
+
65
+ var driverClass = ReflectHelper . ClassForName ( configuration . GetProperty ( Cfg . Environment . ConnectionDriver ) ) ;
66
+ ClientDriverWithParamsStats . DriverClass = driverClass ;
67
+
68
+ configuration . SetProperty (
69
+ Cfg . Environment . ConnectionDriver ,
70
+ typeof ( ClientDriverWithParamsStats ) . AssemblyQualifiedName ) ;
71
+ }
72
+
73
+ protected override void OnSetUp ( )
74
+ {
75
+ base . OnSetUp ( ) ;
76
+
77
+ using ( var s = OpenSession ( ) )
78
+ using ( var t = s . BeginTransaction ( ) )
79
+ {
80
+ var d = new DateTimeClass
81
+ {
82
+ Id = _dateId ,
83
+ LocalDateTimeValue = DateTime . Now . AddDays ( - 1 ) ,
84
+ UtcDateTimeValue = DateTime . UtcNow . AddDays ( - 1 ) ,
85
+ NormalDateTimeValue = DateTime . Now . AddDays ( - 1 )
86
+ } ;
87
+ s . Save ( d ) ;
88
+ t . Commit ( ) ;
89
+ }
90
+ }
91
+
92
+ protected override void OnTearDown ( )
93
+ {
94
+ base . OnTearDown ( ) ;
95
+
96
+ using ( var s = OpenSession ( ) )
97
+ using ( var t = s . BeginTransaction ( ) )
98
+ {
99
+ s . CreateQuery ( "delete from DateTimeClass" ) . ExecuteUpdate ( ) ;
100
+ t . Commit ( ) ;
101
+ }
102
+ }
103
+
104
+ protected override void DropSchema ( )
105
+ {
106
+ ( Sfi . ConnectionProvider . Driver as ClientDriverWithParamsStats ) ? . CleanUp ( ) ;
107
+ base . DropSchema ( ) ;
108
+ }
109
+
110
+ [ Test ]
111
+ public Task DbHasExpectedTypeAsync ( )
112
+ {
113
+ try
114
+ {
115
+ var validator = new SchemaValidator ( cfg ) ;
116
+ return validator . ValidateAsync ( ) ;
117
+ }
118
+ catch ( Exception ex )
119
+ {
120
+ return Task . FromException < object > ( ex ) ;
121
+ }
122
+ }
123
+
124
+ [ Test ]
125
+ public async Task SaveUseExpectedSqlTypeAsync ( )
126
+ {
127
+ var driver = ( ClientDriverWithParamsStats ) Sfi . ConnectionProvider . Driver ;
128
+
129
+ using ( var s = OpenSession ( ) )
130
+ using ( var t = s . BeginTransaction ( ) )
131
+ {
132
+ var d = new DateTimeClass
133
+ {
134
+ Id = 2 ,
135
+ LocalDateTimeValue = DateTime . Now ,
136
+ UtcDateTimeValue = DateTime . UtcNow ,
137
+ NormalDateTimeValue = DateTime . Now
138
+ } ;
139
+ driver . ClearStats ( ) ;
140
+ await ( s . SaveAsync ( d ) ) ;
141
+ await ( t . CommitAsync ( ) ) ;
142
+ }
143
+
144
+ AssertSqlType ( driver , 3 ) ;
145
+ }
146
+
147
+ [ Test ]
148
+ public async Task UpdateUseExpectedSqlTypeAsync ( )
149
+ {
150
+ var driver = ( ClientDriverWithParamsStats ) Sfi . ConnectionProvider . Driver ;
151
+
152
+ using ( var s = OpenSession ( ) )
153
+ using ( var t = s . BeginTransaction ( ) )
154
+ {
155
+ var d = await ( s . GetAsync < DateTimeClass > ( _dateId ) ) ;
156
+ d . LocalDateTimeValue = DateTime . Now ;
157
+ d . UtcDateTimeValue = DateTime . UtcNow ;
158
+ d . NormalDateTimeValue = DateTime . Now ;
159
+ driver . ClearStats ( ) ;
160
+ await ( t . CommitAsync ( ) ) ;
161
+ }
162
+
163
+ AssertSqlType ( driver , 3 ) ;
164
+ }
165
+
166
+ [ Test ]
167
+ public async Task QueryUseExpectedSqlTypeAsync ( )
168
+ {
169
+ if ( ! TestDialect . SupportsNonDataBoundCondition )
170
+ Assert . Ignore ( "Dialect does not support the test query" ) ;
171
+
172
+ var driver = ( ClientDriverWithParamsStats ) Sfi . ConnectionProvider . Driver ;
173
+
174
+ using ( var s = OpenSession ( ) )
175
+ using ( var t = s . BeginTransaction ( ) )
176
+ {
177
+ var q = s
178
+ . CreateQuery (
179
+ "from DateTimeClass d where d.LocalDateTimeValue = :local and " +
180
+ "d.UtcDateTimeValue = :utc and d.NormalDateTimeValue = :normal and " +
181
+ ":other1 = :other2" )
182
+ . SetDateTime ( "local" , DateTime . Now )
183
+ . SetDateTime ( "utc" , DateTime . UtcNow )
184
+ . SetDateTime ( "normal" , DateTime . Now )
185
+ . SetDateTime ( "other1" , DateTime . Now )
186
+ . SetDateTime ( "other2" , DateTime . Now ) ;
187
+ driver . ClearStats ( ) ;
188
+ await ( q . ListAsync < DateTimeClass > ( ) ) ;
189
+ await ( t . CommitAsync ( ) ) ;
190
+ }
191
+
192
+ AssertSqlType ( driver , 5 ) ;
193
+ }
194
+
195
+ private void AssertSqlType ( ClientDriverWithParamsStats driver , int expectedCount )
196
+ {
197
+ if ( NHibernateUtil . DateTime . SqlTypes ( Sfi ) . Any ( t => Equals ( t , SqlTypeFactory . DateTime2 ) ) )
198
+ {
199
+ Assert . That (
200
+ driver . GetCount ( SqlTypeFactory . DateTime ) ,
201
+ Is . EqualTo ( 0 ) ,
202
+ "Found unexpected SqlTypeFactory.DateTime usages." ) ;
203
+ Assert . That (
204
+ driver . GetCount ( SqlTypeFactory . DateTime2 ) ,
205
+ Is . EqualTo ( expectedCount ) ,
206
+ "Unexpected SqlTypeFactory.DateTime2 usage count." ) ;
207
+ Assert . That ( driver . GetCount ( DbType . DateTime ) , Is . EqualTo ( 0 ) , "Found unexpected DbType.DateTime usages." ) ;
208
+ Assert . That (
209
+ driver . GetCount ( DbType . DateTime2 ) ,
210
+ Is . EqualTo ( expectedCount ) ,
211
+ "Unexpected DbType.DateTime2 usage count." ) ;
212
+ }
213
+ else
214
+ {
215
+ Assert . That (
216
+ driver . GetCount ( SqlTypeFactory . DateTime2 ) ,
217
+ Is . EqualTo ( 0 ) ,
218
+ "Found unexpected SqlTypeFactory.DateTime2 usages." ) ;
219
+ Assert . That (
220
+ driver . GetCount ( SqlTypeFactory . DateTime ) ,
221
+ Is . EqualTo ( expectedCount ) ,
222
+ "Unexpected SqlTypeFactory.DateTime usage count." ) ;
223
+ Assert . That ( driver . GetCount ( DbType . DateTime2 ) , Is . EqualTo ( 0 ) , "Found unexpected DbType.DateTime2 usages." ) ;
224
+ Assert . That ( driver . GetCount ( DbType . DateTime ) , Is . EqualTo ( expectedCount ) , "Unexpected DbType.DateTime usage count." ) ;
225
+ }
226
+ }
227
+ }
228
+ }
0 commit comments