Skip to content

Commit 1c347bd

Browse files
Add test for wrong proxy generation
nhibernate#2052
1 parent f1e54fc commit 1c347bd

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
private bool _useReflectionOptimizerBackup;
24+
25+
protected override void Configure(Configuration configuration)
26+
{
27+
base.Configure(configuration);
28+
29+
_useReflectionOptimizerBackup = Cfg.Environment.UseReflectionOptimizer;
30+
Cfg.Environment.UseReflectionOptimizer = false;
31+
}
32+
33+
protected override void DropSchema()
34+
{
35+
// Restore original setting.
36+
Cfg.Environment.UseReflectionOptimizer = _useReflectionOptimizerBackup;
37+
base.DropSchema();
38+
}
39+
40+
protected override HbmMapping GetMappings()
41+
{
42+
var mapper = new ModelMapper();
43+
44+
mapper.Class<EntityClassProxy>(
45+
rc =>
46+
{
47+
// calling the id getter of the proxy triggers a database query
48+
//rc.Proxy(typeof(EntityClassProxy)); // That is the default, no need to actually uncomment it
49+
50+
// calling the id getter of the proxy doesn't trigger a database query
51+
//rc.Proxy(typeof(IEntity));
52+
53+
rc.Id(x => x.Id);
54+
rc.Property(x => x.Name);
55+
});
56+
57+
mapper.UnionSubclass<SubEntityInterfaceProxy>(
58+
rc =>
59+
{
60+
rc.Proxy(typeof(ISubEntityProxy));
61+
62+
rc.Property(x => x.AnotherName);
63+
});
64+
65+
mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>(
66+
rc =>
67+
{
68+
rc.Proxy(typeof(IAnotherSubEntityProxy));
69+
70+
rc.Property(x => x.AnotherName);
71+
});
72+
73+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
74+
}
75+
76+
[Test]
77+
public async Task ShouldSupportCheckingLookupIdAsync()
78+
{
79+
using (var session = OpenSession())
80+
{
81+
var lookupId = Guid.NewGuid();
82+
var entity = (IEntity) await (session.LoadAsync(typeof(EntityClassProxy), lookupId));
83+
Assert.That(entity.Id, Is.EqualTo(lookupId));
84+
}
85+
}
86+
87+
[Test]
88+
public void ShouldSupportLoadingNonexistentAsync()
89+
{
90+
using (var session = OpenSession())
91+
{
92+
var lookupId = Guid.NewGuid();
93+
Assert.That(() => session.LoadAsync<EntityClassProxy>(lookupId), Throws.Nothing);
94+
}
95+
}
96+
}
97+
}
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,86 @@
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+
private bool _useReflectionOptimizerBackup;
13+
14+
protected override void Configure(Configuration configuration)
15+
{
16+
base.Configure(configuration);
17+
18+
_useReflectionOptimizerBackup = Cfg.Environment.UseReflectionOptimizer;
19+
Cfg.Environment.UseReflectionOptimizer = false;
20+
}
21+
22+
protected override void DropSchema()
23+
{
24+
// Restore original setting.
25+
Cfg.Environment.UseReflectionOptimizer = _useReflectionOptimizerBackup;
26+
base.DropSchema();
27+
}
28+
29+
protected override HbmMapping GetMappings()
30+
{
31+
var mapper = new ModelMapper();
32+
33+
mapper.Class<EntityClassProxy>(
34+
rc =>
35+
{
36+
// calling the id getter of the proxy triggers a database query
37+
//rc.Proxy(typeof(EntityClassProxy)); // That is the default, no need to actually uncomment it
38+
39+
// calling the id getter of the proxy doesn't trigger a database query
40+
//rc.Proxy(typeof(IEntity));
41+
42+
rc.Id(x => x.Id);
43+
rc.Property(x => x.Name);
44+
});
45+
46+
mapper.UnionSubclass<SubEntityInterfaceProxy>(
47+
rc =>
48+
{
49+
rc.Proxy(typeof(ISubEntityProxy));
50+
51+
rc.Property(x => x.AnotherName);
52+
});
53+
54+
mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>(
55+
rc =>
56+
{
57+
rc.Proxy(typeof(IAnotherSubEntityProxy));
58+
59+
rc.Property(x => x.AnotherName);
60+
});
61+
62+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
63+
}
64+
65+
[Test]
66+
public void ShouldSupportCheckingLookupId()
67+
{
68+
using (var session = OpenSession())
69+
{
70+
var lookupId = Guid.NewGuid();
71+
var entity = (IEntity) session.Load(typeof(EntityClassProxy), lookupId);
72+
Assert.That(entity.Id, Is.EqualTo(lookupId));
73+
}
74+
}
75+
76+
[Test]
77+
public void ShouldSupportLoadingNonexistent()
78+
{
79+
using (var session = OpenSession())
80+
{
81+
var lookupId = Guid.NewGuid();
82+
Assert.That(() => session.Load<EntityClassProxy>(lookupId), Throws.Nothing);
83+
}
84+
}
85+
}
86+
}
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)