Skip to content

Commit b0a2fd2

Browse files
Fix a regression fromminimizing breaking change
And fix the empty case.
1 parent 594d694 commit b0a2fd2

File tree

11 files changed

+271
-101
lines changed

11 files changed

+271
-101
lines changed

src/NHibernate.Test/Async/QueryTest/MultiCriteriaFixture.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
139139

140140
await (CreateItemsAsync());
141141

142-
await (DoMutiQueryAndAssertAsync());
142+
await (DoMultiQueryAndAssertAsync());
143143

144144
Assert.AreEqual(1, cacheHashtable.Count);
145145
}
@@ -148,20 +148,20 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
148148
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
149149
{
150150
await (CreateItemsAsync());
151-
//set the query in the cache
152-
await (DoMutiQueryAndAssertAsync());
151+
// Set the query in the cache.
152+
await (DoMultiQueryAndAssertAsync());
153153

154154
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
155-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
155+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
156156
// The first element is a timestamp, then only we have the cached data.
157-
var cachedQuery = (IList)cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
157+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
158158

159-
var firstQueryResults = (IList)cachedQuery[0];
159+
var firstQueryResults = (IList) cachedQuery[0];
160160
firstQueryResults.Clear();
161161
firstQueryResults.Add(3);
162162
firstQueryResults.Add(4);
163163

164-
var secondQueryResults = (IList)cachedQuery[1];
164+
var secondQueryResults = (IList) cachedQuery[1];
165165
secondQueryResults[0] = 2;
166166

167167
using (var s = Sfi.OpenSession())
@@ -173,9 +173,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
173173
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
174174
multiCriteria.SetCacheable(true);
175175
var results = await (multiCriteria.ListAsync());
176-
var items = (IList)results[0];
176+
var items = (IList) results[0];
177177
Assert.AreEqual(2, items.Count);
178-
var count = (int)((IList)results[1])[0];
178+
var count = (int) ((IList) results[1])[0];
179179
Assert.AreEqual(2L, count);
180180
}
181181
}
@@ -185,12 +185,12 @@ public async Task CanUpdateStatisticsWhenGetMultiQueryFromSecondLevelCacheAsync(
185185
{
186186
await (CreateItemsAsync());
187187

188-
await (DoMutiQueryAndAssertAsync());
188+
await (DoMultiQueryAndAssertAsync());
189189
Assert.AreEqual(0, Sfi.Statistics.QueryCacheHitCount);
190190
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
191191
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
192192

193-
await (DoMutiQueryAndAssertAsync());
193+
await (DoMultiQueryAndAssertAsync());
194194
Assert.AreEqual(1, Sfi.Statistics.QueryCacheHitCount);
195195
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
196196
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
@@ -391,7 +391,7 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
391391
}
392392
}
393393

394-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
394+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
395395
{
396396
using (var s = OpenSession())
397397
{
@@ -402,9 +402,9 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
402402
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
403403
multiCriteria.SetCacheable(true);
404404
var results = await (multiCriteria.ListAsync(cancellationToken));
405-
var items = (IList)results[0];
405+
var items = (IList) results[0];
406406
Assert.AreEqual(89, items.Count);
407-
var count = (int)((IList)results[1])[0];
407+
var count = (int) ((IList) results[1])[0];
408408
Assert.AreEqual(99L, count);
409409
}
410410
}

src/NHibernate.Test/Async/QueryTest/MultipleMixedQueriesFixture.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
7878
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
7979
{
8080
await (CreateItemsAsync());
81-
//set the query in the cache
82-
await (DoMutiQueryAndAssertAsync());
81+
// Set the query in the cache.
82+
await (DoMultiQueryAndAssertAsync());
8383

8484
var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
85-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
85+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
8686
// The first element is a timestamp, then only we have the cached data.
8787
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
8888

89-
var firstQueryResults = (IList)cachedQuery[0];
89+
var firstQueryResults = (IList) cachedQuery[0];
9090
firstQueryResults.Clear();
9191
firstQueryResults.Add(3);
9292
firstQueryResults.Add(4);
9393

94-
var secondQueryResults = (IList)cachedQuery[1];
94+
var secondQueryResults = (IList) cachedQuery[1];
9595
secondQueryResults[0] = 2L;
9696

9797
using (var s = Sfi.OpenSession())
@@ -104,9 +104,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
104104
.SetInt32(0, 50));
105105
multiQuery.SetCacheable(true);
106106
var results = await (multiQuery.ListAsync());
107-
var items = (IList)results[0];
107+
var items = (IList) results[0];
108108
Assert.AreEqual(2, items.Count);
109-
var count = (long)((IList)results[1])[0];
109+
var count = (long) ((IList) results[1])[0];
110110
Assert.AreEqual(2L, count);
111111
}
112112
}
@@ -186,12 +186,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
186186

187187
await (CreateItemsAsync());
188188

189-
await (DoMutiQueryAndAssertAsync());
189+
await (DoMultiQueryAndAssertAsync());
190190

191191
Assert.AreEqual(1, cacheHashtable.Count);
192192
}
193193

