Skip to content

Commit b147a8d

Browse files
committed
Add tests for future with aliased join and mapped eager fetch
See #1344 & #1290
1 parent 426d3b3 commit b147a8d

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/NHibernate.Test/Async/Futures/QueryBatchFixture.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,75 @@ public async Task SameCollectionFetchesAsync()
208208
}
209209
}
210210

211+
//NH-3864 - Cacheable Multicriteria/Future'd query with aliased join throw exception
212+
[Test]
213+
public void CacheableCriteriaWithAliasedJoinFutureAsync()
214+
{
215+
using (var session = OpenSession())
216+
{
217+
EntitySimpleChild child1 = null;
218+
var ecFuture = session.QueryOver<EntityComplex>()
219+
.JoinAlias(c => c.Child1, () => child1)
220+
.Where(c => c.Id == _parentId)
221+
.Cacheable()
222+
.FutureValue();
223+
EntityComplex value = null;
224+
Assert.DoesNotThrowAsync(async () => value = await (ecFuture.GetValueAsync()));
225+
Assert.That(value, Is.Not.Null);
226+
}
227+
228+
using (var sqlLog = new SqlLogSpy())
229+
using (var session = OpenSession())
230+
{
231+
EntitySimpleChild child1 = null;
232+
var ecFuture = session.QueryOver<EntityComplex>()
233+
.JoinAlias(c => c.Child1, () => child1)
234+
.Where(c => c.Id == _parentId)
235+
.Cacheable()
236+
.FutureValue();
237+
EntityComplex value = null;
238+
Assert.DoesNotThrowAsync(async () => value = await (ecFuture.GetValueAsync()));
239+
Assert.That(value, Is.Not.Null);
240+
241+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(0), "Query is expected to be retrieved from cache");
242+
}
243+
}
244+
245+
//NH-3334 - 'collection is not associated with any session' upon refreshing objects from QueryOver<>().Future<>()
246+
[KnownBug("NH-3334")]
247+
[Test]
248+
public async Task RefreshFutureWithEagerCollectionsAsync()
249+
{
250+
using (var session = OpenSession())
251+
{
252+
var ecFutureList = session.QueryOver<EntityEager>().Future();
253+
254+
foreach(var ec in await (ecFutureList.GetEnumerableAsync()))
255+
{
256+
//trouble causes ec.ChildrenListEager with eager select mapping
257+
Assert.DoesNotThrowAsync(() => session.RefreshAsync(ec), "session.Refresh should not throw exception");
258+
}
259+
}
260+
}
261+
262+
//Related to NH-3334. Eager mappings are not fetched by Future
263+
[KnownBug("NH-3334")]
264+
[Test]
265+
public async Task FutureForEagerMappedCollectionAsync()
266+
{
267+
//Note: This behavior might be considered as feature but it's not documented.
268+
//Quirk: if this query is also cached - results will be still eager loaded when values retrieved from cache
269+
using (var session = OpenSession())
270+
{
271+
var futureValue = session.QueryOver<EntityEager>().Where(e => e.Id == _eagerId).FutureValue();
272+
273+
Assert.That(await (futureValue.GetValueAsync()), Is.Not.Null);
274+
Assert.That(NHibernateUtil.IsInitialized(await (futureValue.GetValueAsync())), Is.True);
275+
Assert.That(NHibernateUtil.IsInitialized((await (futureValue.GetValueAsync())).ChildrenListEager), Is.True);
276+
Assert.That(NHibernateUtil.IsInitialized((await (futureValue.GetValueAsync())).ChildrenListSubselect), Is.True);
277+
}
278+
}
279+
211280
#region Test Setup
212281

213282
protected override HbmMapping GetMappings()

src/NHibernate.Test/Futures/QueryBatchFixture.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,75 @@ public void SameCollectionFetches()
196196
}
197197
}
198198

199+
//NH-3864 - Cacheable Multicriteria/Future'd query with aliased join throw exception
200+
[Test]
201+
public void CacheableCriteriaWithAliasedJoinFuture()
202+
{
203+
using (var session = OpenSession())
204+
{
205+
EntitySimpleChild child1 = null;
206+
var ecFuture = session.QueryOver<EntityComplex>()
207+
.JoinAlias(c => c.Child1, () => child1)
208+
.Where(c => c.Id == _parentId)
209+
.Cacheable()
210+
.FutureValue();
211+
EntityComplex value = null;
212+
Assert.DoesNotThrow(() => value = ecFuture.Value);
213+
Assert.That(value, Is.Not.Null);
214+
}
215+
216+
using (var sqlLog = new SqlLogSpy())
217+
using (var session = OpenSession())
218+
{
219+
EntitySimpleChild child1 = null;
220+
var ecFuture = session.QueryOver<EntityComplex>()
221+
.JoinAlias(c => c.Child1, () => child1)
222+
.Where(c => c.Id == _parentId)
223+
.Cacheable()
224+
.FutureValue();
225+
EntityComplex value = null;
226+
Assert.DoesNotThrow(() => value = ecFuture.Value);
227+
Assert.That(value, Is.Not.Null);
228+
229+
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(0), "Query is expected to be retrieved from cache");
230+
}
231+
}
232+
233+
//NH-3334 - 'collection is not associated with any session' upon refreshing objects from QueryOver<>().Future<>()
234+
[KnownBug("NH-3334")]
235+
[Test]
236+
public void RefreshFutureWithEagerCollections()
237+
{
238+
using (var session = OpenSession())
239+
{
240+
var ecFutureList = session.QueryOver<EntityEager>().Future();
241+
242+
foreach(var ec in ecFutureList.GetEnumerable())
243+
{
244+
//trouble causes ec.ChildrenListEager with eager select mapping
245+
Assert.DoesNotThrow(() => session.Refresh(ec), "session.Refresh should not throw exception");
246+
}
247+
}
248+
}
249+
250+
//Related to NH-3334. Eager mappings are not fetched by Future
251+
[KnownBug("NH-3334")]
252+
[Test]
253+
public void FutureForEagerMappedCollection()
254+
{
255+
//Note: This behavior might be considered as feature but it's not documented.
256+
//Quirk: if this query is also cached - results will be still eager loaded when values retrieved from cache
257+
using (var session = OpenSession())
258+
{
259+
var futureValue = session.QueryOver<EntityEager>().Where(e => e.Id == _eagerId).FutureValue();
260+
261+
Assert.That(futureValue.Value, Is.Not.Null);
262+
Assert.That(NHibernateUtil.IsInitialized(futureValue.Value), Is.True);
263+
Assert.That(NHibernateUtil.IsInitialized(futureValue.Value.ChildrenListEager), Is.True);
264+
Assert.That(NHibernateUtil.IsInitialized(futureValue.Value.ChildrenListSubselect), Is.True);
265+
}
266+
}
267+
199268
#region Test Setup
200269

201270
protected override HbmMapping GetMappings()

0 commit comments

Comments
 (0)