|
| 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.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | +using System.Text; |
| 14 | +using System.Threading.Tasks; |
| 15 | +using NHibernate.Cache; |
| 16 | +using NHibernate.Cfg; |
| 17 | +using NUnit.Framework; |
| 18 | + |
| 19 | +namespace NHibernate.Test.LazyGroup |
| 20 | +{ |
| 21 | + using System.Threading; |
| 22 | + [TestFixture] |
| 23 | + public class LazyGroupFixtureAsync : TestCase |
| 24 | + { |
| 25 | + protected override string MappingsAssembly => "NHibernate.Test"; |
| 26 | + |
| 27 | + protected override string[] Mappings => new[] { "LazyGroup.Mappings.hbm.xml" }; |
| 28 | + |
| 29 | + protected override void Configure(Configuration configuration) |
| 30 | + { |
| 31 | + base.Configure(configuration); |
| 32 | + configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName; |
| 33 | + configuration.Properties[Environment.UseSecondLevelCache] = "true"; |
| 34 | + configuration.Properties[Environment.GenerateStatistics] = "true"; |
| 35 | + } |
| 36 | + |
| 37 | + protected override void OnSetUp() |
| 38 | + { |
| 39 | + using (var s = OpenSession()) |
| 40 | + using (var tx = s.BeginTransaction()) |
| 41 | + { |
| 42 | + for (var i = 1; i <= 5; i++) |
| 43 | + { |
| 44 | + var person = GeneratePerson(i); |
| 45 | + s.Save(person); |
| 46 | + } |
| 47 | + |
| 48 | + tx.Commit(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected override void OnTearDown() |
| 53 | + { |
| 54 | + using (var s = OpenSession()) |
| 55 | + using (var tx = s.BeginTransaction()) |
| 56 | + { |
| 57 | + s.CreateQuery("delete from Person").ExecuteUpdate(); |
| 58 | + tx.Commit(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public async Task TestGroupsAsync() |
| 64 | + { |
| 65 | + using (var s = OpenSession()) |
| 66 | + { |
| 67 | + var person = await (s.GetAsync<Person>(1)); |
| 68 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Name"), Is.True); |
| 69 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False); |
| 70 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False); |
| 71 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False); |
| 72 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 73 | + |
| 74 | + var nickName = person.NickName; |
| 75 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True); |
| 76 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False); |
| 77 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False); |
| 78 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 79 | + Assert.That(nickName, Is.EqualTo("NickName1")); |
| 80 | + |
| 81 | + var address = person.Address; |
| 82 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.True); |
| 83 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False); |
| 84 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 85 | + Assert.That(address.City, Is.EqualTo("City1")); |
| 86 | + Assert.That(address.Street, Is.EqualTo("Street1")); |
| 87 | + Assert.That(address.PostCode, Is.EqualTo(1001)); |
| 88 | + |
| 89 | + var image = person.Image; |
| 90 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True); |
| 91 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 92 | + Assert.That(person.Image, Has.Length.EqualTo(1)); |
| 93 | + |
| 94 | + var age = person.Age; |
| 95 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.True); |
| 96 | + Assert.That(person.Age, Is.EqualTo(1)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + [TestCase(true)] |
| 101 | + [TestCase(false)] |
| 102 | + public async Task TestUpdateAsync(bool fetchBeforeUpdate, CancellationToken cancellationToken = default(CancellationToken)) |
| 103 | + { |
| 104 | + Sfi.Statistics.Clear(); |
| 105 | + |
| 106 | + using (var s = OpenSession()) |
| 107 | + using (var tx = s.BeginTransaction()) |
| 108 | + { |
| 109 | + var person = await (s.GetAsync<Person>(1, cancellationToken)); |
| 110 | + if (fetchBeforeUpdate) |
| 111 | + { |
| 112 | + var nickName = person.NickName; |
| 113 | + } |
| 114 | + |
| 115 | + person.NickName = "test"; |
| 116 | + |
| 117 | + await (tx.CommitAsync(cancellationToken)); |
| 118 | + } |
| 119 | + |
| 120 | + Assert.That(Sfi.Statistics.EntityUpdateCount, Is.EqualTo(1)); |
| 121 | + |
| 122 | + using (var s = OpenSession()) |
| 123 | + using (var tx = s.BeginTransaction()) |
| 124 | + { |
| 125 | + var person = await (s.GetAsync<Person>(1, cancellationToken)); |
| 126 | + Assert.That(person.NickName, Is.EqualTo("test")); |
| 127 | + |
| 128 | + person.NickName = "NickName1"; // reset name |
| 129 | + |
| 130 | + await (tx.CommitAsync(cancellationToken)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + public async Task TestCacheAsync() |
| 136 | + { |
| 137 | + var persister = Sfi.GetEntityPersister(typeof(Person).FullName); |
| 138 | + var cache = (HashtableCache) persister.Cache.Cache; |
| 139 | + await (cache.ClearAsync(CancellationToken.None)); |
| 140 | + |
| 141 | + using (var s = OpenSession()) |
| 142 | + using (var tx = s.BeginTransaction()) |
| 143 | + { |
| 144 | + var person = await (s.GetAsync<Person>(1)); |
| 145 | + |
| 146 | + var nickName = person.NickName; |
| 147 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True); |
| 148 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False); |
| 149 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False); |
| 150 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 151 | + Assert.That(nickName, Is.EqualTo("NickName1")); |
| 152 | + |
| 153 | + await (tx.CommitAsync()); |
| 154 | + } |
| 155 | + |
| 156 | + using (var s = OpenSession()) |
| 157 | + using (var tx = s.BeginTransaction()) |
| 158 | + { |
| 159 | + var person = await (s.GetAsync<Person>(1)); |
| 160 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True); |
| 161 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False); |
| 162 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False); |
| 163 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 164 | + Assert.That(person.NickName, Is.EqualTo("NickName1")); |
| 165 | + |
| 166 | + await (tx.CommitAsync()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + [Test] |
| 171 | + public async Task TestInitializeFromCacheAsync() |
| 172 | + { |
| 173 | + var persister = Sfi.GetEntityPersister(typeof(Person).FullName); |
| 174 | + var cache = (HashtableCache) persister.Cache.Cache; |
| 175 | + await (cache.ClearAsync(CancellationToken.None)); |
| 176 | + Sfi.Statistics.Clear(); |
| 177 | + |
| 178 | + using (var s = OpenSession()) |
| 179 | + using (var tx = s.BeginTransaction()) |
| 180 | + { |
| 181 | + var person = await (s.GetAsync<Person>(1)); |
| 182 | + |
| 183 | + await (InitializeImageAsync()); |
| 184 | + |
| 185 | + Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2)); |
| 186 | + |
| 187 | + var image = person.Image; // Should be initialized from cache |
| 188 | + |
| 189 | + Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2)); |
| 190 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False); |
| 191 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False); |
| 192 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True); |
| 193 | + Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False); |
| 194 | + Assert.That(image, Has.Length.EqualTo(1)); |
| 195 | + |
| 196 | + await (tx.CommitAsync()); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private async Task InitializeImageAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 201 | + { |
| 202 | + using (var s = OpenSession()) |
| 203 | + using (var tx = s.BeginTransaction()) |
| 204 | + { |
| 205 | + var person = await (s.GetAsync<Person>(1, cancellationToken)); |
| 206 | + var image = person.Image; |
| 207 | + |
| 208 | + await (tx.CommitAsync(cancellationToken)); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + private static Person GeneratePerson(int i) |
| 213 | + { |
| 214 | + return new Person |
| 215 | + { |
| 216 | + Id = i, |
| 217 | + Name = $"Person{i}", |
| 218 | + Address = new Address |
| 219 | + { |
| 220 | + City = $"City{i}", |
| 221 | + PostCode = 1000+i, |
| 222 | + Street = $"Street{i}" |
| 223 | + }, |
| 224 | + Image = new byte[i], |
| 225 | + NickName = $"NickName{i}" |
| 226 | + }; |
| 227 | + |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments