diff --git a/src/NHibernate.Test/Async/Linq/ProjectionsTests.cs b/src/NHibernate.Test/Async/Linq/ProjectionsTests.cs index ecd7db50a9d..ca709594e90 100644 --- a/src/NHibernate.Test/Async/Linq/ProjectionsTests.cs +++ b/src/NHibernate.Test/Async/Linq/ProjectionsTests.cs @@ -12,8 +12,8 @@ using System.Collections.Generic; using System.Linq; using NHibernate.DomainModel.Northwind.Entities; -using NUnit.Framework; using NHibernate.Linq; +using NUnit.Framework; namespace NHibernate.Test.Linq { @@ -237,6 +237,7 @@ public async Task CanProjectCollectionsInsideAnonymousTypeAsync() { var query = db.Orders.Select(o => new { o.OrderId, o.OrderLines }); var result = await (query.ToListAsync()); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -250,6 +251,7 @@ public async Task ProjectAnonymousTypeWithCollectionAsync() var result = await (query.ToListAsync()); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result[0].o.OrderLines, Is.EquivalentTo(result[0].OrderLines)); } @@ -263,6 +265,7 @@ public async Task ProjectAnonymousTypeWithCollection1Async() var result = await (query.ToListAsync()); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result[0].o.OrderLines, Is.EquivalentTo(result[0].OrderLines)); } @@ -275,6 +278,7 @@ public async Task ProjectAnonymousTypeWithCollection2Async() select new { o.OrderLines, A = 1, B = 2 }; var result = await (query.ToListAsync()); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -287,6 +291,7 @@ public async Task ProjectAnonymousTypeWithCollection3Async() select new { OrderLines = o.OrderLines.ToList() }; var result = await (query.ToListAsync()); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -304,9 +309,32 @@ public async Task ProjectKnownTypeWithCollectionAsync() var result = await (query.ToListAsync()); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].ExpandedElement.OrderLines), Is.False); + Assert.That(NHibernateUtil.IsInitialized(result[0].ProjectedProperty0), Is.True); Assert.That(result[0].ExpandedElement.OrderLines, Is.EquivalentTo(result[0].ProjectedProperty0)); } - + + [Test] + public async Task ProjectKnownTypeWithCollectionWithFetchAsync() + { + // NH-3396 + // However, due to a double join on Orderlines, this query has 7000+ results + var query = from o in db.Orders.Fetch(x => x.OrderLines) + select new ExpandedWrapper> + { + ExpandedElement = o, + ProjectedProperty0 = o.OrderLines, + Description = "OrderLine", + ReferenceDescription = "OrderLine" + }; + + var result = await (query.ToListAsync()); + Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].ExpandedElement.OrderLines), Is.True); + Assert.That(NHibernateUtil.IsInitialized(result[0].ProjectedProperty0), Is.True); + Assert.That(result[0].ExpandedElement.OrderLines, Is.EquivalentTo(result[0].ProjectedProperty0)); + } + [Test] public async Task ProjectKnownTypeWithCollection2Async() { diff --git a/src/NHibernate.Test/Linq/ProjectionsTests.cs b/src/NHibernate.Test/Linq/ProjectionsTests.cs index 2072964783a..7c0f7b436ce 100644 --- a/src/NHibernate.Test/Linq/ProjectionsTests.cs +++ b/src/NHibernate.Test/Linq/ProjectionsTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NHibernate.DomainModel.Northwind.Entities; +using NHibernate.Linq; using NUnit.Framework; namespace NHibernate.Test.Linq @@ -225,6 +226,7 @@ public void CanProjectCollectionsInsideAnonymousType() { var query = db.Orders.Select(o => new { o.OrderId, o.OrderLines }); var result = query.ToList(); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -238,6 +240,7 @@ public void ProjectAnonymousTypeWithCollection() var result = query.ToList(); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result[0].o.OrderLines, Is.EquivalentTo(result[0].OrderLines)); } @@ -251,6 +254,7 @@ public void ProjectAnonymousTypeWithCollection1() var result = query.ToList(); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result[0].o.OrderLines, Is.EquivalentTo(result[0].OrderLines)); } @@ -263,6 +267,7 @@ public void ProjectAnonymousTypeWithCollection2() select new { o.OrderLines, A = 1, B = 2 }; var result = query.ToList(); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -275,6 +280,7 @@ public void ProjectAnonymousTypeWithCollection3() select new { OrderLines = o.OrderLines.ToList() }; var result = query.ToList(); + Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True); Assert.That(result.Count, Is.EqualTo(830)); } @@ -292,9 +298,32 @@ public void ProjectKnownTypeWithCollection() var result = query.ToList(); Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].ExpandedElement.OrderLines), Is.False); + Assert.That(NHibernateUtil.IsInitialized(result[0].ProjectedProperty0), Is.True); Assert.That(result[0].ExpandedElement.OrderLines, Is.EquivalentTo(result[0].ProjectedProperty0)); } - + + [Test] + public void ProjectKnownTypeWithCollectionWithFetch() + { + // NH-3396 + // However, due to a double join on Orderlines, this query has 7000+ results + var query = from o in db.Orders.Fetch(x => x.OrderLines) + select new ExpandedWrapper> + { + ExpandedElement = o, + ProjectedProperty0 = o.OrderLines, + Description = "OrderLine", + ReferenceDescription = "OrderLine" + }; + + var result = query.ToList(); + Assert.That(result.Count, Is.EqualTo(830)); + Assert.That(NHibernateUtil.IsInitialized(result[0].ExpandedElement.OrderLines), Is.True); + Assert.That(NHibernateUtil.IsInitialized(result[0].ProjectedProperty0), Is.True); + Assert.That(result[0].ExpandedElement.OrderLines, Is.EquivalentTo(result[0].ProjectedProperty0)); + } + [Test] public void ProjectKnownTypeWithCollection2() {