Skip to content

Commit

Permalink
Generate async files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Aug 3, 2023
1 parent 70d9322 commit 373d8b2
Showing 1 changed file with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@


using System;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH750
{
using System.Threading.Tasks;
[TestFixture]
[TestFixture(0)]
[TestFixture(1)]
[TestFixture(2)]
public class ManyToManyNotFoundIgnoreFixtureAsync : BugTestCase
{
private int id1;
private int id2;
private readonly int _drivesCount;
private int DrivesCountWithOneIgnored => _drivesCount == 0? 0 : _drivesCount - 1;

public ManyToManyNotFoundIgnoreFixtureAsync(int drivesCount)
{
_drivesCount = drivesCount;
}

protected override void OnSetUp()
{
Expand All @@ -36,10 +44,10 @@ protected override void OnSetUp()
s.Save(dr1);
s.Save(dr2);
s.Save(dr3);
dv1.Drives.Add(dr1);
dv1.Drives.Add(dr2);
dv2.Drives.Add(dr1);
dv2.Drives.Add(dr3);
AddDrive(dv1, dr2);
AddDrive(dv1, dr1);
AddDrive(dv2, dr3);
AddDrive(dv2, dr1);

id1 = (int) s.Save(dv1);
id2 = (int) s.Save(dv2);
Expand All @@ -51,11 +59,19 @@ protected override void OnSetUp()
}
}

private void AddDrive(Device dv, Drive drive)
{
if(dv.Drives.Count >= _drivesCount)
return;
dv.Drives.Add(drive);
}

protected override void OnTearDown()
{
using (ISession s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateSQLQuery("delete from DriveOfDevice").ExecuteUpdate();
s.Delete("from Device");
s.Delete("from Drive");
t.Commit();
Expand All @@ -73,9 +89,9 @@ public async Task DeviceOfDriveAsync()
dv2 = (Device) await (s.LoadAsync(typeof(Device), id2));
}

Assert.That(dv1.Drives, Has.Count.EqualTo(2).And.None.Null);
Assert.That(dv1.Drives, Has.Count.EqualTo(_drivesCount).And.None.Null);
// Verify one is missing
Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null);
Assert.That(dv2.Drives, Has.Count.EqualTo(DrivesCountWithOneIgnored).And.None.Null);

//Make sure that flush didn't touch not-found="ignore" records for not modified collection
using (var s = Sfi.OpenSession())
Expand All @@ -86,18 +102,22 @@ public async Task DeviceOfDriveAsync()
await (t.CommitAsync());
}

await (VerifyResultAsync(expectedInCollection: 1, expectedInDb: 2, msg: "not modified collection"));
await (VerifyResultAsync(expectedInCollection: DrivesCountWithOneIgnored, expectedInDb: _drivesCount, msg: "not modified collection"));

//Many-to-many clears collection and recreates it so not-found ignore records are lost
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
dv2 = await (s.GetAsync<Device>(dv2.Id));
dv2.Drives.Add(dv1.Drives[1]);
if(_drivesCount > 0)
dv2.Drives.Add(dv1.Drives[0]);
await (t.CommitAsync());
}

await (VerifyResultAsync(2, 2, msg: "modified collection"));
if(_drivesCount == 1)
Assert.Ignore("Test case fails for unrelated reasons");

await (VerifyResultAsync(_drivesCount, _drivesCount, msg: "modified collection"));

async Task VerifyResultAsync(int expectedInCollection, int expectedInDb, string msg)
{
Expand Down Expand Up @@ -127,23 +147,23 @@ public async Task QueryOverFetchAsync()
.SingleOrDefaultAsync());

Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True);
Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null);
Assert.That(dv2.Drives, Has.Count.EqualTo(DrivesCountWithOneIgnored).And.None.Null);
}
}

[Test]
public async Task HqlFetchAsync()
{
using (var s = OpenSession())
{
var dv2 = await (s.CreateQuery("from Device d left join fetch d.Drives where d.id = :id")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetParameter("id", id2)
.UniqueResultAsync<Device>());

Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True);
Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null);
}
using var log = new SqlLogSpy();
using var s = OpenSession();
var dv2 = await (s.CreateQuery("from Device d left join fetch d.Drives where d.id = :id")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetParameter("id", id2)
.UniqueResultAsync<Device>());

Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True);
Assert.That(dv2.Drives, Has.Count.EqualTo(DrivesCountWithOneIgnored).And.None.Null);
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}

[Test]
Expand All @@ -155,7 +175,7 @@ public async Task LazyLoadAsync()
await (NHibernateUtil.InitializeAsync(dv2.Drives));

Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True);
Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null);
Assert.That(dv2.Drives, Has.Count.EqualTo(DrivesCountWithOneIgnored).And.None.Null);
}
}
}
Expand Down

0 comments on commit 373d8b2

Please sign in to comment.