Skip to content

Commit 19eb0d2

Browse files
csharper2010oskarb
authored andcommitted
Unit Test for FetchMany and ThenFetchMany in Stateless Session
Signed-off-by: CSharper2010 <[email protected]>
1 parent 9262202 commit 19eb0d2

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,13 +1233,15 @@
12331233
<Compile Include="RecordingInterceptor.cs" />
12341234
<Compile Include="Stateless\Contact.cs" />
12351235
<Compile Include="Stateless\Country.cs" />
1236+
<Compile Include="Stateless\FetchingLazyCollections\TreeFetchTests.cs" />
12361237
<Compile Include="Stateless\FetchingLazyCollections\LazyCollectionFetchTests.cs" />
12371238
<Compile Include="Stateless\Fetching\Resource.cs" />
12381239
<Compile Include="Stateless\Fetching\StatelessSessionFetchingTest.cs" />
12391240
<Compile Include="Stateless\Fetching\Task.cs" />
12401241
<Compile Include="Stateless\Fetching\User.cs" />
12411242
<Compile Include="Stateless\Org.cs" />
12421243
<Compile Include="Stateless\StatelessSessionQueryFixture.cs" />
1244+
<Compile Include="Stateless\TreeNode.cs" />
12431245
<Compile Include="Subselect\ClassSubselectFixture.cs" />
12441246
<Compile Include="Subselect\Domain.cs" />
12451247
<Compile Include="TestCaseMappingByCode.cs" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NHibernate.Cfg.MappingSchema;
5+
using NHibernate.Mapping.ByCode;
6+
using NHibernate.Mapping.ByCode.Conformist;
7+
using NHibernate.Linq;
8+
using NUnit.Framework;
9+
using SharpTestsEx;
10+
11+
namespace NHibernate.Test.Stateless.FetchingLazyCollections
12+
{
13+
public class TreeFetchTests : TestCaseMappingByCode
14+
{
15+
protected override HbmMapping GetMappings()
16+
{
17+
var mapper = new ModelMapper();
18+
mapper.BeforeMapClass += (mi, t, cm) => cm.Id(im => im.Generator(Generators.HighLow));
19+
mapper.Class<TreeNode>(
20+
mc =>
21+
{
22+
mc.Id(x => x.Id);
23+
mc.Property(x => x.Content);
24+
mc.Set(x => x.Children, cam =>
25+
{
26+
cam.Key(km => km.Column("parentId"));
27+
cam.Cascade(Mapping.ByCode.Cascade.All);
28+
}, rel => rel.OneToMany());
29+
});
30+
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
31+
return mappings;
32+
}
33+
34+
[Test]
35+
public void FetchMultipleHierarchies()
36+
{
37+
using (ISession s = sessions.OpenSession())
38+
using (ITransaction tx = s.BeginTransaction())
39+
{
40+
var root = new TreeNode { Content = "Root" };
41+
var child1 = new TreeNode { Content = "Child1" };
42+
root.Children.Add(child1);
43+
root.Children.Add(new TreeNode { Content = "Child2" });
44+
child1.Children.Add(new TreeNode { Content = "Child1Child1" });
45+
child1.Children.Add(new TreeNode { Content = "Child1Child2" });
46+
s.Save(root);
47+
tx.Commit();
48+
}
49+
50+
using (IStatelessSession s = sessions.OpenStatelessSession())
51+
using (ITransaction tx = s.BeginTransaction())
52+
{
53+
IList<TreeNode> rootNodes = s.Query<TreeNode>().Where(t => t.Content == "Root")
54+
.FetchMany(f => f.Children)
55+
.ThenFetchMany(f => f.Children).ToList();
56+
Assert.That(rootNodes.Count, Is.EqualTo(1));
57+
Assert.That(rootNodes.First().Children.Count, Is.EqualTo(2));
58+
59+
tx.Commit();
60+
}
61+
62+
using (ISession s = sessions.OpenSession())
63+
using (ITransaction tx = s.BeginTransaction())
64+
{
65+
s.Delete("from TreeNode");
66+
tx.Commit();
67+
}
68+
}
69+
}
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
4+
namespace NHibernate.Test.Stateless
5+
{
6+
public class TreeNode
7+
{
8+
private ISet<TreeNode> children = new HashSet<TreeNode>();
9+
10+
public virtual int Id { get; protected set; }
11+
12+
public virtual string Content { get; set; }
13+
14+
public virtual ISet<TreeNode> Children
15+
{
16+
get { return children; }
17+
protected set { children = value; }
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)