99
1010
1111using 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 ;
1222using NHibernate . Type ;
23+ using NHibernate . Util ;
1324using NUnit . Framework ;
1425
1526namespace NHibernate . Test . TypesTest
@@ -25,20 +36,193 @@ public class DateTimeTypeFixtureAsync
2536 [ Test ]
2637 public async Task NextAsync ( )
2738 {
28- DateTimeType type = ( DateTimeType ) NHibernateUtil . DateTime ;
39+ var type = NHibernateUtil . DateTime ;
2940 object current = DateTime . Parse ( "2004-01-01" ) ;
3041 object next = await ( type . NextAsync ( current , null , CancellationToken . None ) ) ;
3142
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" ) ;
3545 }
3646
3747 [ Test ]
3848 public async Task SeedAsync ( )
3949 {
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" ) ;
4252 }
4353 }
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