Skip to content

Commit f16e9b5

Browse files
itmaginationhazzik
authored andcommitted
NH-3727 - Add test case
1 parent 5225d8f commit f16e9b5

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3727
2+
{
3+
class Entity
4+
{
5+
public virtual int Id { get; set; }
6+
}
7+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Criterion;
5+
using NHibernate.Mapping.ByCode;
6+
using NHibernate.Transform;
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.NHSpecificTest.NH3727
10+
{
11+
public class ByCodeFixture : TestCaseMappingByCode
12+
{
13+
protected override HbmMapping GetMappings()
14+
{
15+
var mapper = new ModelMapper();
16+
mapper.Class<Entity>(rc =>
17+
{
18+
rc.Id(x => x.Id);
19+
});
20+
21+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
22+
}
23+
24+
[Test]
25+
public void QueryOverWithSubqueryProjectionCanBeExecutedMoreThanOnce()
26+
{
27+
using (ISession session = OpenSession())
28+
using (session.BeginTransaction())
29+
{
30+
const int parameter1 = 111;
31+
32+
var countSubquery = QueryOver.Of<Entity>()
33+
.Where(x => x.Id == parameter1) //any condition which makes output SQL has parameter
34+
.Select(Projections.RowCount())
35+
;
36+
37+
var originalQueryOver = session.QueryOver<Entity>()
38+
.SelectList(l => l
39+
.Select(x => x.Id)
40+
.SelectSubQuery(countSubquery)
41+
)
42+
.TransformUsing(Transformers.ToList);
43+
44+
var objects = originalQueryOver.List<object>();
45+
46+
Assert.DoesNotThrow(() => originalQueryOver.List<object>(), "Second try to execute QueryOver thrown exception.");
47+
}
48+
}
49+
50+
[Test]
51+
public void ClonedQueryOverExecutionMakesOriginalQueryOverNotWorking()
52+
{
53+
// Projections are copied by clone operation.
54+
// SubqueryProjection use SubqueryExpression which holds CriteriaQueryTranslator (class SubqueryExpression { private CriteriaQueryTranslator innerQuery; })
55+
// So given CriteriaQueryTranslator is used twice.
56+
// Since CriteriaQueryTranslator has CollectedParameters collection, second execution of the Criteria does not fit SqlCommand parameters.
57+
58+
using (ISession session = OpenSession())
59+
using (session.BeginTransaction())
60+
{
61+
const int parameter1 = 111;
62+
63+
var countSubquery = QueryOver.Of<Entity>()
64+
.Where(x => x.Id == parameter1) //any condition which makes output SQL has parameter
65+
.Select(Projections.RowCount())
66+
;
67+
68+
var originalQueryOver = session.QueryOver<Entity>()
69+
//.Where(x => x.Id == parameter2)
70+
.SelectList(l => l
71+
.Select(x => x.Id)
72+
.SelectSubQuery(countSubquery)
73+
)
74+
.TransformUsing(Transformers.ToList);
75+
76+
var clonedQueryOver = originalQueryOver.Clone();
77+
clonedQueryOver.List<object>();
78+
79+
Assert.DoesNotThrow(() => originalQueryOver.List<object>(), "Cloned QueryOver execution caused source QueryOver throw exception when executed.");
80+
}
81+
}
82+
}
83+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@
863863
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
864864
<Compile Include="NHSpecificTest\NH3754\Fixture.cs" />
865865
<Compile Include="NHSpecificTest\NH3754\Model.cs" />
866+
<Compile Include="NHSpecificTest\NH3727\Entity.cs" />
867+
<Compile Include="NHSpecificTest\NH3727\FixtureByCode.cs" />
866868
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
867869
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
868870
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />

0 commit comments

Comments
 (0)