|
| 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 NHibernate.Cfg; |
| 15 | +using NHibernate.Cfg.MappingSchema; |
| 16 | +using NHibernate.Loader; |
| 17 | +using NUnit.Framework; |
| 18 | +using Environment = NHibernate.Cfg.Environment; |
| 19 | + |
| 20 | +namespace NHibernate.Test.NHSpecificTest.NH3530 |
| 21 | +{ |
| 22 | + using System.Threading.Tasks; |
| 23 | + using System.Threading; |
| 24 | + //NH-3530 (GH-1316) |
| 25 | + [TestFixture(BatchFetchStyle.Dynamic)] |
| 26 | + [TestFixture(BatchFetchStyle.Legacy)] |
| 27 | + public class BatchFetchStyleFixtureAsync : TestCaseMappingByCode |
| 28 | + { |
| 29 | + private readonly BatchFetchStyle _fetchStyle; |
| 30 | + private readonly List<object> _ids = new List<object>(); |
| 31 | + |
| 32 | + public BatchFetchStyleFixtureAsync(BatchFetchStyle fetchStyle) |
| 33 | + { |
| 34 | + _fetchStyle = fetchStyle; |
| 35 | + } |
| 36 | + |
| 37 | + protected override void Configure(Configuration configuration) |
| 38 | + { |
| 39 | + base.Configure(configuration); |
| 40 | + configuration.SetProperty(Environment.BatchFetchStyle, _fetchStyle.ToString()); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public async Task CanLoadEntityAsync() |
| 45 | + { |
| 46 | + await (PrepareEntitiesAsync(2)); |
| 47 | + |
| 48 | + using (var session = OpenSession()) |
| 49 | + { |
| 50 | + var proxy = await (session.LoadAsync<Entity>(_ids[0])); |
| 51 | + var result = await (session.GetAsync<Entity>(_ids[1])); |
| 52 | + |
| 53 | + Assert.That(result.Name, Is.Not.Null); |
| 54 | + |
| 55 | + var childrenCount = result.Children.Count; |
| 56 | + //Assert.That(NHibernateUtil.IsInitialized(proxy), Is.True); |
| 57 | + Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True); |
| 58 | + Assert.That(childrenCount, Is.EqualTo(4)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [KnownBug("GH-2960")] |
| 63 | + [Test] |
| 64 | + public async Task CanLoadComponentEntityAsync() |
| 65 | + { |
| 66 | + await (PrepareComponentEntitiesAsync(2)); |
| 67 | + |
| 68 | + using (var session = OpenSession()) |
| 69 | + { |
| 70 | + var proxy = await (session.LoadAsync<EntityComponent>(_ids[0])); |
| 71 | + var result = await (session.GetAsync<EntityComponent>(_ids[1])); |
| 72 | + |
| 73 | + Assert.That(result.Name, Is.Not.Null); |
| 74 | + |
| 75 | + var childrenCount = result.Children.Count; |
| 76 | + Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True); |
| 77 | + Assert.That(childrenCount, Is.EqualTo(4)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public async Task CanLoadComponentIdEntityAsync() |
| 83 | + { |
| 84 | + await (PrepareComponentIdEntitiesAsync(2)); |
| 85 | + |
| 86 | + using (var session = OpenSession()) |
| 87 | + { |
| 88 | + var proxy = await (session.LoadAsync<EntityComponentId>(_ids[0])); |
| 89 | + var result = await (session.GetAsync<EntityComponentId>(_ids[1])); |
| 90 | + |
| 91 | + Assert.That(result.Name, Is.Not.Null); |
| 92 | + |
| 93 | + var childrenCount = result.Children.Count; |
| 94 | + Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True); |
| 95 | + Assert.That(childrenCount, Is.EqualTo(4)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [TestCase(1)] |
| 100 | + [TestCase(2)] |
| 101 | + [TestCase(3)] |
| 102 | + [TestCase(4)] |
| 103 | + [TestCase(5)] |
| 104 | + public async Task CanLoadBatchAsync(int loadCount) |
| 105 | + { |
| 106 | + await (PrepareEntitiesAsync(5)); |
| 107 | + |
| 108 | + using (var session = OpenSession()) |
| 109 | + { |
| 110 | + foreach (var id in _ids.Take(loadCount)) |
| 111 | + { |
| 112 | + await (session.LoadAsync<Entity>(id)); |
| 113 | + } |
| 114 | + |
| 115 | + var result = await (session.GetAsync<Entity>(_ids[0])); |
| 116 | + var result2 = await (session.GetAsync<Entity>(_ids[1])); |
| 117 | + var last = await (session.GetAsync<Entity>(_ids.Last())); |
| 118 | + |
| 119 | + var count = result.Children.Count; |
| 120 | + Assert.That(result.Name, Is.Not.Null); |
| 121 | + Assert.That(last.Name, Is.Not.Null); |
| 122 | + Assert.That(NHibernateUtil.IsInitialized(result2.Children), Is.True); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + protected override void OnTearDown() |
| 127 | + { |
| 128 | + using (var session = OpenSession()) |
| 129 | + using (var transaction = session.BeginTransaction()) |
| 130 | + { |
| 131 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 132 | + transaction.Commit(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected override HbmMapping GetMappings() |
| 137 | + { |
| 138 | + return EntityMappings.CreateMapping(); |
| 139 | + } |
| 140 | + |
| 141 | + private async Task PrepareEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken)) |
| 142 | + { |
| 143 | + _ids.Clear(); |
| 144 | + using (var session = OpenSession()) |
| 145 | + using (var transaction = session.BeginTransaction()) |
| 146 | + { |
| 147 | + for (int i = 0; i < count; i++) |
| 148 | + { |
| 149 | + var entity = new Entity { Name = "some name" + 1 }; |
| 150 | + AddChildren(entity.Children, 4); |
| 151 | + _ids.Add((Guid) await (session.SaveAsync(entity, cancellationToken))); |
| 152 | + } |
| 153 | + |
| 154 | + await (transaction.CommitAsync(cancellationToken)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private async Task PrepareComponentEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken)) |
| 159 | + { |
| 160 | + _ids.Clear(); |
| 161 | + using (var session = OpenSession()) |
| 162 | + using (var transaction = session.BeginTransaction()) |
| 163 | + { |
| 164 | + for (int i = 0; i < count; i++) |
| 165 | + { |
| 166 | + var entity = new EntityComponent { Id1 = i, Id2 = i + 1, Name = "some name" + 1 }; |
| 167 | + AddChildren(entity.Children, 4); |
| 168 | + |
| 169 | + _ids.Add(await (session.SaveAsync(entity, cancellationToken))); |
| 170 | + } |
| 171 | + |
| 172 | + await (transaction.CommitAsync(cancellationToken)); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private async Task PrepareComponentIdEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken)) |
| 177 | + { |
| 178 | + _ids.Clear(); |
| 179 | + using (var session = OpenSession()) |
| 180 | + using (var transaction = session.BeginTransaction()) |
| 181 | + { |
| 182 | + for (int i = 0; i < count; i++) |
| 183 | + { |
| 184 | + var entity = new EntityComponentId { Id = new ComponentId { Id1 = i, Id2 = i + 1 }, Name = "some name" + 1 }; |
| 185 | + AddChildren(entity.Children, 4); |
| 186 | + _ids.Add(await (session.SaveAsync(entity, cancellationToken))); |
| 187 | + } |
| 188 | + |
| 189 | + await (transaction.CommitAsync(cancellationToken)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void AddChildren<T>(IList<T> list, int count) where T : new() |
| 194 | + { |
| 195 | + for (int i = 0; i < count; i++) |
| 196 | + { |
| 197 | + list.Add(new T()); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments