forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide test case for nhibernate#3643
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
// ReSharper disable CollectionNeverUpdated.Local | ||
// ReSharper disable UnassignedGetOnlyAutoProperty | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3643 | ||
{ | ||
class Entity | ||
{ | ||
private readonly ICollection<ChildEntity> _children = new List<ChildEntity>(); | ||
public virtual EntityId Id { get; protected set; } | ||
public virtual IEnumerable<ChildEntity> Children => _children.AsEnumerable(); | ||
} | ||
|
||
class ChildEntity | ||
{ | ||
public virtual int Id { get; protected set; } | ||
} | ||
|
||
enum EntityId | ||
{ | ||
Id1, | ||
Id2 | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/NHibernate.Test/NHSpecificTest/GH3643/FixtureByCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Linq; | ||
using NHibernate.Cfg; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Linq; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3643 | ||
{ | ||
[TestFixture] | ||
public class FixtureByCode : TestCaseMappingByCode | ||
{ | ||
protected override void Configure(Configuration configuration) | ||
{ | ||
configuration.SetProperty(Environment.UseQueryCache, "true"); | ||
configuration.SetProperty(Environment.GenerateStatistics, "true"); | ||
} | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
|
||
mapper.Class<Entity>( | ||
rc => | ||
{ | ||
rc.Id(x => x.Id); | ||
rc.Bag( | ||
x => x.Children, | ||
m => | ||
{ | ||
m.Access(Accessor.Field); | ||
m.Key(k => k.Column("EntityId")); | ||
}, | ||
r => r.OneToMany()); | ||
|
||
rc.Cache( | ||
cm => | ||
{ | ||
cm.Include(CacheInclude.All); | ||
cm.Usage(CacheUsage.ReadWrite); | ||
}); | ||
}); | ||
|
||
mapper.Class<ChildEntity>( | ||
rc => | ||
{ | ||
rc.Id(x => x.Id); | ||
rc.Cache( | ||
cm => | ||
{ | ||
cm.Include(CacheInclude.All); | ||
cm.Usage(CacheUsage.ReadWrite); | ||
}); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
|
||
session.CreateSQLQuery( | ||
"INSERT INTO Entity (Id) VALUES (0)" | ||
).ExecuteUpdate(); | ||
|
||
session.CreateSQLQuery( | ||
"INSERT INTO ChildEntity (Id, EntityId) VALUES (0, 0)" | ||
).ExecuteUpdate(); | ||
|
||
session.CreateSQLQuery( | ||
"INSERT INTO ChildEntity (Id, EntityId) VALUES (1, 0)" | ||
).ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
|
||
session.CreateSQLQuery("DELETE FROM ChildEntity").ExecuteUpdate(); | ||
session.CreateSQLQuery("DELETE FROM Entity").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
[Test] | ||
public void LoadsEntityWithEnumIdAndChildrenUsingQueryCache() | ||
{ | ||
LoadEntityByNameWithQueryCache(); // warm up cache | ||
|
||
var entity = LoadEntityByNameWithQueryCache(); | ||
|
||
Assert.That(entity.Children.Count(), Is.EqualTo(2)); | ||
|
||
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count"); | ||
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count"); | ||
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count"); | ||
} | ||
|
||
private Entity LoadEntityByNameWithQueryCache() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
var entity = session | ||
.Query<Entity>() | ||
.FetchMany(x => x.Children) | ||
.WithOptions(opt => opt.SetCacheable(true)) | ||
.ToList()[0]; | ||
|
||
transaction.Commit(); | ||
return entity; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters