Skip to content

Commit 2f842b5

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH2201: Add tests for checking if fetch loops is enabled/disabled.
1 parent ceb7a9e commit 2f842b5

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2201
2+
{
3+
public class Detail
4+
{
5+
public virtual int Id { get; protected set; }
6+
public virtual Person Person { get; set; }
7+
public virtual string Data { get; set; }
8+
}
9+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NHibernate.Stat;
5+
using NUnit.Framework;
6+
using NHCfg = NHibernate.Cfg;
7+
8+
namespace NHibernate.Test.NHSpecificTest.GH2201
9+
{
10+
[TestFixture]
11+
public class Fixture : BugTestCase
12+
{
13+
protected override void Configure(NHCfg.Configuration configuration)
14+
{
15+
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
16+
}
17+
18+
protected override void OnTearDown()
19+
{
20+
using (var s = OpenSession())
21+
using (var tx = s.BeginTransaction())
22+
{
23+
s.Delete("from Person");
24+
25+
tx.Commit();
26+
}
27+
}
28+
29+
protected override void OnSetUp()
30+
{
31+
using (var s = OpenSession())
32+
using (var tx = s.BeginTransaction())
33+
{
34+
string[] names = { "Alice", "Bob" };
35+
36+
for (int i = 0; i < names.Length; i++)
37+
{
38+
var name = names[i];
39+
40+
Person parent = new Person()
41+
{
42+
Name = name,
43+
Details = new Detail()
44+
{
45+
Data = $"Details for ${name}"
46+
}
47+
};
48+
49+
for (int j = 1; j <= 3; j++)
50+
{
51+
Person child = new Person()
52+
{
53+
Name = $"Child ${j} of ${parent.Name}",
54+
Parent = parent,
55+
Details = new Detail()
56+
{
57+
Data = $"Details for child ${j} of ${name}"
58+
}
59+
};
60+
61+
parent.Children.Add(child);
62+
}
63+
64+
s.Save(parent);
65+
}
66+
67+
tx.Commit();
68+
}
69+
}
70+
71+
[Test]
72+
public void QueryOverPersonWithParent()
73+
{
74+
var stats = Sfi.Statistics;
75+
76+
stats.Clear();
77+
78+
using (var s = OpenSession())
79+
using (var tx = s.BeginTransaction())
80+
{
81+
var people = s.QueryOver<Person>()
82+
.Fetch(SelectMode.Fetch,p => p.Parent)
83+
.Where(p => p.Parent != null)
84+
.List();
85+
86+
foreach (Person p in people)
87+
{
88+
Assert.That(p.Parent, Is.Not.Null);
89+
Assert.That(p.Parent.Details, Is.Not.Null);
90+
}
91+
92+
Assert.That(people.Count, Is.EqualTo(6));
93+
Assert.That(stats.QueryExecutionCount, Is.EqualTo(1));
94+
Assert.That(stats.EntityFetchCount, Is.EqualTo(0));
95+
Assert.That(stats.EntityLoadCount, Is.EqualTo(16));
96+
}
97+
}
98+
99+
[Test]
100+
public void QueryOverSinglePersonWithParent()
101+
{
102+
var stats = Sfi.Statistics;
103+
104+
stats.Clear();
105+
106+
using (var s = OpenSession())
107+
using (var tx = s.BeginTransaction())
108+
{
109+
var person = s.QueryOver<Person>()
110+
.Where(p => p.Parent != null)
111+
.Fetch(SelectMode.Fetch, p => p.Parent)
112+
.Take(1)
113+
.SingleOrDefault();
114+
115+
Assert.That(person, Is.Not.Null);
116+
Assert.That(stats.QueryExecutionCount, Is.EqualTo(1));
117+
Assert.That(stats.EntityFetchCount, Is.EqualTo(0));
118+
Assert.That(stats.EntityLoadCount, Is.EqualTo(4));
119+
}
120+
}
121+
}
122+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH2201">
3+
<class name="Person" table="Persons">
4+
<id name="Id" column="personId" unsaved-value="0" generator="native"/>
5+
<property name="Name" column="name"/>
6+
<set name="Children" table="Persons" lazy="true" cascade="all" inverse="true">
7+
<key column="prarentId" not-null="false"/>
8+
<one-to-many class="Person"/>
9+
</set>
10+
<many-to-one name="Parent" class="Person" column="parentId" not-null="false"/>
11+
<one-to-one name="Details" class="Detail" fetch="join" cascade="all-delete-orphan"/>
12+
</class>
13+
14+
<class name="Detail" table="Details">
15+
<id name="Id" column="personId">
16+
<generator class="foreign">
17+
<param name="property">Person</param>
18+
</generator>
19+
</id>
20+
<one-to-one name="Person" class="Person" constrained="true"/>
21+
<property name="Data"/>
22+
</class>
23+
</hibernate-mapping>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2201
4+
{
5+
public class Order
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual Person Person { get; set; }
9+
public virtual DateTime Date { get; set; }
10+
}
11+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2201
4+
{
5+
public class Person
6+
{
7+
private Detail _detail;
8+
9+
public Person()
10+
{
11+
this.Children = new HashSet<Person>();
12+
}
13+
14+
public virtual int Id { get; set; }
15+
public virtual string Name { get; set; }
16+
public virtual Person Parent { get; set; }
17+
public virtual ISet<Person> Children { get; protected set; }
18+
public virtual Detail Details
19+
{
20+
get { return _detail; }
21+
set
22+
{
23+
_detail = value;
24+
25+
if (_detail != null)
26+
{
27+
_detail.Person = this;
28+
}
29+
}
30+
}
31+
32+
}
33+
}

0 commit comments

Comments
 (0)