Skip to content

Commit 4037bdd

Browse files
Add test for wrong proxy generation
nhibernate#2052
1 parent 21846c0 commit 4037bdd

File tree

8 files changed

+213
-0
lines changed

8 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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;
12+
using NHibernate.Cfg;
13+
using NHibernate.Cfg.MappingSchema;
14+
using NHibernate.Mapping.ByCode;
15+
using NUnit.Framework;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH2052
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture]
21+
public class FixtureAsync : TestCaseMappingByCode
22+
{
23+
protected override void Configure(Configuration configuration)
24+
{
25+
base.Configure(configuration);
26+
27+
Cfg.Environment.UseReflectionOptimizer = false;
28+
}
29+
30+
protected override HbmMapping GetMappings()
31+
{
32+
var mapper = new ModelMapper();
33+
34+
mapper.Class<EntityClassProxy>(
35+
rc =>
36+
{
37+
// calling the id getter of the proxy triggers a database query
38+
//rc.Proxy(typeof(EntityClassProxy)); // That is the default, no need to actually uncomment it
39+
40+
// calling the id getter of the proxy doesn't trigger a database query
41+
//rc.Proxy(typeof(IEntity));
42+
43+
rc.Id(x => x.Id);
44+
rc.Property(x => x.Name);
45+
});
46+
47+
mapper.UnionSubclass<SubEntityInterfaceProxy>(
48+
rc =>
49+
{
50+
rc.Proxy(typeof(ISubEntityProxy));
51+
52+
rc.Property(x => x.AnotherName);
53+
});
54+
55+
mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>(
56+
rc =>
57+
{
58+
rc.Proxy(typeof(IAnotherSubEntityProxy));
59+
60+
rc.Property(x => x.AnotherName);
61+
});
62+
63+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
64+
}
65+
66+
[Test]
67+
public async Task ShouldSupportCheckingLookupIdAsync()
68+
{
69+
using (var session = OpenSession())
70+
{
71+
var lookupId = Guid.NewGuid();
72+
var entity = (IEntity) await (session.LoadAsync(typeof(EntityClassProxy), lookupId));
73+
Assert.That(entity.Id, Is.EqualTo(lookupId));
74+
}
75+
}
76+
77+
[Test]
78+
public void ShouldSupportLoadingNonexistentAsync()
79+
{
80+
using (var session = OpenSession())
81+
{
82+
var lookupId = Guid.NewGuid();
83+
Assert.That(() => session.LoadAsync<EntityClassProxy>(lookupId), Throws.Nothing);
84+
}
85+
}
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2052
2+
{
3+
class AnotherSubEntityInterfaceProxy : EntityClassProxy, IAnotherSubEntityProxy
4+
{
5+
public virtual string AnotherName { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2052
4+
{
5+
public class EntityClassProxy : IEntity
6+
{
7+
public virtual Guid Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using NHibernate.Cfg;
3+
using NHibernate.Cfg.MappingSchema;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH2052
8+
{
9+
[TestFixture]
10+
public class Fixture : TestCaseMappingByCode
11+
{
12+
protected override void Configure(Configuration configuration)
13+
{
14+
base.Configure(configuration);
15+
16+
Cfg.Environment.UseReflectionOptimizer = false;
17+
}
18+
19+
protected override HbmMapping GetMappings()
20+
{
21+
var mapper = new ModelMapper();
22+
23+
mapper.Class<EntityClassProxy>(
24+
rc =>
25+
{
26+
// calling the id getter of the proxy triggers a database query
27+
//rc.Proxy(typeof(EntityClassProxy)); // That is the default, no need to actually uncomment it
28+
29+
// calling the id getter of the proxy doesn't trigger a database query
30+
//rc.Proxy(typeof(IEntity));
31+
32+
rc.Id(x => x.Id);
33+
rc.Property(x => x.Name);
34+
});
35+
36+
mapper.UnionSubclass<SubEntityInterfaceProxy>(
37+
rc =>
38+
{
39+
rc.Proxy(typeof(ISubEntityProxy));
40+
41+
rc.Property(x => x.AnotherName);
42+
});
43+
44+
mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>(
45+
rc =>
46+
{
47+
rc.Proxy(typeof(IAnotherSubEntityProxy));
48+
49+
rc.Property(x => x.AnotherName);
50+
});
51+
52+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
53+
}
54+
55+
[Test]
56+
public void ShouldSupportCheckingLookupId()
57+
{
58+
using (var session = OpenSession())
59+
{
60+
var lookupId = Guid.NewGuid();
61+
var entity = (IEntity) session.Load(typeof(EntityClassProxy), lookupId);
62+
Assert.That(entity.Id, Is.EqualTo(lookupId));
63+
}
64+
}
65+
66+
[Test]
67+
public void ShouldSupportLoadingNonexistent()
68+
{
69+
using (var session = OpenSession())
70+
{
71+
var lookupId = Guid.NewGuid();
72+
Assert.That(() => session.Load<EntityClassProxy>(lookupId), Throws.Nothing);
73+
}
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2052
2+
{
3+
public interface IAnotherSubEntityProxy : IEntity
4+
{
5+
string AnotherName { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2052
4+
{
5+
public interface IEntity
6+
{
7+
Guid Id { get; set; }
8+
9+
string Name { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2052
2+
{
3+
public interface ISubEntityProxy: IEntity
4+
{
5+
string AnotherName { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2052
2+
{
3+
class SubEntityInterfaceProxy : EntityClassProxy, ISubEntityProxy
4+
{
5+
public virtual string AnotherName { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)