-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathLazyPropertyFixture.cs
157 lines (132 loc) · 3.59 KB
/
LazyPropertyFixture.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
namespace NHibernate.Test.LazyProperty
{
[TestFixture]
public class LazyPropertyFixture : TestCase
{
private string log;
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override IList Mappings
{
get { return new[] { "LazyProperty.Mappings.hbm.xml" }; }
}
protected override void BuildSessionFactory()
{
using (var logSpy = new LogSpy(typeof(EntityMetamodel)))
{
base.BuildSessionFactory();
log = logSpy.GetWholeLog();
}
}
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Persist(new Book
{
Name = "some name",
Id = 1,
ALotOfText = "a lot of text ..."
});
tx.Commit();
}
}
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
Assert.That(s.CreateSQLQuery("delete from Book").ExecuteUpdate(), Is.EqualTo(1));
tx.Commit();
}
}
[Test]
public void PropertyLoadedNotInitialized()
{
using (ISession s = OpenSession())
{
var book = s.Load<Book>(1);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
NHibernateUtil.Initialize(book);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
}
}
[Test]
public void ShouldGenerateErrorForNonAutoPropLazyProp()
{
Assert.IsTrue(log.Contains("NHibernate.Test.LazyProperty.Book.ALotOfText is not an auto property, which may result in uninitialized property access"));
}
[Test]
public void PropertyLoadedNotInitializedWhenUsingGet()
{
using (ISession s = OpenSession())
{
var book = s.Get<Book>(1);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
}
}
[Test]
public void CanGetValueForLazyProperty()
{
using (ISession s = OpenSession())
{
var book = s.Get<Book>(1);
Assert.That(book.ALotOfText, Is.EqualTo("a lot of text ..."));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
}
}
[Test]
public void CanGetValueForNonLazyProperty()
{
using (ISession s = OpenSession())
{
var book = s.Get<Book>(1);
Assert.That(book.Name, Is.EqualTo("some name"));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
}
}
[Test]
public void CanLoadAndSaveObjectInDifferentSessions()
{
Book book;
using (ISession s = OpenSession())
{
book = s.Get<Book>(1);
}
using (ISession s = OpenSession())
{
s.Merge(book);
}
}
[Test]
public void CanUpdateNonLazyWithoutLoadingLazyProperty()
{
Book book;
using (ISession s = OpenSession())
using (var trans = s.BeginTransaction())
{
book = s.Get<Book>(1);
book.Name += "updated";
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
trans.Commit();
}
using (ISession s = OpenSession())
{
book = s.Get<Book>(1);
Assert.That(book.Name, Is.EqualTo("some nameupdated"));
}
}
}
}