Skip to content

Commit f96692a

Browse files
Add test for commit/rollback through session
1 parent 481f6f9 commit f96692a

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

src/NHibernate.Test/Async/TransactionTest/TransactionFixture.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,97 @@ public async Task WhenCommittingItemsWillAddThemTo2ndLevelCacheAsync()
228228
Assert.That(person.NotNullData, Is.EqualTo(notNullData));
229229
}
230230
}
231+
232+
[Test]
233+
public async Task CanCommitFromSessionTransactionAsync()
234+
{
235+
int id;
236+
using (var s = OpenSession())
237+
using (s.BeginTransaction())
238+
{
239+
var person = new Person();
240+
await (s.SaveAsync(person));
241+
id = person.Id;
242+
243+
await (s.CurrentTransaction.CommitAsync());
244+
}
245+
246+
using (var s = OpenSession())
247+
using (var t = s.BeginTransaction())
248+
{
249+
var person = await (s.GetAsync<Person>(id));
250+
Assert.That(person, Is.Not.Null);
251+
await (t.CommitAsync());
252+
}
253+
}
254+
255+
[Test]
256+
public async Task CanRollbackFromSessionTransactionAsync()
257+
{
258+
int id;
259+
using (var s = OpenSession())
260+
using (s.BeginTransaction())
261+
{
262+
var person = new Person();
263+
await (s.SaveAsync(person));
264+
id = person.Id;
265+
266+
await (s.CurrentTransaction.RollbackAsync());
267+
}
268+
269+
using (var s = OpenSession())
270+
using (var t = s.BeginTransaction())
271+
{
272+
var person = await (s.GetAsync<Person>(id));
273+
Assert.That(person, Is.Null);
274+
await (t.CommitAsync());
275+
}
276+
}
277+
278+
[Test, Obsolete]
279+
public async Task CanCommitFromSessionObsoleteTransactionAsync()
280+
{
281+
int id;
282+
using (var s = OpenSession())
283+
using (s.BeginTransaction())
284+
{
285+
var person = new Person();
286+
await (s.SaveAsync(person));
287+
id = person.Id;
288+
289+
await (s.Transaction.CommitAsync());
290+
}
291+
292+
using (var s = OpenSession())
293+
using (var t = s.BeginTransaction())
294+
{
295+
var person = await (s.GetAsync<Person>(id));
296+
Assert.That(person, Is.Not.Null);
297+
await (t.CommitAsync());
298+
}
299+
}
300+
301+
[Test, Obsolete]
302+
public async Task CanRollbackFromSessionObsoleteTransactionAsync()
303+
{
304+
int id;
305+
using (var s = OpenSession())
306+
using (s.BeginTransaction())
307+
{
308+
var person = new Person();
309+
await (s.SaveAsync(person));
310+
id = person.Id;
311+
312+
await (s.Transaction.RollbackAsync());
313+
}
314+
315+
using (var s = OpenSession())
316+
using (var t = s.BeginTransaction())
317+
{
318+
var person = await (s.GetAsync<Person>(id));
319+
Assert.That(person, Is.Null);
320+
await (t.CommitAsync());
321+
}
322+
}
231323
}
232324
}

src/NHibernate.Test/TransactionTest/TransactionFixture.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,97 @@ public void WhenCommittingItemsWillAddThemTo2ndLevelCache()
230230
Assert.That(person.NotNullData, Is.EqualTo(notNullData));
231231
}
232232
}
233+
234+
[Test]
235+
public void CanCommitFromSessionTransaction()
236+
{
237+
int id;
238+
using (var s = OpenSession())
239+
using (s.BeginTransaction())
240+
{
241+
var person = new Person();
242+
s.Save(person);
243+
id = person.Id;
244+
245+
s.CurrentTransaction.Commit();
246+
}
247+
248+
using (var s = OpenSession())
249+
using (var t = s.BeginTransaction())
250+
{
251+
var person = s.Get<Person>(id);
252+
Assert.That(person, Is.Not.Null);
253+
t.Commit();
254+
}
255+
}
256+
257+
[Test]
258+
public void CanRollbackFromSessionTransaction()
259+
{
260+
int id;
261+
using (var s = OpenSession())
262+
using (s.BeginTransaction())
263+
{
264+
var person = new Person();
265+
s.Save(person);
266+
id = person.Id;
267+
268+
s.CurrentTransaction.Rollback();
269+
}
270+
271+
using (var s = OpenSession())
272+
using (var t = s.BeginTransaction())
273+
{
274+
var person = s.Get<Person>(id);
275+
Assert.That(person, Is.Null);
276+
t.Commit();
277+
}
278+
}
279+
280+
[Test, Obsolete]
281+
public void CanCommitFromSessionObsoleteTransaction()
282+
{
283+
int id;
284+
using (var s = OpenSession())
285+
using (s.BeginTransaction())
286+
{
287+
var person = new Person();
288+
s.Save(person);
289+
id = person.Id;
290+
291+
s.Transaction.Commit();
292+
}
293+
294+
using (var s = OpenSession())
295+
using (var t = s.BeginTransaction())
296+
{
297+
var person = s.Get<Person>(id);
298+
Assert.That(person, Is.Not.Null);
299+
t.Commit();
300+
}
301+
}
302+
303+
[Test, Obsolete]
304+
public void CanRollbackFromSessionObsoleteTransaction()
305+
{
306+
int id;
307+
using (var s = OpenSession())
308+
using (s.BeginTransaction())
309+
{
310+
var person = new Person();
311+
s.Save(person);
312+
id = person.Id;
313+
314+
s.Transaction.Rollback();
315+
}
316+
317+
using (var s = OpenSession())
318+
using (var t = s.BeginTransaction())
319+
{
320+
var person = s.Get<Person>(id);
321+
Assert.That(person, Is.Null);
322+
t.Commit();
323+
}
324+
}
233325
}
234326
}

0 commit comments

Comments
 (0)