Skip to content

Commit

Permalink
Test for nhibernate#18 - Incorrect audit table insert on tables with …
Browse files Browse the repository at this point in the history
…tree structure
  • Loading branch information
wojtek-abakus committed Jul 25, 2019
1 parent 559596a commit cd1b2d2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Envers.Tests"
namespace="NHibernate.Envers.Tests.NetSpecific.Integration.Tree">
<class name="TreeEntity">
<id name="Id">
<generator class="native"/>
</id>
<property name="Name" not-null="true"/>
<many-to-one name="Parent" class="TreeEntity" column="parentKey"/>
<list name="Children" cascade="all-delete-orphan">
<key column="parentKey"/>
<index column="parentInd"/>
<one-to-many class="TreeEntity"/>
</list>
</class>
</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using NHibernate.Envers.Configuration.Attributes;

namespace NHibernate.Envers.Tests.NetSpecific.Integration.Tree
{
[Audited]
public class TreeEntity
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual TreeEntity Parent { get; set; }
public virtual IList<TreeEntity> Children { get; protected set; } = new List<TreeEntity>();

public override bool Equals(object obj)
{
if (!(obj is TreeEntity other))
return false;
return Id == other.Id;
}

public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Envers.Tests.NetSpecific.Integration.Tree
{
public class TreeTest : TestBase
{
private long parentId;
private long childId;

public TreeTest(AuditStrategyForTest strategyType) : base(strategyType)
{
}

protected override IEnumerable<string> Mappings => new[] {"NetSpecific.Integration.Tree.Mapping.hbm.xml"};

protected override void Initialize()
{
var parent = new TreeEntity {Name = "Parent"};

using (var tx = Session.BeginTransaction())
{
parentId = (long) Session.Save(parent);
tx.Commit();
}

var child = new TreeEntity
{
Name = "Child",
Parent = new TreeEntity {Id = parentId}
};

using (var tx = Session.BeginTransaction())
{
childId = (long) Session.Save(child);
tx.Commit();
}
}

[Test]
public void VerifyRevisionCount()
{
CollectionAssert.AreEquivalent(new[] {1}, AuditReader().GetRevisions(typeof(TreeEntity), parentId));
CollectionAssert.AreEquivalent(new[] {2}, AuditReader().GetRevisions(typeof(TreeEntity), childId));
}

[Test]
public void VerifyHistory()
{
var rev1 = AuditReader().Find<TreeEntity>(parentId, 1);
var rev2 = AuditReader().Find<TreeEntity>(parentId, 2);

Assert.IsNotNull(rev1.Name);
Assert.IsNotNull(rev2.Name);
}
}
}

0 comments on commit cd1b2d2

Please sign in to comment.