Skip to content

Commit a6bedf4

Browse files
committed
Add test case from patch in https://nhibernate.jira.com/browse/NH-2204
1 parent 20f8098 commit a6bedf4

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

src/NHibernate.Test/NHSpecificTest/NH2204/Fixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System;using System.Data;using NUnit.Framework;using NHibernate.Dialect;namespace NHibernate.Test.NHSpecificTest.NH2204{ [TestFixture] public class Fixture : BugTestCase { public override string BugNumber { get { return "NH2204"; } } protected override bool AppliesTo(Dialect.Dialect dialect) { return dialect is PostgreSQL81Dialect; } // create the trigger protected override void OnSetUp() { using (ISession s = OpenSession()) { IDbCommand command = s.Connection.CreateCommand(); command.CommandText = "CREATE OR REPLACE FUNCTION audit_parent() RETURNS trigger AS $audit_parent$" + Environment.NewLine + "BEGIN" + Environment.NewLine +
2+
"INSERT INTO parent_history SELECT nextval('parent_history_histid_seq'), now(), NEW.*;" + Environment.NewLine + "RETURN NEW;" + Environment.NewLine + "END" + Environment.NewLine + " $audit_parent$ LANGUAGE 'plpgsql';"; command.ExecuteNonQuery(); command.CommandText = "CREATE TRIGGER parent_audit" + Environment.NewLine + "AFTER INSERT OR UPDATE ON parent" + Environment.NewLine + "FOR EACH ROW EXECUTE PROCEDURE audit_parent();"; command.ExecuteNonQuery(); } } // remove trigger and remove data from tables protected override void OnTearDown() { using (ISession s = OpenSession()) { IDbCommand command = s.Connection.CreateCommand(); command.CommandText = "DROP FUNCTION audit_parent() CASCADE;"; command.ExecuteNonQuery();
3+
command.CommandText = "DELETE from parent_history;"; command.ExecuteNonQuery(); command.CommandText = "DELETE from parent;"; command.ExecuteNonQuery(); } } [Test] public void KnownFailure_Correct_Id_Returned_When_Using_Trigger() { Assert.Throws<AssertionException>(() => RunTest(), "We expected this test to fail - if the problem has been fixed, clean-up the test."); } private void RunTest() { var entity1 = new Parent {Name = "Parent1_0"}; // when saved this entity should have the id of 1 var entity2 = new Parent {Name = "Parent2_0"}; // when saved this entity should have the id of 2 var entity3 = new Parent {Name = "Parent3_0"}; // when saved this entity should have the id of 3 using (ISession s = OpenSession()) { // save first entity s.Save(entity1); s.Flush(); Assert.AreEqual(1, entity1.Id); // save second entity s.Save(entity2); s.Flush(); Assert.AreEqual(2, entity2.Id); // update this entity 10 times - adds entries to the audit table // causing the sequences for the parent and history table to no longer be aligned for (int i = 1; i < 11; i++) { entity2.Name = string.Format("Parent2_{0}", i); s.Update(entity2); s.Flush(); } // save third entity s.Save(entity3); s.Flush(); Assert.AreEqual( 3, entity3.Id, "oh uh - it would appear that lastval() is not our friend when a trigger updates other sequences."); // now would be a good time to look at the data in the tables and see that they have the IDs as expected // which are not the same as those returned by nhibernate } } }}

src/NHibernate.Test/NHSpecificTest/NH2204/Mappings.hbm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="utf-8"?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH2204"> <class name="Parent"> <id name="Id"> <generator class="native" /> </id> <property name="Name" /> </class> <class name="ParentHistory" table="parent_history"> <id name="HistId"> <generator class="native" /> </id> <property name="HistWhen" /> <property name="Id" /> <property name="Name" /> </class></hibernate-mapping>

src/NHibernate.Test/NHSpecificTest/NH2204/Model.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
using System;namespace NHibernate.Test.NHSpecificTest.NH2204{ public class Parent { public virtual int Id { get; set; } public virtual string Name { get; set; } } public class ParentHistory { public virtual int HistId { get; set; } public virtual DateTime HistWhen { get; set; } public virtual int Id { get; set; } public virtual string Name { get; set; } }}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@
731731
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Fixture.cs" />
732732
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
733733
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
734+
<Compile Include="NHSpecificTest\NH2204\Model.cs" />
735+
<Compile Include="NHSpecificTest\NH2204\Fixture.cs" />
734736
<Compile Include="NHSpecificTest\NH3414\Entity.cs" />
735737
<Compile Include="NHSpecificTest\NH3414\FixtureByCode.cs" />
736738
<Compile Include="NHSpecificTest\NH2218\Fixture.cs" />
@@ -3184,6 +3186,7 @@
31843186
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
31853187
</ItemGroup>
31863188
<ItemGroup>
3189+
<EmbeddedResource Include="NHSpecificTest\NH2204\Mappings.hbm.xml" />
31873190
<EmbeddedResource Include="NHSpecificTest\NH3874\Mappings.hbm.xml" />
31883191
<EmbeddedResource Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Mappings.hbm.xml" />
31893192
<EmbeddedResource Include="NHSpecificTest\NH2218\Mappings.hbm.xml" />

0 commit comments

Comments
 (0)