Skip to content

Commit d7d212c

Browse files
NH-4034 - tests semantic adjustments.
1 parent ac1de47 commit d7d212c

File tree

5 files changed

+52
-43
lines changed

5 files changed

+52
-43
lines changed

src/NHibernate.Test/SystemTransactions/TransactionFixture.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Linq;
23
using System.Transactions;
34
using NHibernate.Linq;
@@ -35,59 +36,58 @@ public void CanUseSystemTransactionsToCommit()
3536
[Test]
3637
public void FlushFromTransactionAppliesToDisposedSharingSession()
3738
{
38-
using (var s = OpenSession())
39+
var flushOrder = new List<int>();
40+
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
3941
{
4042
var builder = s.SessionWithOptions().Connection();
4143

4244
using (var t = new TransactionScope())
4345
{
4446
var p1 = new Person();
45-
// The relationship is there for failing in case the flush ordering is not the expected one.
46-
var p2 = new Person { Related = p1 };
47-
var p3 = new Person { Related = p2 };
48-
// If putting p3 here, adjust base tear-down.
49-
var p4 = new Person { Related = p2 };
47+
var p2 = new Person();
48+
var p3 = new Person();
49+
var p4 = new Person();
5050

51-
using (var s1 = builder.OpenSession())
51+
using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
5252
s1.Save(p1);
53-
using (var s2 = builder.OpenSession())
53+
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
5454
{
5555
s2.Save(p2);
56-
using (var s3 = s2.SessionWithOptions().Connection().OpenSession())
56+
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
5757
s3.Save(p3);
5858
}
5959
s.Save(p4);
6060
t.Complete();
6161
}
6262
}
6363

64+
Assert.That(flushOrder, Is.EqualTo(new[] { 1, 2, 3, 0 }));
65+
6466
using (var s = OpenSession())
6567
using (var t = s.BeginTransaction())
6668
{
6769
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
68-
Assert.That(s.Query<Person>().Count(p => p.Related != null), Is.EqualTo(3));
6970
t.Commit();
7071
}
7172
}
7273

7374
[Test]
7475
public void FlushFromTransactionAppliesToSharingSession()
7576
{
76-
using (var s = OpenSession())
77+
var flushOrder = new List<int>();
78+
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
7779
{
7880
var builder = s.SessionWithOptions().Connection();
7981

80-
using (var s1 = builder.OpenSession())
81-
using (var s2 = builder.OpenSession())
82-
using (var s3 = s2.SessionWithOptions().Connection().OpenSession())
82+
using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
83+
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
84+
using (var s3 = s2.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
8385
using (var t = new TransactionScope())
8486
{
8587
var p1 = new Person();
86-
// The relationship is there for failing in case the flush ordering is not the expected one.
87-
var p2 = new Person { Related = p1 };
88-
var p3 = new Person { Related = p2 };
89-
// If putting p3 here, adjust base tear-down.
90-
var p4 = new Person { Related = p2 };
88+
var p2 = new Person();
89+
var p3 = new Person();
90+
var p4 = new Person();
9191
s1.Save(p1);
9292
s2.Save(p2);
9393
s3.Save(p3);
@@ -96,11 +96,12 @@ public void FlushFromTransactionAppliesToSharingSession()
9696
}
9797
}
9898

99+
Assert.That(flushOrder, Is.EqualTo(new[] { 1, 2, 3, 0 }));
100+
99101
using (var s = OpenSession())
100102
using (var t = s.BeginTransaction())
101103
{
102104
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
103-
Assert.That(s.Query<Person>().Count(p => p.Related != null), Is.EqualTo(3));
104105
t.Commit();
105106
}
106107
}

src/NHibernate.Test/TransactionTest/Person.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ public class Person
77
public virtual int Id { get; set; }
88

99
public virtual DateTime CreatedAt { get; set; } = DateTime.Now;
10-
11-
public virtual Person Related { get; set; }
1210
}
1311
}

