|
| 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 NHibernate.Criterion; |
| 13 | +using NHibernate.Transform; |
| 14 | +using NUnit.Framework; |
| 15 | + |
| 16 | +namespace NHibernate.Test.NHSpecificTest.NH750 |
| 17 | +{ |
| 18 | + using System.Threading.Tasks; |
| 19 | + [TestFixture(0)] |
| 20 | + [TestFixture(1)] |
| 21 | + [TestFixture(2)] |
| 22 | + public class ManyToManyFixtureAsync : BugTestCase |
| 23 | + { |
| 24 | + private int id2; |
| 25 | + private readonly int _drivesCount; |
| 26 | + private int _withTempalteId; |
| 27 | + private int ValidDrivesCount => _drivesCount; |
| 28 | + |
| 29 | + public ManyToManyFixtureAsync(int drivesCount) |
| 30 | + { |
| 31 | + _drivesCount = drivesCount; |
| 32 | + } |
| 33 | + |
| 34 | + protected override void OnSetUp() |
| 35 | + { |
| 36 | + Drive dr1 = new Drive("Drive 1"); |
| 37 | + Drive dr2 = new Drive("Drive 2"); |
| 38 | + Drive dr3 = new Drive("Drive 3"); |
| 39 | + Device dv1 = new Device("Device 1"); |
| 40 | + Device dv2 = new Device("Device 2"); |
| 41 | + var withTempalte = new Device("Device With Device 2 template") { Template = dv2 }; |
| 42 | + using var s = Sfi.OpenSession(); |
| 43 | + using var t = s.BeginTransaction(); |
| 44 | + s.Save(dr1); |
| 45 | + s.Save(dr2); |
| 46 | + s.Save(dr3); |
| 47 | + AddDrive(dv1, dr2); |
| 48 | + AddDrive(dv1, dr1); |
| 49 | + AddDrive(dv2, dr3); |
| 50 | + AddDrive(dv2, dr1); |
| 51 | + |
| 52 | + s.Save(dv1); |
| 53 | + id2 = (int) s.Save(dv2); |
| 54 | + _withTempalteId = (int)s.Save(withTempalte); |
| 55 | + t.Commit(); |
| 56 | + } |
| 57 | + |
| 58 | + private void AddDrive(Device dv, Drive drive) |
| 59 | + { |
| 60 | + if(dv.DrivesNotIgnored.Count >= _drivesCount) |
| 61 | + return; |
| 62 | + dv.DrivesNotIgnored.Add(drive); |
| 63 | + } |
| 64 | + |
| 65 | + protected override void OnTearDown() |
| 66 | + { |
| 67 | + using var s = Sfi.OpenSession(); |
| 68 | + using var t = s.BeginTransaction(); |
| 69 | + |
| 70 | + s.CreateSQLQuery("delete from DriveOfDevice").ExecuteUpdate(); |
| 71 | + s.Delete("from Device"); |
| 72 | + s.Delete("from Drive"); |
| 73 | + t.Commit(); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public async Task QueryOverFetchAsync() |
| 78 | + { |
| 79 | + using var log = new SqlLogSpy(); |
| 80 | + using var s = OpenSession(); |
| 81 | + var dv2 = await (s.QueryOver<Device>() |
| 82 | + .Fetch(SelectMode.Fetch, x => x.DrivesNotIgnored) |
| 83 | + .Where(Restrictions.IdEq(id2)) |
| 84 | + .TransformUsing(Transformers.DistinctRootEntity) |
| 85 | + .SingleOrDefaultAsync()); |
| 86 | + |
| 87 | + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); |
| 88 | + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); |
| 89 | + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); |
| 90 | + } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public async Task QueryOverFetch2Async() |
| 94 | + { |
| 95 | + using var log = new SqlLogSpy(); |
| 96 | + using var s = OpenSession(); |
| 97 | + var withTemplate = await (s.QueryOver<Device>() |
| 98 | + .Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.DrivesNotIgnored) |
| 99 | + .Where(Restrictions.IdEq(_withTempalteId)) |
| 100 | + .TransformUsing(Transformers.DistinctRootEntity) |
| 101 | + .SingleOrDefaultAsync()); |
| 102 | + |
| 103 | + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); |
| 104 | + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True); |
| 105 | + Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); |
| 106 | + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public async Task HqlFetchAsync() |
| 111 | + { |
| 112 | + using var log = new SqlLogSpy(); |
| 113 | + using var s = OpenSession(); |
| 114 | + var dv2 = await (s.CreateQuery("from Device d left join fetch d.DrivesNotIgnored where d.id = :id") |
| 115 | + .SetResultTransformer(Transformers.DistinctRootEntity) |
| 116 | + .SetParameter("id", id2) |
| 117 | + .UniqueResultAsync<Device>()); |
| 118 | + |
| 119 | + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); |
| 120 | + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); |
| 121 | + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public async Task HqlFetch2Async() |
| 126 | + { |
| 127 | + using var log = new SqlLogSpy(); |
| 128 | + using var s = OpenSession(); |
| 129 | + var withTemplate = await (s.CreateQuery("from Device t left join fetch t.Template d left join fetch d.DrivesNotIgnored where d.id = :id") |
| 130 | + .SetResultTransformer(Transformers.DistinctRootEntity) |
| 131 | + .SetParameter("id", id2) |
| 132 | + .UniqueResultAsync<Device>()); |
| 133 | + |
| 134 | + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); |
| 135 | + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True); |
| 136 | + Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); |
| 137 | + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public async Task LazyLoadAsync() |
| 142 | + { |
| 143 | + using var log = new SqlLogSpy(); |
| 144 | + using var s = OpenSession(); |
| 145 | + |
| 146 | + var dv2 = await (s.GetAsync<Device>(id2)); |
| 147 | + |
| 148 | + await (NHibernateUtil.InitializeAsync(dv2.DrivesNotIgnored)); |
| 149 | + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); |
| 150 | + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); |
| 151 | + // First query for Device, second for Drives collection |
| 152 | + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(2)); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments