-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix empty dynamic components causing a phantom update (#3600)
Fix #3421
- Loading branch information
Showing
5 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/NHibernate.Test/Async/NHSpecificTest/GH3421/Fixture.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,109 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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.Collections.Generic; | ||
using System.Linq; | ||
using NHibernate.Cfg; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Mapping.ByCode; | ||
using NHibernate.SqlCommand; | ||
using NUnit.Framework; | ||
using NHibernate.Linq; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3421 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class ByCodeFixtureAsync : TestCaseMappingByCode | ||
{ | ||
private SqlInterceptor _interceptor; | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); | ||
rc.Property(x => x.Name); | ||
rc.Component(x => x.Attributes, new { | ||
Sku = (string)null | ||
}, dc => { | ||
dc.Property(x => x.Sku); | ||
}); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
base.Configure(configuration); | ||
|
||
_interceptor = new SqlInterceptor(); | ||
|
||
configuration.SetInterceptor(_interceptor); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Name = "Bob" }; | ||
session.Save(e1); | ||
|
||
var e2 = new Entity { Name = "Sally", Attributes = new Dictionary<string, object>() { | ||
{ "Sku", "AAA" } | ||
} }; | ||
session.Save(e2); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Entity").ExecuteUpdate(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestFlushDoesNotTriggerAnUpdateAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var foo = await (session.Query<Entity>().ToListAsync()); | ||
|
||
await (session.FlushAsync()); | ||
|
||
var updateStatements = _interceptor.SqlStatements.Where(s => s.ToString().ToUpper().Contains("UPDATE")).ToList(); | ||
|
||
Assert.That(updateStatements, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
public class SqlInterceptor : EmptyInterceptor | ||
{ | ||
public IList<SqlString> SqlStatements = new List<SqlString>(); | ||
|
||
public override SqlString OnPrepareStatement(SqlString sql) | ||
{ | ||
SqlStatements.Add(sql); | ||
|
||
return base.OnPrepareStatement(sql); | ||
} | ||
} | ||
} | ||
} |
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3421 | ||
{ | ||
class Entity | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
|
||
private IDictionary<string, object> _attributes; | ||
public virtual IDictionary<string, object> Attributes { | ||
get { | ||
if (_attributes == null) | ||
_attributes = new Dictionary<string, object>(); | ||
return _attributes; | ||
} | ||
set => _attributes = value; | ||
} | ||
} | ||
} |
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,97 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NHibernate.Cfg; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Mapping.ByCode; | ||
using NHibernate.SqlCommand; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3421 | ||
{ | ||
[TestFixture] | ||
public class ByCodeFixture : TestCaseMappingByCode | ||
{ | ||
private SqlInterceptor _interceptor; | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); | ||
rc.Property(x => x.Name); | ||
rc.Component(x => x.Attributes, new { | ||
Sku = (string)null | ||
}, dc => { | ||
dc.Property(x => x.Sku); | ||
}); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
base.Configure(configuration); | ||
|
||
_interceptor = new SqlInterceptor(); | ||
|
||
configuration.SetInterceptor(_interceptor); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Name = "Bob" }; | ||
session.Save(e1); | ||
|
||
var e2 = new Entity { Name = "Sally", Attributes = new Dictionary<string, object>() { | ||
{ "Sku", "AAA" } | ||
} }; | ||
session.Save(e2); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Entity").ExecuteUpdate(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestFlushDoesNotTriggerAnUpdate() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var foo = session.Query<Entity>().ToList(); | ||
|
||
session.Flush(); | ||
|
||
var updateStatements = _interceptor.SqlStatements.Where(s => s.ToString().ToUpper().Contains("UPDATE")).ToList(); | ||
|
||
Assert.That(updateStatements, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
public class SqlInterceptor : EmptyInterceptor | ||
{ | ||
public IList<SqlString> SqlStatements = new List<SqlString>(); | ||
|
||
public override SqlString OnPrepareStatement(SqlString sql) | ||
{ | ||
SqlStatements.Add(sql); | ||
|
||
return base.OnPrepareStatement(sql); | ||
} | ||
} | ||
} | ||
} |
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