Skip to content

Commit c07c6af

Browse files
Add test for multi-columns cached queries
1 parent 8232bcf commit c07c6af

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ async Task AssertQueryAsync(bool fromCache)
354354
.AddScalar("regionCode", NHibernateUtil.String)
355355
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
356356
.SetCacheable(true);
357-
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
357+
var q2 = s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
358358
.AddScalar("orgId", NHibernateUtil.Int64)
359+
.AddScalar("regionCode", NHibernateUtil.String)
359360
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
360361
.SetCacheable(true);
361362

@@ -394,8 +395,9 @@ async Task AssertQueryAsync(bool fromCache)
394395
.AddScalar("regionCode", NHibernateUtil.String)
395396
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
396397
.SetCacheable(true);
397-
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
398+
var q2 = s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
398399
.AddScalar("orgId", NHibernateUtil.Int64)
400+
.AddScalar("regionCode", NHibernateUtil.String)
399401
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
400402
.SetCacheable(true);
401403

@@ -421,6 +423,43 @@ async Task AssertQueryAsync(bool fromCache)
421423
await (AssertQueryAsync(true));
422424
}
423425

426+
[Test(Description = "GH-3169")]
427+
public async Task CacheableMultiScalarSqlQueryWithTransformerAsync()
428+
{
429+
Organization ifa = new Organization("IFA");
430+
431+
using (ISession s = OpenSession())
432+
using (ITransaction t = s.BeginTransaction())
433+
{
434+
await (s.SaveAsync(ifa));
435+
await (t.CommitAsync());
436+
}
437+
438+
async Task AssertQueryAsync(bool fromCache)
439+
{
440+
using (var s = OpenSession())
441+
using (var t = s.BeginTransaction())
442+
using (EnableStatisticsScope())
443+
{
444+
var l = await (s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
445+
.AddScalar("orgId", NHibernateUtil.Int64)
446+
.AddScalar("regionCode", NHibernateUtil.String)
447+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
448+
.SetCacheable(true)
449+
.ListAsync());
450+
await (t.CommitAsync());
451+
452+
Assert.That(l.Count, Is.EqualTo(1));
453+
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
454+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
455+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
456+
}
457+
}
458+
459+
await (AssertQueryAsync(false));
460+
await (AssertQueryAsync(true));
461+
}
462+
424463
[Test]
425464
public async Task ResultSetMappingDefinitionAsync()
426465
{

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,9 @@ void AssertQuery(bool fromCache)
336336
.AddScalar("regionCode", NHibernateUtil.String)
337337
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
338338
.SetCacheable(true);
339-
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
339+
var q2 = s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
340340
.AddScalar("orgId", NHibernateUtil.Int64)
341+
.AddScalar("regionCode", NHibernateUtil.String)
341342
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
342343
.SetCacheable(true);
343344

@@ -376,8 +377,9 @@ void AssertQuery(bool fromCache)
376377
.AddScalar("regionCode", NHibernateUtil.String)
377378
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
378379
.SetCacheable(true);
379-
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
380+
var q2 = s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
380381
.AddScalar("orgId", NHibernateUtil.Int64)
382+
.AddScalar("regionCode", NHibernateUtil.String)
381383
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
382384
.SetCacheable(true);
383385

@@ -403,6 +405,43 @@ void AssertQuery(bool fromCache)
403405
AssertQuery(true);
404406
}
405407

408+
[Test(Description = "GH-3169")]
409+
public void CacheableMultiScalarSqlQueryWithTransformer()
410+
{
411+
Organization ifa = new Organization("IFA");
412+
413+
using (ISession s = OpenSession())
414+
using (ITransaction t = s.BeginTransaction())
415+
{
416+
s.Save(ifa);
417+
t.Commit();
418+
}
419+
420+
void AssertQuery(bool fromCache)
421+
{
422+
using (var s = OpenSession())
423+
using (var t = s.BeginTransaction())
424+
using (EnableStatisticsScope())
425+
{
426+
var l = s.CreateSQLQuery("select org.ORGID as orgId, org.NAME as regionCode from ORGANIZATION org")
427+
.AddScalar("orgId", NHibernateUtil.Int64)
428+
.AddScalar("regionCode", NHibernateUtil.String)
429+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
430+
.SetCacheable(true)
431+
.List();
432+
t.Commit();
433+
434+
Assert.That(l.Count, Is.EqualTo(1));
435+
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
436+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
437+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
438+
}
439+
}
440+
441+
AssertQuery(false);
442+
AssertQuery(true);
443+
}
444+
406445
[Test]
407446
public void ResultSetMappingDefinition()
408447
{

0 commit comments

Comments
 (0)