Skip to content

Commit 2de3f85

Browse files
bahusoidfredericDelaporte
authored andcommitted
Add tests for future with aliased join and mapped eager fetch
See nhibernate#1344 & nhibernate#1290
1 parent 866c3eb commit 2de3f85

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

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

+69
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,75 @@ public async Task SameCollectionFetchesAsync()
209209
}
210210
}
211211

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

214283
protected override HbmMapping GetMappings()

src/NHibernate.Test/Futures/QueryBatchFixture.cs

+69
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,75 @@ public void SameCollectionFetches()
197197
}
198198
}
199199

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

202271
protected override HbmMapping GetMappings()

0 commit comments

Comments
 (0)