-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue that EntityUpdateAction increments version despite veto on…
- Loading branch information
Showing
4 changed files
with
251 additions
and
14 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/NHibernate.Test/Async/NHSpecificTest/GH3198/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,130 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by AsyncGenerator. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NHibernate.Cfg; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Event; | ||
using NHibernate.Mapping.ByCode; | ||
using NHibernate.Type; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3198 | ||
{ | ||
[TestFixture] | ||
public partial class ByCodeFixtureAsync : TestCaseMappingByCode | ||
{ | ||
private const int EXAMPLE_ID_VALUE = 1; | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
// A listener always returning true | ||
configuration.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] | ||
{ | ||
new TestEventListener() | ||
}; | ||
base.Configure(configuration); | ||
} | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Table("Entity"); | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); | ||
rc.Property(x => x.Name, x => x.Type<StringType>()); | ||
rc.Version(x => x.Version, vm => { }); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Id = EXAMPLE_ID_VALUE, Name = "old_name" }; | ||
session.Save(e1); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestVersionNotChangedWhenPreUpdateEventVetodAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = await (session.LoadAsync<Entity>(EXAMPLE_ID_VALUE)); | ||
|
||
entity.Name = "new_name"; | ||
await (session.UpdateAsync(entity)); | ||
|
||
var versionBeforeFlush = entity.Version; | ||
|
||
await (session.FlushAsync()); | ||
|
||
var versionAfterflush = entity.Version; | ||
|
||
Assert.That(versionAfterflush, Is.EqualTo(versionBeforeFlush), "The entity version must not change when update is vetoed"); | ||
} | ||
} | ||
|
||
// A listener always returning true | ||
public partial class TestEventListener : IPreUpdateEventListener | ||
{ | ||
public bool Executed { get; set; } | ||
public bool FoundAny { get; set; } | ||
|
||
public Task<bool> OnPreUpdateAsync(PreUpdateEvent @event, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<bool>(true); | ||
} | ||
|
||
public bool OnPreUpdate(PreUpdateEvent @event) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
public partial class Entity | ||
{ | ||
public virtual int Id { get; set; } | ||
public virtual string Name { get; set; } | ||
public virtual int Version { get; set; } | ||
} | ||
} | ||
public partial class ByCodeFixture : TestCaseMappingByCode | ||
{ | ||
|
||
public partial class TestEventListener : IPreUpdateEventListener | ||
{ | ||
|
||
public Task<bool> OnPreUpdateAsync(PreUpdateEvent @event, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<bool>(true); | ||
} | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/NHibernate.Test/NHSpecificTest/GH3198/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,103 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NHibernate.Cfg; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Event; | ||
using NHibernate.Mapping.ByCode; | ||
using NHibernate.Type; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3198 | ||
{ | ||
[TestFixture] | ||
public partial class ByCodeFixture : TestCaseMappingByCode | ||
{ | ||
private const int EXAMPLE_ID_VALUE = 1; | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
// A listener always returning true | ||
configuration.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] | ||
{ | ||
new TestEventListener() | ||
}; | ||
base.Configure(configuration); | ||
} | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Table("Entity"); | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); | ||
rc.Property(x => x.Name, x => x.Type<StringType>()); | ||
rc.Version(x => x.Version, vm => { }); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Id = EXAMPLE_ID_VALUE, Name = "old_name" }; | ||
session.Save(e1); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestVersionNotChangedWhenPreUpdateEventVetod() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = session.Load<Entity>(EXAMPLE_ID_VALUE); | ||
|
||
entity.Name = "new_name"; | ||
session.Update(entity); | ||
|
||
var versionBeforeFlush = entity.Version; | ||
|
||
session.Flush(); | ||
|
||
var versionAfterflush = entity.Version; | ||
|
||
Assert.That(versionAfterflush, Is.EqualTo(versionBeforeFlush), "The entity version must not change when update is vetoed"); | ||
} | ||
} | ||
|
||
// A listener always returning true | ||
public partial class TestEventListener : IPreUpdateEventListener | ||
{ | ||
public bool Executed { get; set; } | ||
public bool FoundAny { get; set; } | ||
|
||
public bool OnPreUpdate(PreUpdateEvent @event) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
public partial class Entity | ||
{ | ||
public virtual int Id { get; set; } | ||
public virtual string Name { get; set; } | ||
public virtual int Version { get; set; } | ||
} | ||
} | ||
} |
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
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