src/NHibernate.Test/TransactionTest/Person.hbm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
<generator class="hilo"/>
99
</id>
1010
<property name="CreatedAt"/>
11-
<many-to-one name="Related" class="Person" />
1211
</class>
1312
</hibernate-mapping>

src/NHibernate.Test/TransactionTest/TransactionFixture.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Linq;
35
using NHibernate.Linq;
46
using NUnit.Framework;
@@ -139,21 +141,20 @@ public void WasCommittedOrRolledBack()
139141
[Test]
140142
public void FlushFromTransactionAppliesToSharingSession()
141143
{
142-
using (var s = OpenSession())
144+
var flushOrder = new List<int>();
145+
using (var s = OpenSession(new TestInterceptor(0, flushOrder)))
143146
{
144147
var builder = s.SessionWithOptions().Connection();
145148

146-
using (var s1 = builder.OpenSession())
147-
using (var s2 = builder.OpenSession())
148-
using (var s3 = s1.SessionWithOptions().Connection().OpenSession())
149+
using (var s1 = builder.Interceptor(new TestInterceptor(1, flushOrder)).OpenSession())
150+
using (var s2 = builder.Interceptor(new TestInterceptor(2, flushOrder)).OpenSession())
151+
using (var s3 = s1.SessionWithOptions().Connection().Interceptor(new TestInterceptor(3, flushOrder)).OpenSession())
149152
using (var t = s.BeginTransaction())
150153
{
151154
var p1 = new Person();
152-
// The relationship is there for failing in case the flush ordering is not the expected one.
153-
var p2 = new Person { Related = p1 };
154-
var p3 = new Person { Related = p2 };
155-
// If putting p3 here, adjust base tear-down.
156-
var p4 = new Person { Related = p2 };
155+
var p2 = new Person();
156+
var p3 = new Person();
157+
var p4 = new Person();
157158
s1.Save(p1);
158159
s2.Save(p2);
159160
s3.Save(p3);
@@ -162,11 +163,12 @@ public void FlushFromTransactionAppliesToSharingSession()
162163
}
163164
}
164165

166+
Assert.That(flushOrder, Is.EqualTo(new[] { 1, 2, 3, 0 }));
167+
165168
using (var s = OpenSession())
166169
using (var t = s.BeginTransaction())
167170
{
168171
Assert.That(s.Query<Person>().Count(), Is.EqualTo(4));
169-
Assert.That(s.Query<Person>().Count(p => p.Related != null), Is.EqualTo(3));
170172
t.Commit();
171173
}
172174
}
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
2-
using System.Linq;
3-
using NHibernate.Linq;
2+
using System.Collections.Generic;
43

54
namespace NHibernate.Test.TransactionTest
65
{
@@ -15,16 +14,26 @@ protected override void OnTearDown()
1514
using (var s = OpenSession())
1615
using (var t = s.BeginTransaction())
1716
{
18-
// MySql and maybe some other db fails deleting the whole table in "right order"
19-
foreach (var p in s.Query<Person>().Where(p => p.Related.Related != null))
20-
{
21-
s.Delete(p);
22-
}
23-
s.Flush();
24-
s.CreateQuery("delete from Person p where p.Related != null").ExecuteUpdate();
25-
s.CreateQuery("delete from System.Object").ExecuteUpdate();
17+
s.Delete("from System.Object");
2618
t.Commit();
2719
}
2820
}
21+
22+
public class TestInterceptor : EmptyInterceptor
23+
{
24+
private readonly int _numero;
25+
private readonly List<int> _flushOrder;
26+
27+
public TestInterceptor(int numero, List<int> flushOrder)
28+
{
29+
_numero = numero;
30+
_flushOrder = flushOrder;
31+
}
32+
33+
public override void PreFlush(ICollection entitites)
34+
{
35+
_flushOrder.Add(_numero);
36+
}
37+
}
2938
}
3039
}

0 commit comments

Comments
 (0)