194-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
194+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
195195
{
196196
using (var s = OpenSession())
197197
{
@@ -203,9 +203,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
203203
.SetInt32(0, 50));
204204
multiQuery.SetCacheable(true);
205205
var results = await (multiQuery.ListAsync(cancellationToken));
206-
var items = (IList)results[0];
206+
var items = (IList) results[0];
207207
Assert.AreEqual(89, items.Count);
208-
var count = (long)((IList)results[1])[0];
208+
var count = (long) ((IList) results[1])[0];
209209
Assert.AreEqual(99L, count);
210210
}
211211
}

src/NHibernate.Test/Async/QueryTest/MultipleQueriesFixture.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
8181
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
8282
{
8383
await (CreateItemsAsync());
84-
//set the query in the cache
85-
await (DoMutiQueryAndAssertAsync());
84+
// Set the query in the cache.
85+
await (DoMultiQueryAndAssertAsync());
8686

8787
var cacheHashtable = GetHashTableUsedAsQueryCache(Sfi);
88-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
88+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
8989
// The first element is a timestamp, then only we have the cached data.
9090
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
9191

92-
var firstQueryResults = (IList)cachedQuery[0];
92+
var firstQueryResults = (IList) cachedQuery[0];
9393
firstQueryResults.Clear();
9494
firstQueryResults.Add(3);
9595
firstQueryResults.Add(4);
9696

97-
var secondQueryResults = (IList)cachedQuery[1];
97+
var secondQueryResults = (IList) cachedQuery[1];
9898
secondQueryResults[0] = 2L;
9999

100100
using (var s = Sfi.OpenSession())
@@ -107,9 +107,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
107107
.SetInt32(0, 50));
108108
multiQuery.SetCacheable(true);
109109
var results = await (multiQuery.ListAsync());
110-
var items = (IList)results[0];
110+
var items = (IList) results[0];
111111
Assert.AreEqual(2, items.Count);
112-
var count = (long)((IList)results[1])[0];
112+
var count = (long) ((IList) results[1])[0];
113113
Assert.AreEqual(2L, count);
114114
}
115115
}
@@ -188,12 +188,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
188188

189189
await (CreateItemsAsync());
190190

191-
await (DoMutiQueryAndAssertAsync());
191+
await (DoMultiQueryAndAssertAsync());
192192

193193
Assert.AreEqual(1, cacheHashtable.Count);
194194
}
195195

196-
private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
196+
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
197197
{
198198
using (var s = OpenSession())
199199
{
@@ -205,9 +205,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
205205
.SetInt32(0, 50));
206206
multiQuery.SetCacheable(true);
207207
var results = await (multiQuery.ListAsync(cancellationToken));
208-
var items = (IList)results[0];
208+
var items = (IList) results[0];
209209
Assert.AreEqual(89, items.Count);
210-
var count = (long)((IList)results[1])[0];
210+
var count = (long) ((IList) results[1])[0];
211211
Assert.AreEqual(99L, count);
212212
}
213213
}

src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public async Task SQLQueryInterfaceCacheableAsync()
208208
}
209209

210210
[Test(Description = "GH-2904")]
211-
public async Task CacheableScalarSQLQueryAsync()
211+
public async Task CacheableScalarSqlQueryAsync()
212212
{
213213
Organization ifa = new Organization("IFA");
214214
Organization jboss = new Organization("JBoss");
@@ -270,7 +270,7 @@ class ResultDto
270270
}
271271

272272
[Test(Description = "GH-3169")]
273-
public async Task CacheableScalarSQLQueryWithTransformerAsync()
273+
public async Task CacheableScalarSqlQueryWithTransformerAsync()
274274
{
275275
Organization ifa = new Organization("IFA");
276276

@@ -306,7 +306,34 @@ async Task AssertQueryAsync(bool fromCache)
306306
}
307307

308308
[Test(Description = "GH-3169")]
309-
public async Task CacheableScalarSQLMultiQueryWithTransformerAsync()
309+
public async Task CacheableScalarSqlEmptyQueryWithTransformerAsync()
310+
{
311+
async Task AssertQueryAsync(bool fromCache)
312+
{
313+
using (var s = OpenSession())
314+
using (var t = s.BeginTransaction())
315+
using (EnableStatisticsScope())
316+
{
317+
var l = await (s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
318+
.AddScalar("regionCode", NHibernateUtil.String)
319+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
320+
.SetCacheable(true)
321+
.ListAsync());
322+
await (t.CommitAsync());
323+
324+
Assert.That(l.Count, Is.EqualTo(0));
325+
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
326+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
327+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
328+
}
329+
}
330+
331+
await (AssertQueryAsync(false));
332+
await (AssertQueryAsync(true));
333+
}
334+
335+
[Test(Description = "GH-3169")]
336+
public async Task CacheableScalarSqlMultiQueryWithTransformerAsync()
310337
{
311338
Organization ifa = new Organization("IFA");
312339

@@ -354,6 +381,46 @@ async Task AssertQueryAsync(bool fromCache)
354381
await (AssertQueryAsync(true));
355382
}
356383

