|
| 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 | + await (session.FlushBatcherAsync()); |
| 108 | + |
| 109 | + transaction.Complete(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + using (var session = OpenSession()) |
| 114 | + { |
| 115 | + var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>()); |
| 116 | + |
| 117 | + Assert.That(results.Count, Is.EqualTo(count)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public async Task ShouldWorkWithoutTransactionAsync() |
| 123 | + { |
| 124 | + const int count = 3; |
| 125 | + var entities = new List<TestEntity>(count); |
| 126 | + for (var i = 0; i < count; i++) |
| 127 | + { |
| 128 | + entities.Add(new TestEntity { Id = Guid.NewGuid() }); |
| 129 | + } |
| 130 | + |
| 131 | + using (var session = Sfi.OpenStatelessSession()) |
| 132 | + { |
| 133 | + foreach (var entity in entities) |
| 134 | + { |
| 135 | + await (session.InsertAsync(entity)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + using (var session = OpenSession()) |
| 140 | + { |
| 141 | + var results = await (session.QueryOver<TestEntity>().ListAsync<TestEntity>()); |
| 142 | + |
| 143 | + Assert.That(results.Count, Is.EqualTo(count)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [Test] |
| 148 | + public async Task ShouldGetEntityAfterInsertAsync() |
| 149 | + { |
| 150 | + var entity = new TestEntity { Id = Guid.NewGuid() }; |
| 151 | + using (var session = Sfi.OpenStatelessSession()) |
| 152 | + { |
| 153 | + await (session.InsertAsync(entity)); |
| 154 | + Assert.That(await (session.GetAsync<TestEntity>(entity.Id)), Is.Not.Null); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public async Task ShouldQueryEntityAfterInsertAsync() |
| 160 | + { |
| 161 | + var entity = new TestEntity { Id = Guid.NewGuid() }; |
| 162 | + using (var session = Sfi.OpenStatelessSession()) |
| 163 | + { |
| 164 | + await (session.InsertAsync(entity)); |
| 165 | + Assert.That(await (session.Query<TestEntity>().FirstOrDefaultAsync(e => e.Id == entity.Id)), Is.Not.Null); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + [Test] |
| 170 | + public async Task ShouldQueryOverEntityAfterInsertAsync() |
| 171 | + { |
| 172 | + var entity = new TestEntity { Id = Guid.NewGuid() }; |
| 173 | + using (var session = Sfi.OpenStatelessSession()) |
| 174 | + { |
| 175 | + await (session.InsertAsync(entity)); |
| 176 | + Assert.That(await (session.QueryOver<TestEntity>().Where(e => e.Id == entity.Id).SingleOrDefaultAsync()), Is.Not.Null); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + [Test] |
| 181 | + public async Task ShouldHqlQueryEntityAfterInsertAsync() |
| 182 | + { |
| 183 | + var entity = new TestEntity { Id = Guid.NewGuid() }; |
| 184 | + using (var session = Sfi.OpenStatelessSession()) |
| 185 | + { |
| 186 | + await (session.InsertAsync(entity)); |
| 187 | + Assert.That(await (session.CreateQuery("from TestEntity where Id = :id").SetGuid("id", entity.Id).UniqueResultAsync()), Is.Not.Null); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments