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 5, 2023
1 parent aaf7810 commit b031a6d
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 24 deletions.
155 changes: 155 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH750/ManyToManyFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


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

namespace NHibernate.Test.NHSpecificTest.NH750
{
using System.Threading.Tasks;
[TestFixture(0)]
[TestFixture(1)]
[TestFixture(2)]
public class ManyToManyFixtureAsync : BugTestCase
{
private int id2;
private readonly int _drivesCount;
private int _withTempalteId;
private int ValidDrivesCount => _drivesCount;

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

protected override void OnSetUp()
{
Drive dr1 = new Drive("Drive 1");
Drive dr2 = new Drive("Drive 2");
Drive dr3 = new Drive("Drive 3");
Device dv1 = new Device("Device 1");
Device dv2 = new Device("Device 2");
var withTempalte = new Device("Device With Device 2 template") { Template = dv2 };
using var s = Sfi.OpenSession();
using var t = s.BeginTransaction();
s.Save(dr1);
s.Save(dr2);
s.Save(dr3);
AddDrive(dv1, dr2);
AddDrive(dv1, dr1);
AddDrive(dv2, dr3);
AddDrive(dv2, dr1);

s.Save(dv1);
id2 = (int) s.Save(dv2);
_withTempalteId = (int)s.Save(withTempalte);
t.Commit();
}

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

protected override void OnTearDown()
{
using var s = Sfi.OpenSession();
using var t = s.BeginTransaction();

s.CreateSQLQuery("delete from DriveOfDevice").ExecuteUpdate();
s.Delete("from Device");
s.Delete("from Drive");
t.Commit();
}

[Test]
public async Task QueryOverFetchAsync()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var dv2 = await (s.QueryOver<Device>()
.Fetch(SelectMode.Fetch, x => x.DrivesNotIgnored)
.Where(Restrictions.IdEq(id2))
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefaultAsync());

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

[Test]
public async Task QueryOverFetch2Async()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = await (s.QueryOver<Device>()
.Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.DrivesNotIgnored)
.Where(Restrictions.IdEq(_withTempalteId))
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefaultAsync());

Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True);
Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True);
Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null);
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}

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

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

[Test]
public async Task HqlFetch2Async()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = await (s.CreateQuery("from Device t left join fetch t.Template d left join fetch d.DrivesNotIgnored where d.id = :id")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetParameter("id", id2)
.UniqueResultAsync<Device>());

Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True);
Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True);
Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null);
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}

[Test]
public async Task LazyLoadAsync()
{
using var log = new SqlLogSpy();
using var s = OpenSession();

var dv2 = await (s.GetAsync<Device>(id2));

await (NHibernateUtil.InitializeAsync(dv2.DrivesNotIgnored));
Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True);
Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null);
// First query for Device, second for Drives collection
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(2));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ManyToManyNotFoundIgnoreFixtureAsync : BugTestCase
private int id1;
private int id2;
private int _drive2Id;
private int _withTempalteId;
private readonly int _drivesCount;
private int ValidDrivesCount => _drivesCount == 0? 0 : _drivesCount - 1;

Expand All @@ -39,25 +40,26 @@ protected override void OnSetUp()
Drive dr3 = new Drive("Drive 3");
Device dv1 = new Device("Device 1");
Device dv2 = new Device("Device 2");
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(dr1);
_drive2Id = (int)s.Save(dr2);
s.Save(dr3);
AddDrive(dv1, dr2);
AddDrive(dv1, dr1);
AddDrive(dv2, dr3);
AddDrive(dv2, dr1);

id1 = (int) s.Save(dv1);
id2 = (int) s.Save(dv2);
s.Flush();

s.Clear();
s.Delete(dr3);
t.Commit();
}
var withTempalte = new Device("Device With Device 2 template") { Template = dv2 };

using var s = Sfi.OpenSession();
using var t = s.BeginTransaction();
s.Save(dr1);
_drive2Id = (int)s.Save(dr2);
s.Save(dr3);
AddDrive(dv1, dr2);
AddDrive(dv1, dr1);
AddDrive(dv2, dr3);
AddDrive(dv2, dr1);

id1 = (int) s.Save(dv1);
id2 = (int) s.Save(dv2);
_withTempalteId = (int)s.Save(withTempalte);
s.Flush();

s.Clear();
s.Delete(dr3);
t.Commit();
}

private void AddDrive(Device dv, Drive drive)
Expand Down Expand Up @@ -87,6 +89,8 @@ public async Task DeviceOfDriveAsync()
{
dv1 = (Device) await (s.LoadAsync(typeof(Device), id1));
dv2 = (Device) await (s.LoadAsync(typeof(Device), id2));
await (NHibernateUtil.InitializeAsync(dv1.Drives));
await (NHibernateUtil.InitializeAsync(dv2.Drives));
}

Assert.That(dv1.Drives, Has.Count.EqualTo(_drivesCount).And.None.Null);
Expand Down Expand Up @@ -152,6 +156,23 @@ public async Task QueryOverFetchAsync()
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}

[Test]
public async Task QueryOverFetch2Async()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = await (s.QueryOver<Device>()
.Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.Drives)
.Where(Restrictions.IdEq(_withTempalteId))
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefaultAsync());

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

[Test]
public async Task HqlFetchAsync()
{
Expand All @@ -168,17 +189,31 @@ public async Task HqlFetchAsync()
}

[Test]
public async Task LazyLoadAsync()
public async Task HqlFetch2Async()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = await (s.CreateQuery("from Device t left join fetch t.Template d left join fetch d.Drives where d.id = :id")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetParameter("id", id2)
.UniqueResultAsync<Device>());

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

[Test]
public async Task LazyLoadAsync()
{
using var s = OpenSession();

var dv2 = await (s.GetAsync<Device>(id2));
using var log = new SqlLogSpy();

Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True);
Assert.That(dv2.Drives, Has.Count.EqualTo(ValidDrivesCount).And.None.Null);
// First query for Device, second for Drives collection
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(2));
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected override void OnTearDown()
[Test]
public async Task LazyLoadAsync()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var device = await (s.GetAsync<Device>(_id));
Assert.ThrowsAsync<ObjectNotFoundException>(() => NHibernateUtil.InitializeAsync(device.DrivesNotIgnored));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public async Task ManyToManyCriteriaJoinAsync()

s = OpenSession();
t = s.BeginTransaction();

var log = new SqlLogSpy();
await (s.CreateCriteria(typeof(Parent))
.AddOrder(Order.Desc("Name"))
// H3 has this after CreateCriteria("Friends"), but it's not yet supported in NH
Expand Down

0 comments on commit b031a6d

Please sign in to comment.