384+
[Test(Description = "GH-3169")]
385+
public async Task CacheableScalarSqlEmptyMultiQueryWithTransformerAsync()
386+
{
387+
async Task AssertQueryAsync(bool fromCache)
388+
{
389+
using (var s = OpenSession())
390+
using (var t = s.BeginTransaction())
391+
using (EnableStatisticsScope())
392+
{
393+
var q1 = s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
394+
.AddScalar("regionCode", NHibernateUtil.String)
395+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
396+
.SetCacheable(true);
397+
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
398+
.AddScalar("orgId", NHibernateUtil.Int64)
399+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
400+
.SetCacheable(true);
401+
402+
var batch = s.CreateQueryBatch();
403+
batch.Add<ResultDto>(q1);
404+
batch.Add<ResultDto>(q2);
405+
await (batch.ExecuteAsync());
406+
407+
var l1 = await (batch.GetResultAsync<ResultDto>(0));
408+
var l2 = await (batch.GetResultAsync<ResultDto>(1));
409+
410+
await (t.CommitAsync());
411+
412+
Assert.That(l1.Count, Is.EqualTo(0), "Unexpected results count for the first query.");
413+
Assert.That(l2.Count, Is.EqualTo(0), "Unexpected results count for the second query.");
414+
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
415+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 2), msg);
416+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 2 : 0), msg);
417+
}
418+
}
419+
420+
await (AssertQueryAsync(false));
421+
await (AssertQueryAsync(true));
422+
}
423+
357424
[Test]
358425
public async Task ResultSetMappingDefinitionAsync()
359426
{

src/NHibernate.Test/QueryTest/MultiCriteriaFixture.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void CanUseSecondLevelCacheWithPositionalParameters()
127127

128128
CreateItems();
129129

130-
DoMutiQueryAndAssert();
130+
DoMultiQueryAndAssert();
131131

132132
Assert.AreEqual(1, cacheHashtable.Count);
133133
}
@@ -136,20 +136,20 @@ public void CanUseSecondLevelCacheWithPositionalParameters()
136136
public void CanGetMultiQueryFromSecondLevelCache()
137137
{
138138
CreateItems();
139-
//set the query in the cache
140-
DoMutiQueryAndAssert();
139+
// Set the query in the cache.
140+
DoMultiQueryAndAssert();
141141

142142
var cacheHashtable = MultipleQueriesFixture.GetHashTableUsedAsQueryCache(Sfi);
143-
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
143+
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
144144
// The first element is a timestamp, then only we have the cached data.
145-
var cachedQuery = (IList)cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
145+
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
146146

147-
var firstQueryResults = (IList)cachedQuery[0];
147+
var firstQueryResults = (IList) cachedQuery[0];
148148
firstQueryResults.Clear();
149149
firstQueryResults.Add(3);
150150
firstQueryResults.Add(4);
151151

152-
var secondQueryResults = (IList)cachedQuery[1];
152+
var secondQueryResults = (IList) cachedQuery[1];
153153
secondQueryResults[0] = 2;
154154

155155
using (var s = Sfi.OpenSession())
@@ -161,9 +161,9 @@ public void CanGetMultiQueryFromSecondLevelCache()
161161
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
162162
multiCriteria.SetCacheable(true);
163163
var results = multiCriteria.List();
164-
var items = (IList)results[0];
164+
var items = (IList) results[0];
165165
Assert.AreEqual(2, items.Count);
166-
var count = (int)((IList)results[1])[0];
166+
var count = (int) ((IList) results[1])[0];
167167
Assert.AreEqual(2L, count);
168168
}
169169
}
@@ -173,12 +173,12 @@ public void CanUpdateStatisticsWhenGetMultiQueryFromSecondLevelCache()
173173
{
174174
CreateItems();
175175

176-
DoMutiQueryAndAssert();
176+
DoMultiQueryAndAssert();
177177
Assert.AreEqual(0, Sfi.Statistics.QueryCacheHitCount);
178178
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
179179
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
180180

181-
DoMutiQueryAndAssert();
181+
DoMultiQueryAndAssert();
182182
Assert.AreEqual(1, Sfi.Statistics.QueryCacheHitCount);
183183
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
184184
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
@@ -437,7 +437,7 @@ public void CanNotRetrieveDetachedCriteriaResultWithUnknownKey()
437437
}
438438
}
439439

440-
private void DoMutiQueryAndAssert()
440+
private void DoMultiQueryAndAssert()
441441
{
442442
using (var s = OpenSession())
443443
{
@@ -448,9 +448,9 @@ private void DoMutiQueryAndAssert()
448448
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
449449
multiCriteria.SetCacheable(true);
450450
var results = multiCriteria.List();
451-
var items = (IList)results[0];
451+
var items = (IList) results[0];
452452
Assert.AreEqual(89, items.Count);
453-
var count = (int)((IList)results[1])[0];
453+
var count = (int) ((IList) results[1])[0];
454454
Assert.AreEqual(99L, count);
455455
}
456456
}

0 commit comments

Comments
 (0)