Skip to content

Commit 2986d64

Browse files
owerkopigitur
authored andcommitted
NH-3864 Testcase. Failing tests.
# Conflicts: # src/NHibernate.Test/NHibernate.Test.csproj
1 parent af319b6 commit 2986d64

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using NUnit.Framework;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3864
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
Clear2ndLevelCache();
13+
using (ISession s = OpenSession())
14+
using (ITransaction tx = s.BeginTransaction())
15+
{
16+
var p1 = new Person() { Name = "A" };
17+
var p1c1 = new Person() { Name = "AA" };
18+
var p1c2 = new Person() { Name = "AB" };
19+
var p1c3 = new Person() { Name = "AC" };
20+
p1.Children = new HashSet<Person>(new[] { p1c1, p1c2, p1c3 });
21+
s.Save(p1);
22+
23+
var p2 = new Person() { Name = "B" };
24+
var p2c1 = new Person() { Name = "BA" };
25+
var p2c2 = new Person() { Name = "BB" };
26+
var p2c3 = new Person() { Name = "BC" };
27+
p2.Children = new HashSet<Person>(new[] { p2c1, p2c2, p2c3 });
28+
s.Save(p2);
29+
30+
tx.Commit();
31+
}
32+
}
33+
34+
protected override void OnTearDown()
35+
{
36+
base.OnTearDown();
37+
using (ISession s = OpenSession())
38+
using (ITransaction tx = s.BeginTransaction())
39+
{
40+
s.Delete("from Person");
41+
tx.Commit();
42+
}
43+
}
44+
45+
[Test]
46+
public void CacheableMulticriteria_QueryOverWithAliasedJoinQueryOver()
47+
{
48+
ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() =>
49+
{
50+
using (var s = Sfi.OpenSession())
51+
{
52+
var query = CreateQueryOverWithAliasedJoinQueryOver(s);
53+
54+
var multiCriteria = s.CreateMultiCriteria();
55+
multiCriteria.Add("myQuery", query);
56+
multiCriteria.SetCacheable(true);
57+
58+
var list = (IList<Person>)multiCriteria.GetResult("myQuery");
59+
AssertQueryResult(list);
60+
}
61+
});
62+
}
63+
64+
[Test]
65+
public void CacheableFuture_QueryOverWithAliasedJoinQueryOver()
66+
{
67+
ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() =>
68+
{
69+
using (var s = Sfi.OpenSession())
70+
{
71+
var query = CreateQueryOverWithAliasedJoinQueryOver(s)
72+
.Cacheable()
73+
.Future();
74+
75+
var list = query.ToList();
76+
AssertQueryResult(list);
77+
}
78+
});
79+
}
80+
81+
[Test]
82+
public void CacheableMulticriteria_QueryOverWithJoinAlias()
83+
{
84+
ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() =>
85+
{
86+
using (var s = Sfi.OpenSession())
87+
{
88+
var query = CreateQueryOverWithJoinAlias(s);
89+
90+
var multiCriteria = s.CreateMultiCriteria();
91+
multiCriteria.Add("myQuery", query);
92+
multiCriteria.SetCacheable(true);
93+
94+
var list = (IList<Person>)multiCriteria.GetResult("myQuery");
95+
AssertQueryResult(list);
96+
}
97+
});
98+
}
99+
100+
[Test]
101+
public void CacheableFuture_QueryOverWithJoinAlias()
102+
{
103+
ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() =>
104+
{
105+
using (var s = Sfi.OpenSession())
106+
{
107+
var query = CreateQueryOverWithJoinAlias(s)
108+
.Cacheable()
109+
.Future();
110+
111+
var list = query.ToList();
112+
AssertQueryResult(list);
113+
}
114+
});
115+
}
116+
117+
private static void AssertQueryResult(IList<Person> list)
118+
{
119+
Assert.AreEqual(6, list.Count, "Returned records count is wrong.");
120+
var person1 = list.FirstOrDefault(p => p.Name == "A");
121+
Assert.NotNull(person1);
122+
var person2 = list.FirstOrDefault(p => p.Name == "B");
123+
Assert.NotNull(person2);
124+
125+
CollectionAssert.AreEquivalent(person1.Children.Select(c => c.Name), new[] { "AA", "AB", "AC" });
126+
CollectionAssert.AreEquivalent(person2.Children.Select(c => c.Name), new[] { "BA", "BB", "BC" });
127+
}
128+
129+
private static IQueryOver<Person, Person> CreateQueryOverWithJoinAlias(ISession session)
130+
{
131+
Person childAlias = null;
132+
return session.QueryOver<Person>()
133+
.Where(p => p.Parent == null)
134+
.JoinAlias(x => x.Children, () => childAlias);
135+
}
136+
137+
private static IQueryOver<Person, Person> CreateQueryOverWithAliasedJoinQueryOver(ISession session)
138+
{
139+
Person childAlias = null;
140+
return session.QueryOver<Person>()
141+
.Where(p => p.Parent == null)
142+
.JoinQueryOver(x => x.Children, () => childAlias);
143+
}
144+
145+
private void Clear2ndLevelCache()
146+
{
147+
Sfi.EvictQueries();
148+
Sfi.Evict(typeof(Person));
149+
}
150+
151+
private static void ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(System.Action action)
152+
{
153+
action();
154+
EnsureNoSqlExecutedOnDb(action);
155+
}
156+
157+
private static void EnsureNoSqlExecutedOnDb(System.Action action)
158+
{
159+
using (var sqlLogSpy = new SqlLogSpy())
160+
{
161+
action();
162+
if (!string.IsNullOrEmpty(sqlLogSpy.GetWholeLog()))
163+
{
164+
Assert.Fail("SQL executed. 2nd level cache should be used instead.");
165+
}
166+
}
167+
}
168+
}
169+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3864"
5+
assembly="NHibernate.Test">
6+
7+
<class name="Person">
8+
<id name="Id">
9+
<generator class="native" />
10+
</id>
11+
<property name="Name" />
12+
<many-to-one name="Parent" column="parent_id" />
13+
<set name="Children" cascade="all">
14+
<key column="parent_id" />
15+
<one-to-many class="Person" />
16+
</set>
17+
</class>
18+
</hibernate-mapping>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3864
4+
{
5+
public class Person
6+
{
7+
public virtual int Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
11+
public virtual Person Parent { get; set; }
12+
13+
public virtual ISet<Person> Children { get; set; }
14+
}
15+
}

0 commit comments

Comments
 (0)