Skip to content

Commit e454d82

Browse files
committed
Adjust test for Oracle and slight refactoring
1 parent a62df45 commit e454d82

File tree

3 files changed

+97
-15
lines changed

3 files changed

+97
-15
lines changed

src/NHibernate.Test/Async/Linq/QueryCacheableTests.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,47 @@ public async Task FutureFetchIsCachableAsync()
408408
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
409409
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(2), "Unexpected cache hit count");
410410
}
411+
412+
[Explicit("Not working. dto.Customer retrieved from cache as uninitialized proxy")]
413+
[Test]
414+
public async Task ProjectedEntitiesAreCachableAsync()
415+
{
416+
Sfi.Statistics.Clear();
417+
await (Sfi.EvictQueriesAsync());
418+
var dto = await (session.Query<Order>()
419+
.WithOptions(o => o.SetCacheable(true))
420+
.Where(x => x.OrderId == 10248)
421+
.Select(x => new {Customer = x.Customer, Order = x})
422+
.FirstOrDefaultAsync());
423+
424+
Assert.That(dto.Order, Is.Not.Null);
425+
Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True);
426+
Assert.That(dto.Customer, Is.Not.Null);
427+
Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True);
428+
429+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
430+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
431+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");
432+
433+
Sfi.Statistics.Clear();
434+
session.Clear();
435+
436+
dto = await (session.Query<Order>()
437+
.WithOptions(o => o.SetCacheable(true))
438+
.Where(x => x.OrderId == 10248)
439+
.Select(x => new {Customer = x.Customer, Order = x})
440+
.FirstOrDefaultAsync());
441+
442+
Assert.That(dto.Order, Is.Not.Null);
443+
Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True);
444+
Assert.That(dto.Customer, Is.Not.Null);
445+
Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True);
446+
447+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
448+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
449+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
450+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
451+
}
411452

412453
[Test]
413454
public async Task CacheHqlQueryWithFetchAndTransformerThatChangeTupleAsync()
@@ -419,11 +460,11 @@ public async Task CacheHqlQueryWithFetchAndTransformerThatChangeTupleAsync()
419460
// the combination of query and transformer doesn't make sense.
420461
// It's simply used as example of returned data being transformed before caching leading to mismatch between
421462
// Loader.ResultTypes collection and provided tuple
422-
order = await (session.CreateQuery("select o.Customer.CompanyName, o from Order o join fetch o.Customer")
423-
.SetMaxResults(1)
424-
.SetCacheable(true)
425-
.SetResultTransformer(Transformers.RootEntity)
426-
.UniqueResultAsync<Order>());
463+
order = await (session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer")
464+
.SetMaxResults(1)
465+
.SetCacheable(true)
466+
.SetResultTransformer(Transformers.RootEntity)
467+
.UniqueResultAsync<Order>());
427468

428469
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
429470
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
@@ -435,7 +476,7 @@ public async Task CacheHqlQueryWithFetchAndTransformerThatChangeTupleAsync()
435476
session.Clear();
436477
Sfi.Statistics.Clear();
437478

438-
order = await (session.CreateQuery("select o.Customer.CompanyName, o from Order o join fetch o.Customer")
479+
order = await (session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer")
439480
.SetMaxResults(1)
440481
.SetCacheable(true)
441482
.SetResultTransformer(Transformers.RootEntity)
@@ -452,6 +493,7 @@ public async Task CacheHqlQueryWithFetchAndTransformerThatChangeTupleAsync()
452493

453494
private static void AssertFetchedOrder(Order order)
454495
{
496+
Assert.That(NHibernateUtil.IsInitialized(order));
455497
Assert.That(order.Customer, Is.Not.Null, "Expected the fetched Customer to be initialized");
456498
Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.True, "Expected the fetched Customer to be initialized");
457499
Assert.That(NHibernateUtil.IsInitialized(order.OrderLines), Is.True, "Expected the fetched OrderLines to be initialized");

src/NHibernate.Test/Linq/QueryCacheableTests.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,47 @@ public void FutureFetchIsCachable()
397397
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
398398
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(2), "Unexpected cache hit count");
399399
}
400+
401+
[Explicit("Not working. dto.Customer retrieved from cache as uninitialized proxy")]
402+
[Test]
403+
public void ProjectedEntitiesAreCachable()
404+
{
405+
Sfi.Statistics.Clear();
406+
Sfi.EvictQueries();
407+
var dto = session.Query<Order>()
408+
.WithOptions(o => o.SetCacheable(true))
409+
.Where(x => x.OrderId == 10248)
410+
.Select(x => new {Customer = x.Customer, Order = x})
411+
.FirstOrDefault();
412+
413+
Assert.That(dto.Order, Is.Not.Null);
414+
Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True);
415+
Assert.That(dto.Customer, Is.Not.Null);
416+
Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True);
417+
418+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
419+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
420+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");
421+
422+
Sfi.Statistics.Clear();
423+
session.Clear();
424+
425+
dto = session.Query<Order>()
426+
.WithOptions(o => o.SetCacheable(true))
427+
.Where(x => x.OrderId == 10248)
428+
.Select(x => new {Customer = x.Customer, Order = x})
429+
.FirstOrDefault();
430+
431+
Assert.That(dto.Order, Is.Not.Null);
432+
Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True);
433+
Assert.That(dto.Customer, Is.Not.Null);
434+
Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True);
435+
436+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
437+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
438+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
439+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
440+
}
400441

