-
Notifications
You must be signed in to change notification settings - Fork 935
Equals is ignored for Components that contain collection #3541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DmitryMak
wants to merge
7
commits into
nhibernate:master
Choose a base branch
from
DmitryMak:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+262
−0
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24e3b82
Add files via upload
DmitryMak 0fb75ad
Generate async files
github-actions[bot] 6fea5e7
Update src/NHibernate.Test/NHSpecificTest/GH3539/CardInfo.cs
DmitryMak 3ff5b45
Update src/NHibernate.Test/NHSpecificTest/GH3539/ImmutableComponentTe…
DmitryMak 0677d5e
Generate async files
github-actions[bot] 1eac996
Use standard template
hazzik e564889
Generate async files
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/NHibernate.Test/Async/NHSpecificTest/GH3539/ImmutableComponentTest.cs
This file contains hidden or 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,104 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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.IO; | ||
using System.Reflection; | ||
using NHibernate.Cfg; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3539 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class ImmutableComponentTestAsync : TestCase | ||
{ | ||
protected override string MappingsAssembly | ||
{ | ||
get { return "NHibernate.Test"; } | ||
} | ||
|
||
protected override string[] Mappings | ||
{ | ||
get { return Array.Empty<string>(); } | ||
} | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( | ||
"NHibernate.Test.NHSpecificTest.GH3539.Person.hbm.xml")) | ||
{ | ||
using (StreamReader reader = new StreamReader(stream)) | ||
{ | ||
string mapping = reader.ReadToEnd(); | ||
|
||
configuration.AddXml(mapping); | ||
configuration.SetProperty(Cfg.Environment.GenerateStatistics, "true"); | ||
} | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
s.Delete("from Person"); | ||
t.Commit(); | ||
} | ||
|
||
base.OnTearDown(); | ||
} | ||
|
||
[Test] | ||
public async Task TestComponentAsync() | ||
{ | ||
int personId; | ||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
var person = new Person(age: 20) | ||
{ | ||
CardInfo = new("card1", "card2") | ||
}; | ||
await (s.SaveAsync(person)); | ||
await (t.CommitAsync()); | ||
await (s.FlushAsync()); | ||
personId = person.Id; | ||
} | ||
|
||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
var restored = await (s.GetAsync<Person>(personId)); | ||
|
||
var oldCardInfo = restored.CardInfo; | ||
|
||
Assert.That(oldCardInfo.GetCardsCopy().Contains("card1")); | ||
Assert.That(oldCardInfo.GetCardsCopy().Contains("card2")); | ||
|
||
var newCardInfo = new CardInfo("card1", "card2"); | ||
|
||
Assert.That(oldCardInfo.Equals(newCardInfo), Is.True); | ||
|
||
restored.CardInfo = newCardInfo; | ||
|
||
// Expected behaviour: | ||
// | ||
// At this point there should be no DML statements because newCardInfo | ||
// is the same as the old one. But NHibernate will generate DELETE | ||
// followed by 2 INSERT into OldCards table. I'm not sure how to fail | ||
// an assertion when an unexpected DML is issued. | ||
|
||
await (t.CommitAsync()); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3539; | ||
|
||
public class CardInfo | ||
{ | ||
private readonly ISet<string> _oldCards; | ||
|
||
protected CardInfo() { } | ||
|
||
public CardInfo(params string[] cards) | ||
{ | ||
_oldCards = cards.ToHashSet(); | ||
} | ||
|
||
public virtual ISet<string> GetCardsCopy() | ||
{ | ||
return _oldCards.ToHashSet(); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is null) | ||
{ | ||
return false; | ||
} | ||
if (ReferenceEquals(this, obj)) | ||
{ | ||
return true; | ||
} | ||
if (obj.GetType() != GetType()) | ||
{ | ||
return false; | ||
} | ||
var other = (CardInfo) obj; | ||
return _oldCards.SetEquals(other._oldCards); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hashCode = new HashCode(); | ||
hashCode.Add(_oldCards); | ||
return hashCode.ToHashCode(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/NHibernate.Test/NHSpecificTest/GH3539/ImmutableComponentTest.cs
This file contains hidden or 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,93 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using NHibernate.Cfg; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH3539 | ||
{ | ||
[TestFixture] | ||
public class ImmutableComponentTest : TestCase | ||
{ | ||
protected override string MappingsAssembly | ||
{ | ||
get { return "NHibernate.Test"; } | ||
} | ||
|
||
protected override string[] Mappings | ||
{ | ||
get { return Array.Empty<string>(); } | ||
} | ||
|
||
protected override void Configure(Configuration configuration) | ||
{ | ||
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( | ||
"NHibernate.Test.NHSpecificTest.GH3539.Person.hbm.xml")) | ||
{ | ||
using (StreamReader reader = new StreamReader(stream)) | ||
{ | ||
string mapping = reader.ReadToEnd(); | ||
|
||
configuration.AddXml(mapping); | ||
configuration.SetProperty(Cfg.Environment.GenerateStatistics, "true"); | ||
} | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
s.Delete("from Person"); | ||
t.Commit(); | ||
} | ||
|
||
base.OnTearDown(); | ||
} | ||
|
||
[Test] | ||
public void TestComponent() | ||
{ | ||
int personId; | ||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
var person = new Person(age: 20) | ||
{ | ||
CardInfo = new("card1", "card2") | ||
}; | ||
s.Save(person); | ||
t.Commit(); | ||
s.Flush(); | ||
personId = person.Id; | ||
} | ||
|
||
using (ISession s = Sfi.OpenSession()) | ||
using (ITransaction t = s.BeginTransaction()) | ||
{ | ||
var restored = s.Get<Person>(personId); | ||
|
||
var oldCardInfo = restored.CardInfo; | ||
|
||
Assert.That(oldCardInfo.GetCardsCopy().Contains("card1")); | ||
Assert.That(oldCardInfo.GetCardsCopy().Contains("card2")); | ||
DmitryMak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var newCardInfo = new CardInfo("card1", "card2"); | ||
|
||
Assert.That(oldCardInfo.Equals(newCardInfo), Is.True); | ||
|
||
restored.CardInfo = newCardInfo; | ||
|
||
// Expected behaviour: | ||
// | ||
// At this point there should be no DML statements because newCardInfo | ||
// is the same as the old one. But NHibernate will generate DELETE | ||
// followed by 2 INSERT into OldCards table. I'm not sure how to fail | ||
// an assertion when an unexpected DML is issued. | ||
|
||
t.Commit(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
namespace NHibernate.Test.NHSpecificTest.GH3539 | ||
{ | ||
public class Person | ||
{ | ||
private int _id; | ||
private int _age; | ||
private CardInfo _cardInfo; | ||
|
||
protected Person() { } | ||
|
||
public Person(int age) | ||
{ | ||
_cardInfo = new(); | ||
_age = age; | ||
} | ||
|
||
public virtual int Id | ||
{ | ||
get { return _id; } | ||
set { _id = value; } | ||
} | ||
|
||
public virtual int Age | ||
{ | ||
get { return _age; } | ||
set { _age = value; } | ||
} | ||
|
||
public virtual CardInfo CardInfo | ||
{ | ||
get { return _cardInfo; } | ||
set { _cardInfo = value; } | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping | ||
xmlns="urn:nhibernate-mapping-2.2" | ||
assembly="NHibernate.Test" | ||
namespace="NHibernate.Test.NHSpecificTest.GH3539"> | ||
|
||
<class name="Person" table="People"> | ||
<id name="_id" type="int" column="ID" access="field"> | ||
<generator class="increment"/> | ||
</id> | ||
<property name="_age" type="int" access="field"/> | ||
|
||
<component name="_cardInfo" access="field"> | ||
<set name="_oldCards" table="OldCards" cascade="all" access="field" > | ||
<key column="PersonId" /> | ||
<element column="Card" type="String" /> | ||
</set> | ||
</component> | ||
</class> | ||
|
||
</hibernate-mapping> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.