Skip to content

Commit 61a2e9d

Browse files
Test more cases related to #2750
1 parent 3f7dedb commit 61a2e9d

File tree

2 files changed

+300
-5
lines changed

2 files changed

+300
-5
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Transactions;
15+
using NHibernate.Cfg;
16+
using NHibernate.Cfg.MappingSchema;
17+
using NHibernate.Mapping.ByCode;
18+
using NUnit.Framework;
19+
using NHibernate.Linq;
20+
21+
namespace NHibernate.Test.NHSpecificTest.GH2750
22+
{
23+
using System.Threading.Tasks;
24+
[TestFixture]
25+
public class ByCodeFixtureAsync : TestCaseMappingByCode
26+
{
27+
protected override HbmMapping GetMappings()
28+
{
29+
var mapper = new ModelMapper();
30+
mapper.Class<TestEntity>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); });
31+
32+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
33+
}
34+
35+
protected override void Configure(Configuration configuration)
36+
{
37+
configuration.SetProperty(Cfg.Environment.BatchSize, 10.ToString());
38+
configuration.SetProperty(Cfg.Environment.UseConnectionOnSystemTransactionPrepare, true.ToString());
39+
}
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var session = OpenSession())
44+
using (var transaction = session.BeginTransaction())
45+
{
46+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
47+
48+
transaction.Commit();
49+
}
50+
}
51+
52+
[Test]
53+
public async Task ShouldWorkWithOuterSystemTransactionAsync()
54+
{
55+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
56+
Assert.Ignore("System.Transactions support is required by this test");
57+
58+
const int count = 3;
59+
var entities = new List<TestEntity>(count);
60+
for (var i = 0; i < count; i++)
61+
{
62+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
63+
}
64+
65+
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
66+
{
67+
using (var session = Sfi.OpenStatelessSession())
68+
{
69+
foreach (var entity in entities)
70+
{
71+
await (session.InsertAsync(entity));
72+
}
73+
}
74+
75+
transaction.Complete();
76+
}
77+
78+
using (var session = OpenSession())
79+
{
80+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
81+
82+
Assert.That(results.Count, Is.EqualTo(count));
83+
}
84+
}
85+
86+
[Test]
87+
public async Task ShouldWorkWithInnerSystemTransactionAsync()
88+
{
89+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
90+
Assert.Ignore("System.Transactions support is required by this test");
91+
92+
const int count = 3;
93+
var entities = new List<TestEntity>(count);
94+
for (var i = 0; i < count; i++)
95+
{
96+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
97+
}
98+
99+
using (var session = Sfi.OpenStatelessSession())
100+
{
101+
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
102+
{
103+
foreach (var entity in entities)
104+
{
105+
await (session.InsertAsync(entity));
106+
}
107+
108+
transaction.Complete();
109+
}
110+
}
111+
112+
using (var session = OpenSession())
113+
{
114+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
115+
116+
Assert.That(results.Count, Is.EqualTo(count));
117+
}
118+
}
119+
120+
[Test]
121+
public async Task ShouldWorkWithoutTransactionAsync()
122+
{
123+
const int count = 3;
124+
var entities = new List<TestEntity>(count);
125+
for (var i = 0; i < count; i++)
126+
{
127+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
128+
}
129+
130+
using (var session = Sfi.OpenStatelessSession())
131+
{
132+
foreach (var entity in entities)
133+
{
134+
await (session.InsertAsync(entity));
135+
}
136+
}
137+
138+
using (var session = OpenSession())
139+
{
140+
var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>());
141+
142+
Assert.That(results.Count, Is.EqualTo(count));
143+
}
144+
}
145+
146+
[Test]
147+
public async Task ShouldGetEntityAfterInsertAsync()
148+
{
149+
var entity = new TestEntity { Id = Guid.NewGuid() };
150+
using (var session = Sfi.OpenStatelessSession())
151+
{
152+
await (session.InsertAsync(entity));
153+
Assert.That(await (session.GetAsync<TestEntity>(entity.Id)), Is.Not.Null);
154+
}
155+
}
156+
157+
[Test]
158+
public async Task ShouldQueryEntityAfterInsertAsync()
159+
{
160+
var entity = new TestEntity { Id = Guid.NewGuid() };
161+
using (var session = Sfi.OpenStatelessSession())
162+
{
163+
await (session.InsertAsync(entity));
164+
Assert.That(await (session.Query<TestEntity>().FirstOrDefaultAsync(e => e.Id == entity.Id)), Is.Not.Null);
165+
}
166+
}
167+
168+
[Test]
169+
public async Task ShouldQueryOverEntityAfterInsertAsync()
170+
{
171+
var entity = new TestEntity { Id = Guid.NewGuid() };
172+
using (var session = Sfi.OpenStatelessSession())
173+
{
174+
await (session.InsertAsync(entity));
175+
Assert.That(await (session.QueryOver<TestEntity>().Where(e => e.Id == entity.Id).SingleOrDefaultAsync()), Is.Not.Null);
176+
}
177+
}
178+
179+
[Test]
180+
public async Task ShouldHqlQueryEntityAfterInsertAsync()
181+
{
182+
var entity = new TestEntity { Id = Guid.NewGuid() };
183+
using (var session = Sfi.OpenStatelessSession())
184+
{
185+
await (session.InsertAsync(entity));
186+
Assert.That(await (session.CreateQuery("from TestEntity where Id = :id").SetGuid("id", entity.Id).UniqueResultAsync()), Is.Not.Null);
187+
}
188+
}
189+
}
190+
}

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

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Transactions;
45
using NHibernate.Cfg;
56
using NHibernate.Cfg.MappingSchema;
@@ -14,10 +15,7 @@ public class ByCodeFixture : TestCaseMappingByCode
1415
protected override HbmMapping GetMappings()
1516
{
1617
var mapper = new ModelMapper();
17-
mapper.Class<TestEntity>(rc =>
18-
{
19-
rc.Id(x => x.Id, m => m.Generator(Generators.Assigned));
20-
});
18+
mapper.Class<TestEntity>(rc => { rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); });
2119

