Skip to content

Commit 4c931c7

Browse files
NH-3987 - Re-implement NhQueryable options
1 parent 9fd9d4b commit 4c931c7

19 files changed

+430
-405
lines changed

src/NHibernate.Test/Linq/DynamicQueryTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ public void CanQueryWithDynamicOrderBy()
2020
}
2121

2222
[Test(Description = "NH-3239")]
23-
public void CanCahceDynamicLinq()
23+
public void CanCacheDynamicLinq()
2424
{
2525
//dynamic orderby clause
2626
var users = db.Users
27-
.Cacheable()
28-
.Fetch(x => x.Role)
29-
.OrderBy("RegisteredAt");
27+
.SetOptions(o => o.SetCacheable(true))
28+
.Fetch(x => x.Role)
29+
.OrderBy("RegisteredAt");
3030

31-
users
32-
.ToList();
31+
users.ToList();
3332

3433
using (var log = new SqlLogSpy())
3534
{

src/NHibernate.Test/Linq/QueryCacheableTests.cs

Lines changed: 142 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -5,115 +5,135 @@
55

66
namespace NHibernate.Test.Linq
77
{
8-
[TestFixture]
9-
public class QueryCacheableTests : LinqTestCase
10-
{
11-
protected override void Configure(Configuration cfg)
12-
{
13-
cfg.SetProperty(Environment.UseQueryCache, "true");
14-
cfg.SetProperty(Environment.GenerateStatistics, "true");
15-
base.Configure(cfg);
16-
}
17-
18-
[Test]
19-
public void QueryIsCacheable()
20-
{
21-
Sfi.Statistics.Clear();
22-
Sfi.QueryCache.Clear();
23-
24-
var x = (from c in db.Customers
25-
select c).Cacheable().ToList();
26-
27-
var x2 = (from c in db.Customers
28-
select c).Cacheable().ToList();
29-
30-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1));
31-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
32-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
33-
}
34-
35-
[Test]
36-
public void QueryIsCacheable2()
37-
{
38-
Sfi.Statistics.Clear();
39-
Sfi.QueryCache.Clear();
40-
41-
var x = (from c in db.Customers
42-
select c).Cacheable().ToList();
43-
44-
var x2 = (from c in db.Customers
45-
select c).ToList();
46-
47-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
48-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
49-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0));
50-
}
51-
52-
[Test]
53-
public void QueryIsCacheable3()
54-
{
55-
Sfi.Statistics.Clear();
56-
Sfi.QueryCache.Clear();
57-
58-
var x = (from c in db.Customers.Cacheable()
59-
select c).ToList();
60-
61-
var x2 = (from c in db.Customers
62-
select c).ToList();
63-
64-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
65-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
66-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0));
67-
}
68-
69-
[Test]
70-
public void QueryIsCacheableWithRegion()
71-
{
72-
Sfi.Statistics.Clear();
73-
Sfi.QueryCache.Clear();
74-
75-
var x = (from c in db.Customers
76-
select c).Cacheable().CacheRegion("test").ToList();
77-
78-
var x2 = (from c in db.Customers
79-
select c).Cacheable().CacheRegion("test").ToList();
80-
81-
var x3 = (from c in db.Customers
82-
select c).Cacheable().CacheRegion("other").ToList();
83-
84-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
85-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2));
86-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
87-
}
88-
89-
[Test]
90-
public void CacheableBeforeOtherClauses()
91-
{
92-
Sfi.Statistics.Clear();
93-
Sfi.QueryCache.Clear();
94-
95-
db.Customers.Cacheable().Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
96-
db.Customers.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
97-
98-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
99-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
100-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0));
101-
}
102-
103-
[Test]
104-
public void CacheableRegionBeforeOtherClauses()
105-
{
106-
Sfi.Statistics.Clear();
107-
Sfi.QueryCache.Clear();
108-
109-
db.Customers.Cacheable().CacheRegion("test").Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
110-
db.Customers.Cacheable().CacheRegion("test").Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
111-
db.Customers.Cacheable().CacheRegion("other").Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
112-
113-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
114-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2));
115-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
116-
}
8+
[TestFixture]
9+
public class QueryCacheableTests : LinqTestCase
10+
{
11+
protected override void Configure(Configuration cfg)
12+
{
13+
cfg.SetProperty(Environment.UseQueryCache, "true");
14+
cfg.SetProperty(Environment.GenerateStatistics, "true");
15+
base.Configure(cfg);
16+
}
17+
18+
[Test]
19+
public void QueryIsCacheable()
20+
{
21+
Sfi.Statistics.Clear();
22+
Sfi.QueryCache.Clear();
23+
24+
var x = (from c in db.Customers
25+
select c)
26+
.SetOptions(o => o.SetCacheable(true))
27+
.ToList();
28+
29+
var x2 = (from c in db.Customers
30+
select c)
31+
.SetOptions(o => o.SetCacheable(true))
32+
.ToList();
33+
34+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
35+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
36+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
37+
}
38+
39+
[Test]
40+
public void QueryIsCacheable2()
41+
{
42+
Sfi.Statistics.Clear();
43+
Sfi.QueryCache.Clear();
44+
45+
var x = (from c in db.Customers
46+
select c)
47+
.SetOptions(o => o.SetCacheable(true))
48+
.ToList();
49+
50+
var x2 = (from c in db.Customers
51+
select c).ToList();
52+
53+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
54+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
55+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "Unexpected cache hit count");
56+
}
57+
58+
[Test]
59+
public void QueryIsCacheable3()
60+
{
61+
Sfi.Statistics.Clear();
62+
Sfi.QueryCache.Clear();
63+
64+
var x = (from c in db.Customers.SetOptions(o => o.SetCacheable(true))
65+
select c).ToList();
66+
67+
var x2 = (from c in db.Customers
68+
select c).ToList();
69+
70+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
71+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
72+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "Unexpected cache hit count");
73+
}
74+
75+
[Test]
76+
public void QueryIsCacheableWithRegion()
77+
{
78+
Sfi.Statistics.Clear();
79+
Sfi.QueryCache.Clear();
80+
81+
var x = (from c in db.Customers
82+
select c)
83+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
84+
.ToList();
85+
86+
var x2 = (from c in db.Customers
87+
select c)
88+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
89+
.ToList();
90+
91+
var x3 = (from c in db.Customers
92+
select c)
93+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
94+
.ToList();
95+
96+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
97+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2), "Unexpected cache put count");
98+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
99+
}
100+
101+
[Test]
102+
public void CacheableBeforeOtherClauses()
103+
{
104+
Sfi.Statistics.Clear();
105+
Sfi.QueryCache.Clear();
106+
107+
db.Customers
108+
.SetOptions(o => o.SetCacheable(true))
109+
.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
110+
db.Customers.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
111+
112+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
113+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
114+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "Unexpected cache hit count");
115+
}
116+
117+
[Test]
118+
public void CacheableRegionBeforeOtherClauses()
119+
{
120+
Sfi.Statistics.Clear();
121+
Sfi.QueryCache.Clear();
122+
123+
db.Customers
124+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
125+
.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
126+
db.Customers
127+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
128+
.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
129+
db.Customers
130+
.SetOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
131+
.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
132+
133+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
134+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2), "Unexpected cache put count");
135+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
136+
}
117137

