-
Notifications
You must be signed in to change notification settings - Fork 934
Unit test for issue #1766 #1768
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
Closed
Closed
Changes from all commits
Commits
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
80 changes: 80 additions & 0 deletions
80
src/NHibernate.Test/Async/NHSpecificTest/GH1766/Fixture.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,80 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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 NUnit.Framework; | ||
using NHibernate.Linq; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1766 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class FixtureAsync : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var parent = new A | ||
{ | ||
Items = new List<B> | ||
{ | ||
new B(), | ||
new B(), | ||
}, | ||
}; | ||
|
||
foreach (var child in parent.Items) | ||
{ | ||
child.A = parent; | ||
} | ||
|
||
session.Persist(parent); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.Delete("from System.Object"); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task ShouldSetListIndexForInverseListAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = await (session.Query<A>().ToListAsync()); | ||
Assert.That(result, Has.Count.EqualTo(1)); | ||
|
||
var root = result.Single(); | ||
Assert.That(root.Items, Is.Not.Null); | ||
Assert.That(root.Items, Has.Count.EqualTo(2)); | ||
|
||
var expectedIndex = 0; | ||
foreach (var child in root.Items) | ||
{ | ||
Assert.That(child.ListIndex, Is.EqualTo(expectedIndex)); | ||
++expectedIndex; | ||
} | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1766 | ||
{ | ||
public class A | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual IList<B> Items { get; set; } | ||
} | ||
|
||
public class B | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual A A { get; set; } | ||
public virtual int ListIndex { get; set; } | ||
} | ||
} |
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,68 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1766 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var parent = new A | ||
{ | ||
Items = new List<B> | ||
{ | ||
new B(), | ||
new B(), | ||
}, | ||
}; | ||
|
||
foreach (var child in parent.Items) | ||
{ | ||
child.A = parent; | ||
} | ||
|
||
session.Persist(parent); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.Delete("from System.Object"); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void ShouldSetListIndexForInverseList() | ||
{ | ||
using (var session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = session.Query<A>().ToList(); | ||
Assert.That(result, Has.Count.EqualTo(1)); | ||
|
||
var root = result.Single(); | ||
Assert.That(root.Items, Is.Not.Null); | ||
Assert.That(root.Items, Has.Count.EqualTo(2)); | ||
|
||
var expectedIndex = 0; | ||
foreach (var child in root.Items) | ||
{ | ||
Assert.That(child.ListIndex, Is.EqualTo(expectedIndex)); | ||
++expectedIndex; | ||
} | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/NHibernate.Test/NHSpecificTest/GH1766/Mappings.hbm.xml
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,20 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" | ||
namespace="NHibernate.Test.NHSpecificTest.GH1766"> | ||
|
||
<class name="A" table="a"> | ||
<id name="Id" generator="guid.comb" /> | ||
<list name="Items" generic="true" inverse="true" cascade="all"> | ||
<key column="aid" /> | ||
<list-index base="1" column="listindex" /> | ||
<one-to-many class="B"/> | ||
</list> | ||
</class> | ||
|
||
<class name="B" table="b"> | ||
<id name="Id" generator="guid.comb" /> | ||
<many-to-one name="A" column="aid" /> | ||
<property name="ListIndex" column="listindex" /> | ||
</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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The index has to be set here, like the parent. When a list is mapped as inverse, no update occurs from it. It may at most cascade the update/save operation if cascade is enabled, but it will not set the appropriate parent and index.
From here:
The documentation also implies it with:
This is so because once the collection is mapped as inverse, the index must be handled from the other side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok. So it was a misunderstanding on my side. Would it be possible to update/clarify the documentation for this use case and/or write a unit test that shows this behavior? I did expect the index column to always be set/updated, because it's an integral part of the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also:
Anyway, a consequent update is also ongoing, awaiting review (see #1761). It notably adds some more examples and guidance, including some about lists.
I do not think we need to add more.