Skip to content

Commit e240ea4

Browse files
committed
Add test for Get + lazy loading
1 parent d059c9e commit e240ea4

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/NHibernate.Test/NHSpecificTest/GH3176/FixtureByCode.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88

99
namespace NHibernate.Test.NHSpecificTest.GH3176
1010
{
11-
[TestFixture]
11+
[TestFixture(CacheFactory.ReadOnly)]
12+
[TestFixture(CacheFactory.NonstrictReadWrite)]
13+
[TestFixture(CacheFactory.ReadWrite)]
1214
public class ByCodeFixture : TestCaseMappingByCode
1315
{
16+
public ByCodeFixture(string cacheStrategy)
17+
{
18+
CacheConcurrencyStrategy = cacheStrategy;
19+
}
20+
21+
protected override string CacheConcurrencyStrategy { get; }
22+
23+
private int _id;
24+
1425
protected override HbmMapping GetMappings()
1526
{
1627
var mapper = new ModelMapper();
@@ -26,7 +37,6 @@ protected override HbmMapping GetMappings()
2637
m.Property(x => x.Field);
2738
m.Lazy(true);
2839
});
29-
rc.Cache(x => x.Usage(CacheUsage.ReadWrite));
3040
});
3141

3242
return mapper.CompileMappingForAllExplicitlyAddedEntities();
@@ -47,6 +57,7 @@ protected override void OnSetUp()
4757
{
4858
var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } };
4959
session.Save(e1);
60+
_id = e1.Id;
5061

5162
var e2 = new Entity { Name = "Sally" };
5263
session.Save(e2);
@@ -96,6 +107,32 @@ public void TestPreLoadedData()
96107
}
97108
}
98109

110+
[Test]
111+
public void InitializedLazyPropertyShouldBeCached()
112+
{
113+
using (var session = OpenSession())
114+
using (var transaction = session.BeginTransaction())
115+
{
116+
var e = session.Get<Entity>(_id);
117+
Assert.That(e.Component?.Field, Is.EqualTo("Jim"));
118+
119+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True);
120+
transaction.Commit();
121+
}
122+
123+
using (var session = OpenSession())
124+
using (var transaction = session.BeginTransaction())
125+
{
126+
var e = session.Get<Entity>(_id);
127+
128+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True, "Lazy property is not cached");
129+
var field = e.Component?.Field;
130+
131+
Assert.That(field, Is.EqualTo("Jim"));
132+
transaction.Commit();
133+
}
134+
}
135+
99136
[Test]
100137
public void TestNonPreLoadedData()
101138
{

0 commit comments

Comments
 (0)