forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyEntityPropertyRefFixture.cs
114 lines (98 loc) · 2.73 KB
/
KeyEntityPropertyRefFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;
namespace NHibernate.Test.PropertyRef
{
[TestFixture]
public class KeyEntityPropertyRefFixture : TestCase
{
protected override string[] Mappings => new string[] { "PropertyRef.KeyEntityPropertyRef.hbm.xml" };
protected override string MappingsAssembly => "NHibernate.Test";
private int _aWithItemsId;
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var c1 = new C { Name = "Third" };
s.Save(c1);
var c2 = new C { Name = "ThirdBis" };
s.Save(c2);
var b1 = new B { Id = 1, Name = "SecondC1" };
var b2 = new B { Id = 2, Name = "SecondC2" };
s.Save(b1);
s.Save(b2);
var a = new A { Id = 3, Name = "First", C = c1 };
a.CItems.Add(b1);
a.CItems.Add(b2);
_aWithItemsId = (int) s.Save(a);
s.Save(new A { Id = 4, Name = "FirstBis", C = c2 });
t.Commit();
}
}
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from B").ExecuteUpdate();
s.CreateQuery("delete from A").ExecuteUpdate();
s.CreateQuery("delete from C").ExecuteUpdate();
t.Commit();
}
}
[Test]
public void PropertyRefLazyLoad()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var a = s.Get<A>(_aWithItemsId);
Assert.That(NHibernateUtil.IsInitialized(a.CItems), Is.False, "a with items");
Assert.That(a.CItems, Has.Count.EqualTo(2), "a with items");
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
s
.Query<A>()
.Single(a => a.Id != _aWithItemsId);
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.False, "a without items");
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
t.Commit();
}
}
[Test]
public void PropertyRefEagerLoad()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
s
.Query<A>()
.Where(a => a.Id == _aWithItemsId)
.FetchMany(a => a.CItems)
.Single();
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a with items");
Assert.That(entity.CItems, Has.Count.EqualTo(2), "a with items");
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
s
.Query<A>()
.Where(a => a.Id != _aWithItemsId)
.FetchMany(a => a.CItems)
.Single();
Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a without items");
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
t.Commit();
}
}
}
}