Skip to content

Fetch requests do not work with collection projections #2341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion src/NHibernate.Test/Linq/ProjectionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -211,6 +212,14 @@ public void CanProjectManyCollections()
var result = query.ToList();
Assert.That(result.Count, Is.EqualTo(2155));
}

[Test]
public void CanProjectManyCollectionsWithFetch()
{
var query = db.Orders.FetchMany(o => o.OrderLines).SelectMany(o => o.OrderLines);
var result = query.ToList();
Assert.That(result.Count, Is.EqualTo(2155));
}

[Test]
public void CanProjectCollections()
Expand All @@ -220,11 +229,29 @@ public void CanProjectCollections()
Assert.That(result.Count, Is.EqualTo(830));
}

[Test]
public void CanProjectCollectionsWithFetch()
{
var query = db.Orders.Fetch(o => o.OrderLines).Select(o => o.OrderLines);
var result = query.ToList();
Assert.That(result.Count, Is.EqualTo(830));
}

[Test]
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));
}

[Test]
public void CanProjectCollectionsInsideAnonymousTypeWithFetch()
{
var query = db.Orders.Fetch(o => o.OrderLines).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));
}

Expand All @@ -238,6 +265,19 @@ 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));
}

[Test]
public void ProjectAnonymousTypeWithCollectionWithFetch()
{
var query = from o in db.Orders.Fetch(o => o.OrderLines)
select new { o, o.OrderLines };

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));
}

Expand All @@ -251,6 +291,19 @@ 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));
}

[Test]
public void ProjectAnonymousTypeWithCollection1WithFetch()
{
var query = from o in db.Orders.Fetch(o => o.OrderLines)
select new { o.OrderLines, o };

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));
}

Expand All @@ -263,6 +316,18 @@ 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));
}

[Test]
public void ProjectAnonymousTypeWithCollection2WithFetch()
{
var query = from o in db.Orders.Fetch(o => o.OrderLines)
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));
}

Expand All @@ -275,6 +340,18 @@ 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));
}

[Test]
public void ProjectAnonymousTypeWithCollection3WithFetch()
{
var query = from o in db.Orders.Fetch(o => o.OrderLines)
select new { OrderLines = o.OrderLines.ToList() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sql created from this query returns more than 7000 rows, I think that it would be better to use the PatientRecords collection of Patient which has less data:

var query = from o in db.Patients.Fetch(o => o.PatientRecords)
						select new { PatientRecords = o.PatientRecords.ToList() };

the same applies for other queries that were added by this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I should just add the test from the original bug report. Since doing a Fetch when projecting a collection is actually redundant. Adding that one test will guard against a regression for this code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I should just add the test from the original bug report

Fine by me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also ok with that.


var result = query.ToList();
Assert.That(NHibernateUtil.IsInitialized(result[0].OrderLines), Is.True);
Assert.That(result.Count, Is.EqualTo(830));
}

Expand All @@ -292,9 +369,31 @@ 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
var query = from o in db.Orders.Fetch(x => x.OrderLines)
select new ExpandedWrapper<Order, ISet<OrderLine>>
{
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()
{
Expand Down