401442
[Test]
402443
public void CacheHqlQueryWithFetchAndTransformerThatChangeTuple()
@@ -408,11 +449,11 @@ public void CacheHqlQueryWithFetchAndTransformerThatChangeTuple()
408449
// the combination of query and transformer doesn't make sense.
409450
// It's simply used as example of returned data being transformed before caching leading to mismatch between
410451
// Loader.ResultTypes collection and provided tuple
411-
order = session.CreateQuery("select o.Customer.CompanyName, o from Order o join fetch o.Customer")
412-
.SetMaxResults(1)
413-
.SetCacheable(true)
414-
.SetResultTransformer(Transformers.RootEntity)
415-
.UniqueResult<Order>();
452+
order = session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer")
453+
.SetMaxResults(1)
454+
.SetCacheable(true)
455+
.SetResultTransformer(Transformers.RootEntity)
456+
.UniqueResult<Order>();
416457

417458
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
418459
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
@@ -424,7 +465,7 @@ public void CacheHqlQueryWithFetchAndTransformerThatChangeTuple()
424465
session.Clear();
425466
Sfi.Statistics.Clear();
426467

427-
order = session.CreateQuery("select o.Customer.CompanyName, o from Order o join fetch o.Customer")
468+
order = session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer")
428469
.SetMaxResults(1)
429470
.SetCacheable(true)
430471
.SetResultTransformer(Transformers.RootEntity)
@@ -441,6 +482,7 @@ public void CacheHqlQueryWithFetchAndTransformerThatChangeTuple()
441482

442483
private static void AssertFetchedOrder(Order order)
443484
{
485+
Assert.That(NHibernateUtil.IsInitialized(order));
444486
Assert.That(order.Customer, Is.Not.Null, "Expected the fetched Customer to be initialized");
445487
Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.True, "Expected the fetched Customer to be initialized");
446488
Assert.That(NHibernateUtil.IsInitialized(order.OrderLines), Is.True, "Expected the fetched OrderLines to be initialized");

src/NHibernate/Loader/Hql/QueryLoader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ private void Initialize(SelectClause selectClause)
241241
_includeInSelect = new bool[size];
242242
_owners = new int[size];
243243
_ownerAssociationTypes = new EntityType[size];
244-
List<int> resultTypePersisters = new List<int>();
245244

246245
for (int i = 0; i < size; i++)
247246
{
@@ -267,7 +266,6 @@ private void Initialize(SelectClause selectClause)
267266
if (_includeInSelect[i])
268267
{
269268
_selectLength++;
270-
resultTypePersisters.Add(i);
271269
}
272270

273271
_owners[i] = -1; //by default
@@ -288,7 +286,7 @@ private void Initialize(SelectClause selectClause)
288286
//NONE, because its the requested lock mode, not the actual!
289287
_defaultLockModes = ArrayHelper.Fill(LockMode.None, size);
290288
_uncacheableCollectionPersisters = _queryTranslator.UncacheableCollectionPersisters;
291-
CachePersistersWithCollections(resultTypePersisters);
289+
CachePersistersWithCollections(ArrayHelper.IndexesOf(_includeInSelect, true));
292290
}
293291

294292
public IList List(ISessionImplementor session, QueryParameters queryParameters)

0 commit comments

Comments
 (0)