Skip to content

NH-3727 - Unit tests showing bug and bugfix proposition #364

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3727/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3727
{
class Entity
{
public virtual int Id { get; set; }
}
}
93 changes: 93 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3727/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Criterion;
using NHibernate.Impl;
using NHibernate.Mapping.ByCode;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3727
{
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

[Test]
public void QueryOverWithSubqueryProjectionCanBeExecutedMoreThanOnce()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
const int parameter1 = 111;

var countSubquery = QueryOver.Of<Entity>()
.Where(x => x.Id == parameter1) //any condition which makes output SQL has parameter
.Select(Projections.RowCount())
;

var originalQueryOver = session.QueryOver<Entity>()
.SelectList(l => l
.Select(x => x.Id)
.SelectSubQuery(countSubquery)
)
.TransformUsing(Transformers.ToList);

originalQueryOver.List<object>();

Assert.DoesNotThrow(() => originalQueryOver.List<object>(), "Second try to execute QueryOver thrown exception.");
}
}

[Test]
public void ClonedQueryOverExecutionMakesOriginalQueryOverNotWorking()
{
// Projections are copied by clone operation.
// SubqueryProjection use SubqueryExpression which holds CriteriaQueryTranslator (class SubqueryExpression { private CriteriaQueryTranslator innerQuery; })
// So given CriteriaQueryTranslator is used twice.
// Since CriteriaQueryTranslator has CollectedParameters collection, second execution of the Criteria does not fit SqlCommand parameters.

using (ISession session = OpenSession())
using (session.BeginTransaction())
{
const int parameter1 = 111;

var countSubquery = QueryOver.Of<Entity>()
.Where(x => x.Id == parameter1) //any condition which makes output SQL has parameter
.Select(Projections.RowCount())
;

var originalQueryOver = session.QueryOver<Entity>()
//.Where(x => x.Id == parameter2)
.SelectList(l => l
.Select(x => x.Id)
.SelectSubQuery(countSubquery)
)
.TransformUsing(Transformers.ToList);

var clonedQueryOver = originalQueryOver.Clone();
clonedQueryOver.List<object>();

Assert.DoesNotThrow(() => originalQueryOver.List<object>(), "Cloned QueryOver execution caused source QueryOver throw exception when executed.");
}
}

private static IEnumerable<IProjection> GetProjectionList(IQueryOver<Entity, Entity> clonedQueryOver)
{
var projectionList = (((CriteriaImpl)clonedQueryOver.RootCriteria).Projection as ProjectionList);
for (int i = 0; i < projectionList.Length; i++)
{
yield return projectionList[i];
}
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3604\Entity.cs" />
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3727\Entity.cs" />
<Compile Include="NHSpecificTest\NH3727\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />
Expand Down
14 changes: 7 additions & 7 deletions src/NHibernate/Criterion/SubqueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class SubqueryExpression : AbstractCriterion
[NonSerialized] private CriteriaQueryTranslator innerQuery;

protected SubqueryExpression(String op, String quantifier, DetachedCriteria dc)
:this(op, quantifier, dc, true)
: this(op, quantifier, dc, true)
{
}

Expand All @@ -44,7 +44,7 @@ public IType[] GetTypes()

public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
{
InitializeInnerQueryAndParameters(criteriaQuery);
InitializeInnerQueryAndParameters(criteriaQuery, true); // Force reinitialization. There can be old instance from earlier Criteria execution or cloned Criteria.

if (innerQuery.HasProjection == false)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteri
{
buf.Add(quantifier).Add(" ");
}

buf.Add("(").Add(sql).Add(")");

if (quantifier != null && prefixOp == false)
Expand All @@ -101,9 +101,9 @@ public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteri

public override string ToString()
{
if(prefixOp)
if (prefixOp)
return string.Format("{0} {1} ({2})", op, quantifier, criteriaImpl);

return string.Format("{0} ({1}) {2}", op, criteriaImpl, quantifier);
}

Expand All @@ -117,9 +117,9 @@ public override IProjection[] GetProjections()
return null;
}

public void InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery)
public void InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery, bool forceReinitialization = false)
Copy link
Member

Choose a reason for hiding this comment

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

Can you please make usual method overload?

{
if (innerQuery == null)
if (innerQuery == null || forceReinitialization)
{
ISessionFactoryImplementor factory = criteriaQuery.Factory;

Expand Down