Skip to content

Commit a9a728b

Browse files
MarkPerryBVfredericDelaporte
authored andcommitted
Test case for ComposedId Entity with Lazy Property is not proxified (#1440)
Test case for Issue #1439
1 parent 61d9fda commit a9a728b

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg.MappingSchema;
13+
using NHibernate.Intercept;
14+
using NHibernate.Mapping.ByCode;
15+
using NUnit.Framework;
16+
using NHibernate.Linq;
17+
18+
namespace NHibernate.Test.NHSpecificTest.GH1439
19+
{
20+
using System.Threading.Tasks;
21+
[TestFixture]
22+
public class FixtureAsync : TestCaseMappingByCode
23+
{
24+
protected override HbmMapping GetMappings()
25+
{
26+
var mapper = new ModelMapper();
27+
28+
mapper.Class<CompositeEntity>(rc =>
29+
{
30+
rc.ComposedId(
31+
c =>
32+
{
33+
c.Property(t => t.Id);
34+
c.Property(t => t.Name);
35+
});
36+
37+
rc.Property(x => x.LazyProperty, x => x.Lazy(true));
38+
});
39+
40+
mapper.Class<EntityWithComponentId>(rc =>
41+
{
42+
rc.ComponentAsId(e => e.Id, map =>
43+
{
44+
map.Property(c => c.Id);
45+
map.Property(c => c.Name);
46+
});
47+
48+
rc.Property(e => e.LazyProperty, map => map.Lazy(true));
49+
});
50+
51+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
52+
}
53+
54+
protected override void OnSetUp()
55+
{
56+
using (var session = OpenSession())
57+
using (var transaction = session.BeginTransaction())
58+
{
59+
var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"};
60+
session.Save(e1);
61+
var e2 = new EntityWithComponentId
62+
{ Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" };
63+
session.Save(e2);
64+
transaction.Commit();
65+
}
66+
}
67+
68+
protected override void OnTearDown()
69+
{
70+
using (var session = OpenSession())
71+
using (var transaction = session.BeginTransaction())
72+
{
73+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
74+
transaction.Commit();
75+
}
76+
}
77+
78+
[Test]
79+
[KnownBug("#1439")]
80+
public async Task LazyPropertyShouldBeUninitializedAndLoadableAsync()
81+
{
82+
using (var session = OpenSession())
83+
using (var tran = session.BeginTransaction())
84+
{
85+
var e1 = await (session.Query<CompositeEntity>().SingleAsync());
86+
Assert.Multiple(
87+
() =>
88+
{
89+
Assert.That(
90+
NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)),
91+
Is.False,
92+
"Lazy property initialization status");
93+
Assert.That(
94+
FieldInterceptionHelper.IsInstrumented(e1),
95+
Is.True,
96+
"Entity IsInstrumented");
97+
Assert.That(
98+
e1,
99+
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
100+
});
101+
await (tran.CommitAsync());
102+
}
103+
}
104+
105+
[Test]
106+
public async Task LazyPropertyShouldBeUninitializedAndLoadableWithComponentIdAsync()
107+
{
108+
using (var session = OpenSession())
109+
using (var tran = session.BeginTransaction())
110+
{
111+
var e2 = await (session.Query<EntityWithComponentId>().SingleAsync());
112+
Assert.Multiple(
113+
() =>
114+
{
115+
Assert.That(
116+
NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)),
117+
Is.False,
118+
"Lazy property initialization status");
119+
Assert.That(
120+
FieldInterceptionHelper.IsInstrumented(e2),
121+
Is.True,
122+
"Entity IsInstrumented");
123+
Assert.That(
124+
e2,
125+
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
126+
});
127+
await (tran.CommitAsync());
128+
}
129+
}
130+
}
131+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1439
4+
{
5+
public class CompositeEntity : IEquatable<CompositeEntity>
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual string LazyProperty { get; set; }
10+
11+
public virtual bool Equals(CompositeEntity other)
12+
{
13+
if (ReferenceEquals(null, other)) return false;
14+
if (ReferenceEquals(this, other)) return true;
15+
return Id == other.Id && string.Equals(Name, other.Name);
16+
}
17+
18+
public override bool Equals(object obj)
19+
{
20+
return Equals(obj as CompositeEntity);
21+
}
22+
23+
public override int GetHashCode()
24+
{
25+
unchecked
26+
{
27+
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
28+
}
29+
}
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1439
4+
{
5+
public class EntityWithComponentId
6+
{
7+
public virtual ComponentId Id { get; set; }
8+
public virtual string LazyProperty { get; set; }
9+
}
10+
11+
public class ComponentId : IEquatable<ComponentId>
12+
{
13+
public int Id { get; set; }
14+
public string Name { get; set; }
15+
16+
public bool Equals(ComponentId other)
17+
{
18+
if (ReferenceEquals(null, other)) return false;
19+
if (ReferenceEquals(this, other)) return true;
20+
return Id == other.Id && string.Equals(Name, other.Name);
21+
}
22+
23+
public override bool Equals(object obj)
24+
{
25+
return Equals(obj as ComponentId);
26+
}
27+
28+
public override int GetHashCode()
29+
{
30+
unchecked
31+
{
32+
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
33+
}
34+
}
35+
}
36+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Intercept;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH1439
8+
{
9+
[TestFixture]
10+
public class Fixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
16+
mapper.Class<CompositeEntity>(rc =>
17+
{
18+
rc.ComposedId(
19+
c =>
20+
{
21+
c.Property(t => t.Id);
22+
c.Property(t => t.Name);
23+
});
24+
25+
rc.Property(x => x.LazyProperty, x => x.Lazy(true));
26+
});
27+
28+
mapper.Class<EntityWithComponentId>(rc =>
29+
{
30+
rc.ComponentAsId(e => e.Id, map =>
31+
{
32+
map.Property(c => c.Id);
33+
map.Property(c => c.Name);
34+
});
35+
36+
rc.Property(e => e.LazyProperty, map => map.Lazy(true));
37+
});
38+
39+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
40+
}
41+
42+
protected override void OnSetUp()
43+
{
44+
using (var session = OpenSession())
45+
using (var transaction = session.BeginTransaction())
46+
{
47+
var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"};
48+
session.Save(e1);
49+
var e2 = new EntityWithComponentId
50+
{ Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" };
51+
session.Save(e2);
52+
transaction.Commit();
53+
}
54+
}
55+
56+
protected override void OnTearDown()
57+
{
58+
using (var session = OpenSession())
59+
using (var transaction = session.BeginTransaction())
60+
{
61+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
62+
transaction.Commit();
63+
}
64+
}
65+
66+
[Test]
67+
[KnownBug("#1439")]
68+
public void LazyPropertyShouldBeUninitializedAndLoadable()
69+
{
70+
using (var session = OpenSession())
71+
using (var tran = session.BeginTransaction())
72+
{
73+
var e1 = session.Query<CompositeEntity>().Single();
74+
Assert.Multiple(
75+
() =>
76+
{
77+
Assert.That(
78+
NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)),
79+
Is.False,
80+
"Lazy property initialization status");
81+
Assert.That(
82+
FieldInterceptionHelper.IsInstrumented(e1),
83+
Is.True,
84+
"Entity IsInstrumented");
85+
Assert.That(
86+
e1,
87+
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
88+
});
89+
tran.Commit();
90+
}
91+
}
92+
93+
[Test]
94+
public void LazyPropertyShouldBeUninitializedAndLoadableWithComponentId()
95+
{
96+
using (var session = OpenSession())
97+
using (var tran = session.BeginTransaction())
98+
{
99+
var e2 = session.Query<EntityWithComponentId>().Single();
100+
Assert.Multiple(
101+
() =>
102+
{
103+
Assert.That(
104+
NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)),
105+
Is.False,
106+
"Lazy property initialization status");
107+
Assert.That(
108+
FieldInterceptionHelper.IsInstrumented(e2),
109+
Is.True,
110+
"Entity IsInstrumented");
111+
Assert.That(
112+
e2,
113+
Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty"));
114+
});
115+
tran.Commit();
116+
}
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)