118138
[Test]
119139
public void GroupByQueryIsCacheable()
@@ -124,8 +144,8 @@ public void GroupByQueryIsCacheable()
124144
var c = db
125145
.Customers
126146
.GroupBy(x => x.Address.Country)
127-
.Select(x=>x.Key)
128-
.Cacheable()
147+
.Select(x => x.Key)
148+
.SetOptions(o => o.SetCacheable(true))
129149
.ToList();
130150

131151
c = db
@@ -138,12 +158,12 @@ public void GroupByQueryIsCacheable()
138158
.Customers
139159
.GroupBy(x => x.Address.Country)
140160
.Select(x => x.Key)
141-
.Cacheable()
161+
.SetOptions(o => o.SetCacheable(true))
142162
.ToList();
143163

144-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
145-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
146-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
164+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
165+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
166+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
147167
}
148168

149169
[Test]
@@ -153,7 +173,8 @@ public void GroupByQueryIsCacheable2()
153173
Sfi.QueryCache.Clear();
154174

155175
var c = db
156-
.Customers.Cacheable()
176+
.Customers
177+
.SetOptions(o => o.SetCacheable(true))
157178
.GroupBy(x => x.Address.Country)
158179
.Select(x => x.Key)
159180
.ToList();
@@ -165,14 +186,15 @@ public void GroupByQueryIsCacheable2()
165186
.ToList();
166187

167188
c = db
168-
.Customers.Cacheable()
189+
.Customers
190+
.SetOptions(o => o.SetCacheable(true))
169191
.GroupBy(x => x.Address.Country)
170192
.Select(x => x.Key)
171193
.ToList();
172194

173-
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
174-
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
175-
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
195+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
196+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
197+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
176198
}
177199
}
178200
}

src/NHibernate.Test/Linq/QueryTimeoutTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public void CanSetTimeoutOnLinqQueries()
2323
{
2424
var result = (from e in db.Customers
2525
where e.CompanyName == "Corp"
26-
select e).Timeout(17).ToList();
26+
select e)
27+
.SetOptions(o => o.SetTimeout(17))
28+
.ToList();
2729

2830
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
2931
}
@@ -34,7 +36,10 @@ public void CanSetTimeoutOnLinqPagingQuery()
3436
{
3537
var result = (from e in db.Customers
3638
where e.CompanyName == "Corp"
37-
select e).Skip(5).Take(5).Timeout(17).ToList();
39+
select e)
40+
.Skip(5).Take(5)
41+
.SetOptions(o => o.SetTimeout(17))
42+
.ToList();
3843

3944
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
4045
}
@@ -46,7 +51,9 @@ public void CanSetTimeoutBeforeSkipOnLinqOrderedPageQuery()
4651
var result = (from e in db.Customers
4752
orderby e.CompanyName
4853
select e)
49-
.Timeout(17).Skip(5).Take(5).ToList();
54+
.SetOptions(o => o.SetTimeout(17))
55+
.Skip(5).Take(5)
56+
.ToList();
5057

5158
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
5259
}
@@ -55,16 +62,18 @@ orderby e.CompanyName
5562
[Test]
5663
public void CanSetTimeoutOnLinqGroupPageQuery()
5764
{
58-
var subQuery = db.Customers.Where(e2 => e2.CompanyName.Contains("a")).Select(e2 => e2.CustomerId)
59-
.Timeout(18); // This Timeout() should not cause trouble, and be ignored.
65+
var subQuery = db.Customers
66+
.Where(e2 => e2.CompanyName.Contains("a")).Select(e2 => e2.CustomerId)
67+
.SetOptions(o => o.SetTimeout(18)); // This Timeout() should not cause trouble, and be ignored.
6068

6169
var result = (from e in db.Customers
6270
where subQuery.Contains(e.CustomerId)
6371
group e by e.CompanyName
6472
into g
6573
select new { g.Key, Count = g.Count() })
6674
.Skip(5).Take(5)
67-
.Timeout(17).ToList();
75+
.SetOptions(o => o.SetTimeout(17))
76+
.ToList();
6877

6978
Assert.That(TimeoutCatchingNonBatchingBatcher.LastCommandTimeout, Is.EqualTo(17));
7079
}

0 commit comments

Comments
 (0)