|
10 | 10 |
|
11 | 11 | using System.Collections;
|
12 | 12 | using System.Linq;
|
| 13 | +using NHibernate.Criterion; |
| 14 | +using NHibernate.Multi; |
13 | 15 | using NHibernate.Transform;
|
14 | 16 | using NUnit.Framework;
|
15 |
| -using NHibernate.Criterion; |
16 | 17 |
|
17 | 18 | namespace NHibernate.Test.SqlTest.Query
|
18 | 19 | {
|
@@ -62,6 +63,20 @@ protected override string MappingsAssembly
|
62 | 63 | get { return "NHibernate.Test"; }
|
63 | 64 | }
|
64 | 65 |
|
| 66 | + protected override void OnTearDown() |
| 67 | + { |
| 68 | + using (var session = OpenSession()) |
| 69 | + using (var transaction = session.BeginTransaction()) |
| 70 | + { |
| 71 | + session.CreateQuery("delete from Employment").ExecuteUpdate(); |
| 72 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 73 | + |
| 74 | + transaction.Commit(); |
| 75 | + } |
| 76 | + |
| 77 | + Sfi.QueryCache.Clear(); |
| 78 | + } |
| 79 | + |
65 | 80 | [Test]
|
66 | 81 | public async Task FailOnNoAddEntityOrScalarAsync()
|
67 | 82 | {
|
@@ -136,18 +151,6 @@ public async Task SQLQueryInterfaceAsync()
|
136 | 151 | await (t.CommitAsync());
|
137 | 152 | s.Close();
|
138 | 153 | }
|
139 |
| - |
140 |
| - using (var s = OpenSession()) |
141 |
| - using (var t = s.BeginTransaction()) |
142 |
| - { |
143 |
| - await (s.DeleteAsync(emp)); |
144 |
| - await (s.DeleteAsync(gavin)); |
145 |
| - await (s.DeleteAsync(ifa)); |
146 |
| - await (s.DeleteAsync(jboss)); |
147 |
| - |
148 |
| - await (t.CommitAsync()); |
149 |
| - s.Close(); |
150 |
| - } |
151 | 154 | }
|
152 | 155 |
|
153 | 156 | [Test]
|
@@ -202,18 +205,6 @@ public async Task SQLQueryInterfaceCacheableAsync()
|
202 | 205 | await (t.CommitAsync());
|
203 | 206 | s.Close();
|
204 | 207 | }
|
205 |
| - |
206 |
| - using (var s = OpenSession()) |
207 |
| - using (var t = s.BeginTransaction()) |
208 |
| - { |
209 |
| - await (s.DeleteAsync(emp)); |
210 |
| - await (s.DeleteAsync(gavin)); |
211 |
| - await (s.DeleteAsync(ifa)); |
212 |
| - await (s.DeleteAsync(jboss)); |
213 |
| - |
214 |
| - await (t.CommitAsync()); |
215 |
| - s.Close(); |
216 |
| - } |
217 | 208 | }
|
218 | 209 |
|
219 | 210 | [Test(Description = "GH-2904")]
|
@@ -270,20 +261,11 @@ Task<IList> GetCacheableSqlQueryResultsAsync()
|
270 | 261 | Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "results are expected from cache");
|
271 | 262 | }
|
272 | 263 | }
|
273 |
| - |
274 |
| - using (var s = OpenSession()) |
275 |
| - using (var t = s.BeginTransaction()) |
276 |
| - { |
277 |
| - await (s.DeleteAsync(emp)); |
278 |
| - await (s.DeleteAsync(gavin)); |
279 |
| - await (s.DeleteAsync(ifa)); |
280 |
| - await (s.DeleteAsync(jboss)); |
281 |
| - await (t.CommitAsync()); |
282 |
| - } |
283 | 264 | }
|
284 | 265 |
|
285 | 266 | class ResultDto
|
286 | 267 | {
|
| 268 | + public long orgId { get; set; } |
287 | 269 | public string regionCode { get; set; }
|
288 | 270 | }
|
289 | 271 |
|
@@ -312,23 +294,64 @@ async Task AssertQueryAsync(bool fromCache)
|
312 | 294 | .ListAsync());
|
313 | 295 | await (t.CommitAsync());
|
314 | 296 |
|
315 |
| - Assert.AreEqual(1, l.Count); |
316 |
| - //TODO: Uncomment if we properly fix caching auto discovery type queries with transformers |
317 |
| - // var msg = "results are expected from " + (fromCache ? "cache" : "DB"); |
318 |
| - // Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg); |
319 |
| - // Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg); |
| 297 | + Assert.That(l.Count, Is.EqualTo(1)); |
| 298 | + var msg = "Results are expected from " + (fromCache ? "cache" : "DB"); |
| 299 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg); |
| 300 | + Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg); |
320 | 301 | }
|
321 | 302 | }
|
322 | 303 |
|
323 | 304 | await (AssertQueryAsync(false));
|
324 | 305 | await (AssertQueryAsync(true));
|
| 306 | + } |
325 | 307 |
|
326 |
| - using (var s = OpenSession()) |
327 |
| - using (var t = s.BeginTransaction()) |
| 308 | + [Test(Description = "GH-3169")] |
| 309 | + public async Task CacheableScalarSQLMultiQueryWithTransformerAsync() |
| 310 | + { |
| 311 | + Organization ifa = new Organization("IFA"); |
| 312 | + |
| 313 | + using (ISession s = OpenSession()) |
| 314 | + using (ITransaction t = s.BeginTransaction()) |
328 | 315 | {
|
329 |
| - await (s.DeleteAsync(ifa)); |
| 316 | + await (s.SaveAsync(ifa)); |
330 | 317 | await (t.CommitAsync());
|
331 | 318 | }
|
| 319 | + |
| 320 | + async Task AssertQueryAsync(bool fromCache) |
| 321 | + { |
| 322 | + using (var s = OpenSession()) |
| 323 | + using (var t = s.BeginTransaction()) |
| 324 | + using (EnableStatisticsScope()) |
| 325 | + { |
| 326 | + var q1 = s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org") |
| 327 | + .AddScalar("regionCode", NHibernateUtil.String) |
| 328 | + .SetResultTransformer(Transformers.AliasToBean<ResultDto>()) |
| 329 | + .SetCacheable(true); |
| 330 | + var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org") |
| 331 | + .AddScalar("orgId", NHibernateUtil.Int64) |
| 332 | + .SetResultTransformer(Transformers.AliasToBean<ResultDto>()) |
| 333 | + .SetCacheable(true); |
| 334 | + |
| 335 | + var batch = s.CreateQueryBatch(); |
| 336 | + batch.Add<ResultDto>(q1); |
| 337 | + batch.Add<ResultDto>(q2); |
| 338 | + await (batch.ExecuteAsync()); |
| 339 | + |
| 340 | + var l1 = await (batch.GetResultAsync<ResultDto>(0)); |
| 341 | + var l2 = await (batch.GetResultAsync<ResultDto>(1)); |
| 342 | + |
| 343 | + await (t.CommitAsync()); |
| 344 | + |
| 345 | + Assert.That(l1.Count, Is.EqualTo(1), "Unexpected results count for the first query."); |
| 346 | + Assert.That(l2.Count, Is.EqualTo(1), "Unexpected results count for the second query."); |
| 347 | + var msg = "Results are expected from " + (fromCache ? "cache" : "DB"); |
| 348 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 2), msg); |
| 349 | + Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 2 : 0), msg); |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + await (AssertQueryAsync(false)); |
| 354 | + await (AssertQueryAsync(true)); |
332 | 355 | }
|
333 | 356 |
|
334 | 357 | [Test]
|
@@ -356,11 +379,6 @@ public async Task ResultSetMappingDefinitionAsync()
|
356 | 379 | .ListAsync());
|
357 | 380 | Assert.AreEqual(l.Count, 1);
|
358 | 381 |
|
359 |
| - await (s.DeleteAsync(emp)); |
360 |
| - await (s.DeleteAsync(gavin)); |
361 |
| - await (s.DeleteAsync(ifa)); |
362 |
| - await (s.DeleteAsync(jboss)); |
363 |
| - |
364 | 382 | await (t.CommitAsync());
|
365 | 383 | s.Close();
|
366 | 384 | }
|
@@ -443,8 +461,6 @@ public async Task ScalarValuesAsync()
|
443 | 461 | Assert.AreEqual(o[1], "JBoss");
|
444 | 462 | Assert.AreEqual(o[0], idJBoss);
|
445 | 463 |
|
446 |
| - await (s.DeleteAsync(ifa)); |
447 |
| - await (s.DeleteAsync(jboss)); |
448 | 464 | await (t.CommitAsync());
|
449 | 465 | s.Close();
|
450 | 466 | }
|
|
0 commit comments