Skip to content
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

Test for #3665 The deleted child entity is still inserted #3666

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <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 NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2028
{
using System.Threading.Tasks;
[TestFixture]
public class TempReattachedChildFixtureAsync : BugTestCase
{
protected override void OnTearDown()
{
using (ISession session = OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
public async Task WhenRemoveTempChild_ChildShouldNotInsertAsync()
{
Book book = null;
Word word = null;
using (var s = OpenSession())
using (var tr = s.BeginTransaction())
{
book = new Book { Id = 1 };
book.Words = new List<Word> { new Word { Id = 1, Parent = book } };
await (s.SaveAsync(book));
await (tr.CommitAsync());
}

word = new Word { Id = 2, Parent = book };
book.Words.Add(word);

using (var s = OpenSession())
using (var tr = s.BeginTransaction())
{
await (s.UpdateAsync(book));
book.Words.Remove(word);
using (var sl = new SqlLogSpy())
{
await (tr.CommitAsync());
var logs = sl.GetWholeLog();
Assert.That(logs, Does.Not.Contain("INSERT \n INTO\n Word").IgnoreCase, "Сollection record should not be inserted");
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2028/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH2028
{
public class Book
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Word> Words { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2028/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2028">

<class name="Book">
<cache usage="nonstrict-read-write" include="non-lazy" />
<id name="Id">
<generator class="assigned" />
</id>
<property name="Name" />
<bag name="Words" inverse="true" generic="true" cascade="all-delete-orphan" lazy="false" >
<key column="ParentId" />
<one-to-many class="Word" />
</bag>
</class>

<class name="Word">
<id name="Id" column="Id">
<generator class="assigned" />
</id>
<many-to-one name="Parent" class="Book" column="ParentId" />
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2028
{
[TestFixture]
public class TempReattachedChildFixture : BugTestCase
{
protected override void OnTearDown()
{
using (ISession session = OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
public void WhenRemoveTempChild_ChildShouldNotInsert()
{
Book book = null;
Word word = null;
using (var s = OpenSession())
using (var tr = s.BeginTransaction())
{
book = new Book { Id = 1 };
book.Words = new List<Word> { new Word { Id = 1, Parent = book } };
s.Save(book);
tr.Commit();
}

word = new Word { Id = 2, Parent = book };
book.Words.Add(word);

using (var s = OpenSession())
using (var tr = s.BeginTransaction())
{
s.Update(book);
book.Words.Remove(word);
using (var sl = new SqlLogSpy())
{
tr.Commit();
var logs = sl.GetWholeLog();
Assert.That(logs, Does.Not.Contain("INSERT \n INTO\n Word").IgnoreCase, "Сollection record should not be inserted");
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2028/Word.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.GH2028
{
public class Word
{
public virtual int Id { get; set; }
public virtual byte[] Content { get; set; }
public virtual Book Parent { get; set; }
}
}