2220
return mapper.CompileMappingForAllExplicitlyAddedEntities();
2321
}
@@ -40,8 +38,11 @@ protected override void OnTearDown()
4038
}
4139

4240
[Test]
43-
public void ShouldWorkWithSystemTransaction()
41+
public void ShouldWorkWithOuterSystemTransaction()
4442
{
43+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
44+
Assert.Ignore("System.Transactions support is required by this test");
45+
4546
const int count = 3;
4647
var entities = new List<TestEntity>(count);
4748
for (var i = 0; i < count; i++)
@@ -69,5 +70,109 @@ public void ShouldWorkWithSystemTransaction()
6970
Assert.That(results.Count, Is.EqualTo(count));
7071
}
7172
}
73+
74+
[Test]
75+
public void ShouldWorkWithInnerSystemTransaction()
76+
{
77+
if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
78+
Assert.Ignore("System.Transactions support is required by this test");
79+
80+
const int count = 3;
81+
var entities = new List<TestEntity>(count);
82+
for (var i = 0; i < count; i++)
83+
{
84+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
85+
}
86+
87+
using (var session = Sfi.OpenStatelessSession())
88+
{
89+
using (var transaction = new TransactionScope())
90+
{
91+
foreach (var entity in entities)
92+
{
93+
session.Insert(entity);
94+
}
95+
96+
transaction.Complete();
97+
}
98+
}
99+
100+
using (var session = OpenSession())
101+
{
102+
var results = session.QueryOver<TestEntity>().List<TestEntity>();
103+
104+
Assert.That(results.Count, Is.EqualTo(count));
105+
}
106+
}
107+
108+
[Test]
109+
public void ShouldWorkWithoutTransaction()
110+
{
111+
const int count = 3;
112+
var entities = new List<TestEntity>(count);
113+
for (var i = 0; i < count; i++)
114+
{
115+
entities.Add(new TestEntity { Id = Guid.NewGuid() });
116+
}
117+
118+
using (var session = Sfi.OpenStatelessSession())
119+
{
120+
foreach (var entity in entities)
121+
{
122+
session.Insert(entity);
123+
}
124+
}
125+
126+
using (var session = OpenSession())
127+
{
128+
var results = session.QueryOver<TestEntity>().List<TestEntity>();
129+
130+
Assert.That(results.Count, Is.EqualTo(count));
131+
}
132+
}
133+
134+
[Test]
135+
public void ShouldGetEntityAfterInsert()
136+
{
137+
var entity = new TestEntity { Id = Guid.NewGuid() };
138+
using (var session = Sfi.OpenStatelessSession())
139+
{
140+
session.Insert(entity);
141+
Assert.That(session.Get<TestEntity>(entity.Id), Is.Not.Null);
142+
}
143+
}
144+
145+
[Test]
146+
public void ShouldQueryEntityAfterInsert()
147+
{
148+
var entity = new TestEntity { Id = Guid.NewGuid() };
149+
using (var session = Sfi.OpenStatelessSession())
150+
{
151+
session.Insert(entity);
152+
Assert.That(session.Query<TestEntity>().FirstOrDefault(e => e.Id == entity.Id), Is.Not.Null);
153+
}
154+
}
155+
156+
[Test]
157+
public void ShouldQueryOverEntityAfterInsert()
158+
{
159+
var entity = new TestEntity { Id = Guid.NewGuid() };
160+
using (var session = Sfi.OpenStatelessSession())
161+
{
162+
session.Insert(entity);
163+
Assert.That(session.QueryOver<TestEntity>().Where(e => e.Id == entity.Id).SingleOrDefault(), Is.Not.Null);
164+
}
165+
}
166+
167+
[Test]
168+
public void ShouldHqlQueryEntityAfterInsert()
169+
{
170+
var entity = new TestEntity { Id = Guid.NewGuid() };
171+
using (var session = Sfi.OpenStatelessSession())
172+
{
173+
session.Insert(entity);
174+
Assert.That(session.CreateQuery("from TestEntity where Id = :id").SetGuid("id", entity.Id).UniqueResult(), Is.Not.Null);
175+
}
176+
}
72177
}
73178
}

0 commit comments

Comments
 (0)