1+ using NHibernate . Linq ;
12using NUnit . Framework ;
23using System . Collections . Generic ;
34using System . Linq ;
@@ -10,61 +11,70 @@ public class Fixture : BugTestCase
1011 protected override void OnSetUp ( )
1112 {
1213 Clear2ndLevelCache ( ) ;
13- using ( ISession s = OpenSession ( ) )
14- using ( ITransaction tx = s . BeginTransaction ( ) )
14+ using ( var session = OpenSession ( ) )
15+ using ( var transaction = session . BeginTransaction ( ) )
1516 {
1617 var p1 = new Person ( ) { Name = "A" } ;
1718 var p1c1 = new Person ( ) { Name = "AA" } ;
1819 var p1c2 = new Person ( ) { Name = "AB" } ;
1920 var p1c3 = new Person ( ) { Name = "AC" } ;
2021 p1 . Children = new HashSet < Person > ( new [ ] { p1c1 , p1c2 , p1c3 } ) ;
21- s . Save ( p1 ) ;
22+ session . Save ( p1 ) ;
2223
2324 var p2 = new Person ( ) { Name = "B" } ;
2425 var p2c1 = new Person ( ) { Name = "BA" } ;
2526 var p2c2 = new Person ( ) { Name = "BB" } ;
2627 var p2c3 = new Person ( ) { Name = "BC" } ;
2728 p2 . Children = new HashSet < Person > ( new [ ] { p2c1 , p2c2 , p2c3 } ) ;
28- s . Save ( p2 ) ;
29+ session . Save ( p2 ) ;
2930
30- tx . Commit ( ) ;
31+ transaction . Commit ( ) ;
3132 }
3233 }
3334
3435 protected override void OnTearDown ( )
3536 {
36- base . OnTearDown ( ) ;
37- using ( ISession s = OpenSession ( ) )
38- using ( ITransaction tx = s . BeginTransaction ( ) )
37+ using ( var session = OpenSession ( ) )
38+ using ( var transaction = session . BeginTransaction ( ) )
3939 {
40- s . Delete ( "from Person" ) ;
41- tx . Commit ( ) ;
40+ session . Delete ( "from Person" ) ;
41+ transaction . Commit ( ) ;
4242 }
4343 }
4444
4545 [ Test ]
4646 public void CacheableMulticriteria_QueryOverWithAliasedJoinQueryOver ( )
4747 {
48- ExecuteActionTwiceSecondRunEnsureNoSqlExecuted ( ( ) =>
48+ foreach ( var checkCache in new [ ] { false , true } )
49+ {
50+ using ( var sqlLogSpy = new SqlLogSpy ( ) )
4951 {
50- using ( var s = Sfi . OpenSession ( ) )
52+ using ( var session = Sfi . OpenSession ( ) )
5153 {
52- var query = CreateQueryOverWithAliasedJoinQueryOver ( s ) ;
54+ var query = CreateQueryOverWithAliasedJoinQueryOver ( session ) ;
5355
54- var multiCriteria = s . CreateMultiCriteria ( ) ;
56+ var multiCriteria = session . CreateMultiCriteria ( ) ;
5557 multiCriteria . Add ( "myQuery" , query ) ;
5658 multiCriteria . SetCacheable ( true ) ;
5759
5860 var list = ( IList < Person > ) multiCriteria . GetResult ( "myQuery" ) ;
5961 AssertQueryResult ( list ) ;
6062 }
61- } ) ;
63+
64+ if ( checkCache && ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
65+ {
66+ Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
67+ }
68+ }
69+ }
6270 }
6371
6472 [ Test ]
6573 public void CacheableFuture_QueryOverWithAliasedJoinQueryOver ( )
6674 {
67- ExecuteActionTwiceSecondRunEnsureNoSqlExecuted ( ( ) =>
75+ foreach ( var checkCache in new [ ] { false , true } )
76+ {
77+ using ( var sqlLogSpy = new SqlLogSpy ( ) )
6878 {
6979 using ( var s = Sfi . OpenSession ( ) )
7080 {
@@ -75,13 +85,20 @@ public void CacheableFuture_QueryOverWithAliasedJoinQueryOver()
7585 var list = query . ToList ( ) ;
7686 AssertQueryResult ( list ) ;
7787 }
78- } ) ;
88+ if ( checkCache && ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
89+ {
90+ Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
91+ }
92+ }
93+ }
7994 }
8095
8196 [ Test ]
8297 public void CacheableMulticriteria_QueryOverWithJoinAlias ( )
8398 {
84- ExecuteActionTwiceSecondRunEnsureNoSqlExecuted ( ( ) =>
99+ foreach ( var checkCache in new [ ] { false , true } )
100+ {
101+ using ( var sqlLogSpy = new SqlLogSpy ( ) )
85102 {
86103 using ( var s = Sfi . OpenSession ( ) )
87104 {
@@ -94,13 +111,20 @@ public void CacheableMulticriteria_QueryOverWithJoinAlias()
94111 var list = ( IList < Person > ) multiCriteria . GetResult ( "myQuery" ) ;
95112 AssertQueryResult ( list ) ;
96113 }
97- } ) ;
114+ if ( checkCache && ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
115+ {
116+ Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
117+ }
118+ }
119+ }
98120 }
99121
100122 [ Test ]
101123 public void CacheableFuture_QueryOverWithJoinAlias ( )
102124 {
103- ExecuteActionTwiceSecondRunEnsureNoSqlExecuted ( ( ) =>
125+ foreach ( var checkCache in new [ ] { false , true } )
126+ {
127+ using ( var sqlLogSpy = new SqlLogSpy ( ) )
104128 {
105129 using ( var s = Sfi . OpenSession ( ) )
106130 {
@@ -111,7 +135,34 @@ public void CacheableFuture_QueryOverWithJoinAlias()
111135 var list = query . ToList ( ) ;
112136 AssertQueryResult ( list ) ;
113137 }
114- } ) ;
138+ if ( checkCache && ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
139+ {
140+ Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
141+ }
142+ }
143+ }
144+ }
145+
146+ [ Test ]
147+ public void CacheableFuture_QueryWithSubQuery ( )
148+ {
149+ foreach ( var checkCache in new [ ] { false , true } )
150+ {
151+ using ( var sqlLogSpy = new SqlLogSpy ( ) )
152+ {
153+ using ( var session = Sfi . OpenSession ( ) )
154+ {
155+ var query = CreateCacheableQueryWithSubquery ( session ) ;
156+
157+ var list = query . ToList ( ) ;
158+ AssertQueryResult ( list ) ;
159+ }
160+ if ( checkCache && ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
161+ {
162+ Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
163+ }
164+ }
165+ }
115166 }
116167
117168 private static void AssertQueryResult ( IList < Person > list )
@@ -126,6 +177,20 @@ private static void AssertQueryResult(IList<Person> list)
126177 CollectionAssert . AreEquivalent ( person2 . Children . Select ( c => c . Name ) , new [ ] { "BA" , "BB" , "BC" } ) ;
127178 }
128179
180+ private static IFutureEnumerable < Person > CreateCacheableQueryWithSubquery ( ISession session )
181+ {
182+ var subQuery = session . Query < Person > ( )
183+ . WithOptions ( p => p . SetCacheable ( true ) ) ;
184+
185+ var query = session . Query < Person > ( )
186+ . FetchMany ( p => p . Children )
187+ . Where ( p => subQuery . Contains ( p ) && p . Parent == null )
188+ . WithOptions ( o => o . SetCacheable ( true ) )
189+ . ToFuture ( ) ;
190+
191+ return query ;
192+ }
193+
129194 private static IQueryOver < Person , Person > CreateQueryOverWithJoinAlias ( ISession session )
130195 {
131196 Person childAlias = null ;
@@ -147,23 +212,5 @@ private void Clear2ndLevelCache()
147212 Sfi . EvictQueries ( ) ;
148213 Sfi . Evict ( typeof ( Person ) ) ;
149214 }
150-
151- private static void ExecuteActionTwiceSecondRunEnsureNoSqlExecuted ( System . Action action )
152- {
153- action ( ) ;
154- EnsureNoSqlExecutedOnDb ( action ) ;
155- }
156-
157- private static void EnsureNoSqlExecutedOnDb ( System . Action action )
158- {
159- using ( var sqlLogSpy = new SqlLogSpy ( ) )
160- {
161- action ( ) ;
162- if ( ! string . IsNullOrEmpty ( sqlLogSpy . GetWholeLog ( ) ) )
163- {
164- Assert . Fail ( "SQL executed. 2nd level cache should be used instead." ) ;
165- }
166- }
167- }
168215 }
169216}
0 commit comments