|
| 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.Linq; |
| 12 | +using NHibernate.Engine; |
| 13 | +using NHibernate.Persister.Entity; |
| 14 | +using NUnit.Framework; |
| 15 | +using NHibernate.Linq; |
| 16 | + |
| 17 | +namespace NHibernate.Test.NHSpecificTest.GH1226 |
| 18 | +{ |
| 19 | + using System.Threading.Tasks; |
| 20 | + [TestFixture] |
| 21 | + public class FixtureAsync : BugTestCase |
| 22 | + { |
| 23 | + protected override void OnSetUp() |
| 24 | + { |
| 25 | + base.OnSetUp(); |
| 26 | + |
| 27 | + using (var session = OpenSession()) |
| 28 | + { |
| 29 | + using (var tx = session.BeginTransaction()) |
| 30 | + { |
| 31 | + var bank = new Bank { Code = "01234" }; |
| 32 | + session.Save(bank); |
| 33 | + |
| 34 | + var account = new Account { Bank = bank }; |
| 35 | + session.Save(account); |
| 36 | + |
| 37 | + var account2 = new Account { Bank = bank }; |
| 38 | + session.Save(account2); |
| 39 | + |
| 40 | + tx.Commit(); |
| 41 | + } |
| 42 | + } |
| 43 | + Sfi.Statistics.IsStatisticsEnabled = true; |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public async Task BankShouldBeJoinFetchedAsync() |
| 48 | + { |
| 49 | + // Simple case: nothing already in session. |
| 50 | + using (var session = OpenSession()) |
| 51 | + using (var tx = session.BeginTransaction()) |
| 52 | + { |
| 53 | + var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; |
| 54 | + |
| 55 | + var accounts = await (session.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); |
| 56 | + var associatedBanks = accounts.Select(x => x.Bank).ToList(); |
| 57 | + Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), |
| 58 | + "One bank or more was lazily loaded."); |
| 59 | + |
| 60 | + var countAfterQuery = Sfi.Statistics.PrepareStatementCount; |
| 61 | + var statementCount = countAfterQuery - countBeforeQuery; |
| 62 | + |
| 63 | + await (tx.CommitAsync()); |
| 64 | + |
| 65 | + Assert.That(statementCount, Is.EqualTo(1)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public async Task InSessionBankShouldBeJoinFetchedAsync() |
| 71 | + { |
| 72 | + using (var session = OpenSession()) |
| 73 | + using (var tx = session.BeginTransaction()) |
| 74 | + { |
| 75 | + // #1226 bug only occurs if the Banks are already in the session cache. |
| 76 | + await (session.CreateQuery("from Bank").ListAsync<Bank>()); |
| 77 | + |
| 78 | + var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; |
| 79 | + |
| 80 | + var accounts = await (session.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); |
| 81 | + var associatedBanks = accounts.Select(x => x.Bank).ToList(); |
| 82 | + Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), |
| 83 | + "One bank or more was lazily loaded."); |
| 84 | + |
| 85 | + var countAfterQuery = Sfi.Statistics.PrepareStatementCount; |
| 86 | + var statementCount = countAfterQuery - countBeforeQuery; |
| 87 | + |
| 88 | + await (tx.CommitAsync()); |
| 89 | + |
| 90 | + Assert.That(statementCount, Is.EqualTo(1)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public async Task AlteredBankShouldBeJoinFetchedAsync() |
| 96 | + { |
| 97 | + using (var s1 = OpenSession()) |
| 98 | + { |
| 99 | + using (var tx = s1.BeginTransaction()) |
| 100 | + { |
| 101 | + // Put them all in s1 cache. |
| 102 | + await (s1.CreateQuery("from Bank").ListAsync()); |
| 103 | + await (tx.CommitAsync()); |
| 104 | + } |
| 105 | + |
| 106 | + string oldCode; |
| 107 | + const string newCode = "12345"; |
| 108 | + // Alter the bank code with another session. |
| 109 | + using (var s2 = OpenSession()) |
| 110 | + using (var tx2 = s2.BeginTransaction()) |
| 111 | + { |
| 112 | + var accounts = await (s2.Query<Account>().ToListAsync()); |
| 113 | + foreach (var account in accounts) |
| 114 | + account.Bank = null; |
| 115 | + await (s2.FlushAsync()); |
| 116 | + var bank = await (s2.Query<Bank>().SingleAsync()); |
| 117 | + oldCode = bank.Code; |
| 118 | + bank.Code = newCode; |
| 119 | + await (s2.FlushAsync()); |
| 120 | + foreach (var account in accounts) |
| 121 | + account.Bank = bank; |
| 122 | + await (tx2.CommitAsync()); |
| 123 | + } |
| 124 | + |
| 125 | + // Check querying them with s1 is still consistent |
| 126 | + using (var tx = s1.BeginTransaction()) |
| 127 | + { |
| 128 | + var accounts = await (s1.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); |
| 129 | + var associatedBanks = accounts.Select(x => x.Bank).ToList(); |
| 130 | + Assert.That(associatedBanks, Has.All.Not.Null, |
| 131 | + "One bank or more failed loading."); |
| 132 | + Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), |
| 133 | + "One bank or more was lazily loaded."); |
| 134 | + Assert.That(associatedBanks, Has.All.Property(nameof(Bank.Code)).EqualTo(oldCode), |
| 135 | + "One bank or more has no more the old code."); |
| 136 | + |
| 137 | + await (tx.CommitAsync()); |
| 138 | + // Do not check statements count: we are in a special case defeating the eager fetching, because |
| 139 | + // we have stale data in session for the bank code. |
| 140 | + // But check that the new code, supposed to be unknown for the session, is not cached. |
| 141 | + var persister = Sfi.GetEntityPersister(typeof(Bank).FullName); |
| 142 | + var index = ((IUniqueKeyLoadable) persister).GetPropertyIndex(nameof(Bank.Code)); |
| 143 | + var type = persister.PropertyTypes[index]; |
| 144 | + var euk = new EntityUniqueKey(persister.EntityName, nameof(Bank.Code), newCode, type, Sfi); |
| 145 | + Assert.That(s1.GetSessionImplementation().PersistenceContext.GetEntity(euk), |
| 146 | + Is.Null, "Found a bank associated to the new code in s1"); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + protected override void OnTearDown() |
| 152 | + { |
| 153 | + base.OnTearDown(); |
| 154 | + |
| 155 | + using (var session = OpenSession()) |
| 156 | + using (var tx = session.BeginTransaction()) |
| 157 | + { |
| 158 | + session.CreateQuery("delete from Account").ExecuteUpdate(); |
| 159 | + session.CreateQuery("delete from Bank").ExecuteUpdate(); |
| 160 | + tx.Commit(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments