Skip to content

Commit

Permalink
More test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bahusoid committed Aug 5, 2023
1 parent 7c31eb5 commit aaf7810
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 35 deletions.
21 changes: 14 additions & 7 deletions src/NHibernate.Test/NHSpecificTest/NH750/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace NHibernate.Test.NHSpecificTest.NH750
{
public class Device
{
private int _id;
private Device _template;
private IList<Drive> _drives = new List<Drive>();
private IList<Drive> _drivesNotIgnored = new List<Drive>();

public Device() : base()
{
}
Expand All @@ -16,32 +21,34 @@ public Device(string manifacturer)
_manifacturer = manifacturer;
}

private int _id;
public virtual Device Template
{
get => _template;
set => _template = value;
}

public int Id
public virtual int Id
{
get { return _id; }
set { _id = value; }
}

private string _manifacturer;

public string Manifacturer
public virtual string Manifacturer
{
get { return _manifacturer; }
set { _manifacturer = value; }
}

private IList<Drive> _drives = new List<Drive>();
private IList<Drive> _drivesNotIgnored = new List<Drive>();

public IList<Drive> Drives
public virtual IList<Drive> Drives
{
get { return _drives; }
set { _drives = value; }
}

public IList<Drive> DrivesNotIgnored
public virtual IList<Drive> DrivesNotIgnored
{
get => _drivesNotIgnored;
set => _drivesNotIgnored = value;
Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate.Test/NHSpecificTest/NH750/Drive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public Drive(string classFullName)

private int _id;

public int Id
public virtual int Id
{
get { return _id; }
set { _id = value; }
}

private string _classFullName;

public string ClassFullName
public virtual string ClassFullName
{
get { return _classFullName; }
set { _classFullName = value; }
Expand All @@ -44,4 +44,4 @@ public override int GetHashCode()
return _classFullName.GetHashCode();
}
}
}
}
144 changes: 144 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using NHibernate.Criterion;
using NHibernate.Transform;
using NUnit.Framework;

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

public ManyToManyFixture(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 void QueryOverFetch()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var dv2 = s.QueryOver<Device>()
.Fetch(SelectMode.Fetch, x => x.DrivesNotIgnored)
.Where(Restrictions.IdEq(id2))
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefault();

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 void QueryOverFetch2()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = s.QueryOver<Device>()
.Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.DrivesNotIgnored)
.Where(Restrictions.IdEq(_withTempalteId))
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefault();

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 void HqlFetch()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var dv2 = s.CreateQuery("from Device d left join fetch d.DrivesNotIgnored where d.id = :id")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetParameter("id", id2)
.UniqueResult<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 void HqlFetch2()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = 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)
.UniqueResult<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 void LazyLoad()
{
using var log = new SqlLogSpy();
using var s = OpenSession();

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

NHibernateUtil.Initialize(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 @@ -13,6 +13,7 @@ public class ManyToManyNotFoundIgnoreFixture : 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 @@ -28,25 +29,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();
var withTempalte = new Device("Device With Device 2 template") { Template = dv2 };

s.Clear();
s.Delete(dr3);
t.Commit();
}
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 @@ -76,6 +78,8 @@ public void DeviceOfDrive()
{
dv1 = (Device) s.Load(typeof(Device), id1);
dv2 = (Device) s.Load(typeof(Device), id2);
NHibernateUtil.Initialize(dv1.Drives);
NHibernateUtil.Initialize(dv2.Drives);
}

Assert.That(dv1.Drives, Has.Count.EqualTo(_drivesCount).And.None.Null);
Expand Down Expand Up @@ -141,6 +145,23 @@ public void QueryOverFetch()
Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1));
}

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

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 void HqlFetch()
{
Expand All @@ -157,17 +178,31 @@ public void HqlFetch()
}

[Test]
public void LazyLoad()
public void HqlFetch2()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var withTemplate = 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)
.UniqueResult<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 void LazyLoad()
{
using var s = OpenSession();

var dv2 = s.Get<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 @@ -44,6 +44,7 @@ protected override void OnTearDown()
[Test]
public void LazyLoad()
{
using var log = new SqlLogSpy();
using var s = OpenSession();
var device = s.Get<Device>(_id);
Assert.Throws<ObjectNotFoundException>(() => NHibernateUtil.Initialize(device.DrivesNotIgnored));
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH750/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH750"
default-access="field.camelcase-underscore"
default-lazy="false">
default-access="field.camelcase-underscore">
<class name="Device">
<id name="Id" column="`_id`" type="Int32">
<generator class="native"/>
</id>
<property name="Manifacturer"/>
<many-to-one name="Template" />
<bag name="Drives" table="DriveOfDevice" cascade="all">
<key column="DeviceId"/>
<many-to-many class="Drive" column="DriveId" not-found="ignore"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void ManyToManyCriteriaJoin()

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

var log = new SqlLogSpy();
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 aaf7810

Please sign in to comment.