8
8
//------------------------------------------------------------------------------
9
9
10
10
11
- using System ;
12
11
using System . Collections ;
13
12
using NHibernate . Cfg ;
14
13
using NHibernate . DomainModel ;
15
14
using NUnit . Framework ;
16
- using Environment = NHibernate . Cfg . Environment ;
15
+ using Environment = NHibernate . Cfg . Environment ;
17
16
18
17
namespace NHibernate . Test . CacheTest
19
18
{
20
19
using System . Threading . Tasks ;
21
20
[ TestFixture ]
22
21
public class QueryCacheFixtureAsync : TestCase
23
22
{
24
- protected override IList Mappings
23
+ protected override IList Mappings => new [ ] { "Simple.hbm.xml" } ;
24
+
25
+ protected override void Configure ( Configuration configuration )
25
26
{
26
- get { return new string [ ] { "Simple.hbm.xml" } ; }
27
+ configuration . SetProperty ( Environment . UseQueryCache , "true" ) ;
27
28
}
28
29
29
- protected override void Configure ( Configuration cfg )
30
+ protected override void OnSetUp ( )
30
31
{
31
- cfg . SetProperty ( Environment . UseQueryCache , "true" ) ;
32
+ using ( var s = OpenSession ( ) )
33
+ {
34
+ s . Save ( new Simple ( ) , 1L ) ;
35
+ s . Flush ( ) ;
36
+ }
32
37
}
33
38
34
- [ Test ]
35
- public async Task QueryCacheWithNullParametersAsync ( )
39
+ protected override void OnTearDown ( )
36
40
{
37
- Simple simple = new Simple ( ) ;
38
-
39
- using ( ISession s = OpenSession ( ) )
41
+ using ( var s = OpenSession ( ) )
40
42
{
41
- await ( s . SaveAsync ( simple , 1L ) ) ;
42
- await ( s . FlushAsync ( ) ) ;
43
+ s . Delete ( "from Simple" ) ;
44
+ s . Flush ( ) ;
43
45
}
46
+ }
44
47
45
- using ( ISession s = OpenSession ( ) )
48
+ [ Test ]
49
+ public async Task QueryCacheWithNullParametersAsync ( )
50
+ {
51
+ using ( var s = OpenSession ( ) )
46
52
{
53
+ const string query = "from Simple s where s = :s or s.Name = :name or s.Address = :address" ;
47
54
await ( s
48
- . CreateQuery ( "from Simple s where s = :s or s.Name = :name or s.Address = :address" )
55
+ . CreateQuery ( query )
49
56
. SetEntity ( "s" , await ( s . LoadAsync ( typeof ( Simple ) , 1L ) ) )
50
57
. SetString ( "name" , null )
51
58
. SetString ( "address" , null )
52
59
. SetCacheable ( true )
53
60
. UniqueResultAsync ( ) ) ;
54
61
55
62
// Run a second time, just to test the query cache
56
- object result = await ( s
57
- . CreateQuery ( "from Simple s where s = :s or s.Name = :name or s.Address = :address" )
63
+ var result = await ( s
64
+ . CreateQuery ( query )
58
65
. SetEntity ( "s" , await ( s . LoadAsync ( typeof ( Simple ) , 1L ) ) )
59
66
. SetString ( "name" , null )
60
67
. SetString ( "address" , null )
61
68
. SetCacheable ( true )
62
69
. UniqueResultAsync ( ) ) ;
63
70
64
- Assert . IsNotNull ( result ) ;
65
- Assert . AreEqual ( 1L , ( long ) s . GetIdentifier ( result ) ) ;
71
+ Assert . That ( result , Is . Not . Null ) ;
72
+ Assert . That ( s . GetIdentifier ( result ) , Is . EqualTo ( 1 ) ) ;
66
73
}
74
+ }
67
75
68
- using ( ISession s = OpenSession ( ) )
76
+ [ Test ]
77
+ public async Task QueryCacheWithScalarReturnAsync ( )
78
+ {
79
+ // Using decimal because:
80
+ // - int is yielded back as decimal by Oracle, wrecking the cast back to int.
81
+ // - Oracle requires a cast to binary_double for yielding a double instead of decimal.
82
+ // - double is not castable in MySql.
83
+ // So long for SQLite which does not have a true decimal type.
84
+ if ( TestDialect . HasBrokenDecimalType )
85
+ Assert . Ignore ( "Database does not support properly decimals." ) ;
86
+
87
+ using ( var s = OpenSession ( ) )
69
88
{
70
- await ( s . DeleteAsync ( "from Simple" ) ) ;
71
- await ( s . FlushAsync ( ) ) ;
89
+ var result = await ( s
90
+ . CreateSQLQuery ( "select cast(200012 as decimal) from Simple where id_ = 1" )
91
+ . SetCacheable ( true )
92
+ . UniqueResultAsync < decimal > ( ) ) ;
93
+
94
+ Assert . That ( result , Is . EqualTo ( 200012 ) , "Unexpected non-cached result" ) ;
95
+
96
+ result = await ( s
97
+ . CreateSQLQuery ( "select cast(200012 as decimal) from Simple where id_ = 1" )
98
+ . SetCacheable ( true )
99
+ . UniqueResultAsync < decimal > ( ) ) ;
100
+
101
+ Assert . That ( result , Is . EqualTo ( 200012 ) , "Unexpected cached result" ) ;
72
102
}
73
103
}
74
104
}
75
- }
105
+ }
0 commit comments