Skip to content

Commit 3a9b700

Browse files
Modernize test example
* Use NUnit fluent syntax. * Commit transaction rather than letting them rollback: Commit is usually more lightweight. * Fix missing TextFixture attribute, causing a lack of async generation for mapping by code. * Delete fully in DB by default in teardown. * Remove flushes redundant with transaction commit. * Fix a missing async regen from a previous PR, just changing some imports.
1 parent c90b08c commit 3a9b700

File tree

7 files changed

+137
-36
lines changed

7 files changed

+137
-36
lines changed

src/NHibernate.Test/Async/NHSpecificTest/GH0000/Fixture.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
using System.Linq;
12-
using NHibernate.Linq;
1312
using NUnit.Framework;
13+
using NHibernate.Linq;
1414

1515
namespace NHibernate.Test.NHSpecificTest.GH0000
1616
{
@@ -20,43 +20,46 @@ public class FixtureAsync : BugTestCase
2020
{
2121
protected override void OnSetUp()
2222
{
23-
using (ISession session = OpenSession())
24-
using (ITransaction transaction = session.BeginTransaction())
23+
using (var session = OpenSession())
24+
using (var transaction = session.BeginTransaction())
2525
{
2626
var e1 = new Entity {Name = "Bob"};
2727
session.Save(e1);
2828

2929
var e2 = new Entity {Name = "Sally"};
3030
session.Save(e2);
3131

32-
session.Flush();
3332
transaction.Commit();
3433
}
3534
}
3635

3736
protected override void OnTearDown()
3837
{
39-
using (ISession session = OpenSession())
40-
using (ITransaction transaction = session.BeginTransaction())
38+
using (var session = OpenSession())
39+
using (var transaction = session.BeginTransaction())
4140
{
42-
session.Delete("from System.Object");
41+
// The HQL delete does all the job inside the database without loading the entities, but it does
42+
// not handle delete order for avoiding violating constraints if any. Use
43+
// session.Delete("from System.Object");
44+
// instead if in need of having NHbernate ordering the deletes, but this will cause
45+
// loading the entities in the session.
46+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
4347

44-
session.Flush();
4548
transaction.Commit();
4649
}
4750
}
4851

4952
[Test]
5053
public async Task YourTestNameAsync()
5154
{
52-
using (ISession session = OpenSession())
55+
using (var session = OpenSession())
5356
using (session.BeginTransaction())
5457
{
5558
var result = from e in session.Query<Entity>()
5659
where e.Name == "Bob"
5760
select e;
5861

59-
Assert.AreEqual(1, (await (result.ToListAsync())).Count);
62+
Assert.That(await (result.ToListAsync()), Has.Count.EqualTo(1));
6063
}
6164
}
6265
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg.MappingSchema;
13+
using NHibernate.Mapping.ByCode;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH0000
18+
{
19+
using System.Threading.Tasks;
20+
/// <summary>
21+
/// Fixture using 'by code' mappings
22+
/// </summary>
23+
/// <remarks>
24+
/// This fixture is identical to <see cref="FixtureAsync" /> except the <see cref="Entity" /> mapping is performed
25+
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
26+
/// if you prefer.
27+
/// </remarks>
28+
[TestFixture]
29+
public class ByCodeFixtureAsync : TestCaseMappingByCode
30+
{
31+
protected override HbmMapping GetMappings()
32+
{
33+
var mapper = new ModelMapper();
34+
mapper.Class<Entity>(rc =>
35+
{
36+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
37+
rc.Property(x => x.Name);
38+
});
39+
40+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
41+
}
42+
43+
protected override void OnSetUp()
44+
{
45+
using (var session = OpenSession())
46+
using (var transaction = session.BeginTransaction())
47+
{
48+
var e1 = new Entity { Name = "Bob" };
49+
session.Save(e1);
50+
51+
var e2 = new Entity { Name = "Sally" };
52+
session.Save(e2);
53+
54+
transaction.Commit();
55+
}
56+
}
57+
58+
protected override void OnTearDown()
59+
{
60+
using (var session = OpenSession())
61+
using (var transaction = session.BeginTransaction())
62+
{
63+
// The HQL delete does all the job inside the database without loading the entities, but it does
64+
// not handle delete order for avoiding violating constraints if any. Use
65+
// session.Delete("from System.Object");
66+
// instead if in need of having NHbernate ordering the deletes, but this will cause
67+
// loading the entities in the session.
68+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
69+
70+
transaction.Commit();
71+
}
72+
}
73+
74+
[Test]
75+
public async Task YourTestNameAsync()
76+
{
77+
using (var session = OpenSession())
78+
using (var transaction = session.BeginTransaction())
79+
{
80+
var result = from e in session.Query<Entity>()
81+
where e.Name == "Bob"
82+
select e;
83+
84+
Assert.That(await (result.ToListAsync()), Has.Count.EqualTo(1));
85+
await (transaction.CommitAsync());
86+
}
87+
}
88+
}
89+
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Linq;
2-
using NHibernate.Linq;
32
using NUnit.Framework;
43

54
namespace NHibernate.Test.NHSpecificTest.GH0000
@@ -9,43 +8,46 @@ public class Fixture : BugTestCase
98
{
109
protected override void OnSetUp()
1110
{
12-
using (ISession session = OpenSession())
13-
using (ITransaction transaction = session.BeginTransaction())
11+
using (var session = OpenSession())
12+
using (var transaction = session.BeginTransaction())
1413
{
1514
var e1 = new Entity {Name = "Bob"};
1615
session.Save(e1);
1716

1817
var e2 = new Entity {Name = "Sally"};
1918
session.Save(e2);
2019

21-
session.Flush();
2220
transaction.Commit();
2321
}
2422
}
2523

2624
protected override void OnTearDown()
2725
{
28-
using (ISession session = OpenSession())
29-
using (ITransaction transaction = session.BeginTransaction())
26+
using (var session = OpenSession())
27+
using (var transaction = session.BeginTransaction())
3028
{
31-
session.Delete("from System.Object");
29+
// The HQL delete does all the job inside the database without loading the entities, but it does
30+
// not handle delete order for avoiding violating constraints if any. Use
31+
// session.Delete("from System.Object");
32+
// instead if in need of having NHbernate ordering the deletes, but this will cause
33+
// loading the entities in the session.
34+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
3235

33-
session.Flush();
3436
transaction.Commit();
3537
}
3638
}
3739

3840
[Test]
3941
public void YourTestName()
4042
{
41-
using (ISession session = OpenSession())
43+
using (var session = OpenSession())
4244
using (session.BeginTransaction())
4345
{
4446
var result = from e in session.Query<Entity>()
4547
where e.Name == "Bob"
4648
select e;
4749

48-
Assert.AreEqual(1, result.ToList().Count);
50+
Assert.That(result.ToList(), Has.Count.EqualTo(1));
4951
}
5052
}
5153
}

src/NHibernate.Test/NHSpecificTest/GH0000/FixtureByCode.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Linq;
22
using NHibernate.Cfg.MappingSchema;
3-
using NHibernate.Linq;
43
using NHibernate.Mapping.ByCode;
54
using NUnit.Framework;
65

@@ -14,6 +13,7 @@ namespace NHibernate.Test.NHSpecificTest.GH0000
1413
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
1514
/// if you prefer.
1615
/// </remarks>
16+
[TestFixture]
1717
public class ByCodeFixture : TestCaseMappingByCode
1818
{
1919
protected override HbmMapping GetMappings()
@@ -30,43 +30,47 @@ protected override HbmMapping GetMappings()
3030

3131
protected override void OnSetUp()
3232
{
33-
using (ISession session = OpenSession())
34-
using (ITransaction transaction = session.BeginTransaction())
33+
using (var session = OpenSession())
34+
using (var transaction = session.BeginTransaction())
3535
{
3636
var e1 = new Entity { Name = "Bob" };
3737
session.Save(e1);
3838

3939
var e2 = new Entity { Name = "Sally" };
4040
session.Save(e2);
4141

42-
session.Flush();
4342
transaction.Commit();
4443
}
4544
}
4645

4746
protected override void OnTearDown()
4847
{
49-
using (ISession session = OpenSession())
50-
using (ITransaction transaction = session.BeginTransaction())
48+
using (var session = OpenSession())
49+
using (var transaction = session.BeginTransaction())
5150
{
52-
session.Delete("from System.Object");
51+
// The HQL delete does all the job inside the database without loading the entities, but it does
52+
// not handle delete order for avoiding violating constraints if any. Use
53+
// session.Delete("from System.Object");
54+
// instead if in need of having NHbernate ordering the deletes, but this will cause
55+
// loading the entities in the session.
56+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
5357

54-
session.Flush();
5558
transaction.Commit();
5659
}
5760
}
5861

5962
[Test]
6063
public void YourTestName()
6164
{
62-
using (ISession session = OpenSession())
63-
using (session.BeginTransaction())
65+
using (var session = OpenSession())
66+
using (var transaction = session.BeginTransaction())
6467
{
6568
var result = from e in session.Query<Entity>()
6669
where e.Name == "Bob"
6770
select e;
6871

69-
Assert.AreEqual(1, result.ToList().Count);
72+
Assert.That(result.ToList(), Has.Count.EqualTo(1));
73+
transaction.Commit();
7074
}
7175
}
7276
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH0000">
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH0000">
34

4-
<class name="Entity">
5-
<id name="Id" generator="guid.comb" />
6-
<property name="Name" />
7-
</class>
5+
<class name="Entity">
6+
<id name="Id" generator="guid.comb"/>
7+
<property name="Name"/>
8+
</class>
89

910
</hibernate-mapping>

src/NHibernate/Async/Event/Default/EvictVisitor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using NHibernate.Collection;
1313
using NHibernate.Engine;
1414
using NHibernate.Impl;
15+
using NHibernate.Persister.Collection;
1516
using NHibernate.Type;
1617

1718
namespace NHibernate.Event.Default

src/NHibernate/Async/Persister/Collection/ICollectionPersister.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using System.Collections.Generic;
1213
using System.Data.Common;
1314
using NHibernate.Cache;

0 commit comments

Comments
 (0)