Skip to content

Commit 0de7af4

Browse files
committed
Add CacheableFuture_QueryWithSubQuery test
1 parent 2986d64 commit 0de7af4

File tree

2 files changed

+243
-39
lines changed

2 files changed

+243
-39
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Linq;
12+
using NUnit.Framework;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
16+
namespace NHibernate.Test.NHSpecificTest.NH3864
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class FixtureAsync : BugTestCase
21+
{
22+
protected override void OnSetUp()
23+
{
24+
Clear2ndLevelCache();
25+
using (var session = OpenSession())
26+
using (var transaction = session.BeginTransaction())
27+
{
28+
var p1 = new Person() { Name = "A" };
29+
var p1c1 = new Person() { Name = "AA" };
30+
var p1c2 = new Person() { Name = "AB" };
31+
var p1c3 = new Person() { Name = "AC" };
32+
p1.Children = new HashSet<Person>(new[] { p1c1, p1c2, p1c3 });
33+
session.Save(p1);
34+
35+
var p2 = new Person() { Name = "B" };
36+
var p2c1 = new Person() { Name = "BA" };
37+
var p2c2 = new Person() { Name = "BB" };
38+
var p2c3 = new Person() { Name = "BC" };
39+
p2.Children = new HashSet<Person>(new[] { p2c1, p2c2, p2c3 });
40+
session.Save(p2);
41+
42+
transaction.Commit();
43+
}
44+
}
45+
46+
protected override void OnTearDown()
47+
{
48+
using (var session = OpenSession())
49+
using (var transaction = session.BeginTransaction())
50+
{
51+
session.Delete("from Person");
52+
transaction.Commit();
53+
}
54+
}
55+
56+
[Test]
57+
public async Task CacheableMulticriteria_QueryOverWithAliasedJoinQueryOverAsync()
58+
{
59+
foreach (var checkCache in new[] { false, true })
60+
{
61+
using (var sqlLogSpy = new SqlLogSpy())
62+
{
63+
using (var session = Sfi.OpenSession())
64+
{
65+
var query = CreateQueryOverWithAliasedJoinQueryOver(session);
66+
67+
var multiCriteria = session.CreateMultiCriteria();
68+
multiCriteria.Add("myQuery", query);
69+
multiCriteria.SetCacheable(true);
70+
71+
var list = (IList<Person>)await (multiCriteria.GetResultAsync("myQuery"));
72+
AssertQueryResult(list);
73+
}
74+
75+
if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog()))
76+
{
77+
Assert.Fail("SQL executed. 2nd level cache should be used instead.");
78+
}
79+
}
80+
}
81+
}
82+
83+
[Test]
84+
public async Task CacheableMulticriteria_QueryOverWithJoinAliasAsync()
85+
{
86+
foreach (var checkCache in new[] { false, true })
87+
{
88+
using (var sqlLogSpy = new SqlLogSpy())
89+
{
90+
using (var s = Sfi.OpenSession())
91+
{
92+
var query = CreateQueryOverWithJoinAlias(s);
93+
94+
var multiCriteria = s.CreateMultiCriteria();
95+
multiCriteria.Add("myQuery", query);
96+
multiCriteria.SetCacheable(true);
97+
98+
var list = (IList<Person>)await (multiCriteria.GetResultAsync("myQuery"));
99+
AssertQueryResult(list);
100+
}
101+
if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog()))
102+
{
103+
Assert.Fail("SQL executed. 2nd level cache should be used instead.");
104+
}
105+
}
106+
}
107+
}
108+
109+
private static void AssertQueryResult(IList<Person> list)
110+
{
111+
Assert.AreEqual(6, list.Count, "Returned records count is wrong.");
112+
var person1 = list.FirstOrDefault(p => p.Name == "A");
113+
Assert.NotNull(person1);
114+
var person2 = list.FirstOrDefault(p => p.Name == "B");
115+
Assert.NotNull(person2);
116+
117+
CollectionAssert.AreEquivalent(person1.Children.Select(c => c.Name), new[] { "AA", "AB", "AC" });
118+
CollectionAssert.AreEquivalent(person2.Children.Select(c => c.Name), new[] { "BA", "BB", "BC" });
119+
}
120+
121+
private static IFutureEnumerable<Person> CreateCacheableQueryWithSubquery(ISession session)
122+
{
123+
var subQuery = session.Query<Person>()
124+
.WithOptions(p => p.SetCacheable(true));
125+
126+
var query = session.Query<Person>()
127+
.FetchMany(p => p.Children)
128+
.Where(p => subQuery.Contains(p) && p.Parent == null)
129+
.WithOptions(o => o.SetCacheable(true))
130+
.ToFuture();
131+
132+
return query;
133+
}
134+
135+
private static IQueryOver<Person, Person> CreateQueryOverWithJoinAlias(ISession session)
136+
{
137+
Person childAlias = null;
138+
return session.QueryOver<Person>()
139+
.Where(p => p.Parent == null)
140+
.JoinAlias(x => x.Children, () => childAlias);
141+
}
142+
143+
private static IQueryOver<Person, Person> CreateQueryOverWithAliasedJoinQueryOver(ISession session)
144+
{
145+
Person childAlias = null;
146+
return session.QueryOver<Person>()
147+
.Where(p => p.Parent == null)
148+
.JoinQueryOver(x => x.Children, () => childAlias);
149+
}
150+
151+
private void Clear2ndLevelCache()
152+
{
153+
Sfi.EvictQueries();
154+
Sfi.Evict(typeof(Person));
155+
}
156+
}
157+
}
Lines changed: 86 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using NHibernate.Linq;
12
using NUnit.Framework;
23
using System.Collections.Generic;
34
using 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

Comments
 (0)