diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs index 686fe9837a9..b618a38c1a4 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs @@ -154,6 +154,9 @@ public async Task TestQueryOverRestrictionWithClauseAsync() [Test] public async Task LazyLoadAsync() { + if(Dialect is PostgreSQLDialect) + Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast"); + using (var s = OpenSession()) { var asset = await (s.Query().FirstAsync()); @@ -166,6 +169,9 @@ public async Task LazyLoadAsync() [Test] public async Task LazyLoadFilteredAsync() { + if(Dialect is PostgreSQLDialect) + Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast"); + using (var s = OpenSession()) { s.EnableFilter("deletedFilter").SetParameter("deletedParam", false); diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH750/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs similarity index 54% rename from src/NHibernate.Test/Async/NHSpecificTest/NH750/Fixture.cs rename to src/NHibernate.Test/Async/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs index a7d88d0d321..938f38b6817 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH750/Fixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs @@ -10,21 +10,55 @@ using System; using NHibernate.Cfg; +using NHibernate.Criterion; +using NHibernate.Transform; using NUnit.Framework; namespace NHibernate.Test.NHSpecificTest.NH750 { using System.Threading.Tasks; [TestFixture] - public class FixtureAsync : BugTestCase + public class ManyToManyNotFoundIgnoreFixtureAsync : BugTestCase { + private int id1; + private int id2; + + 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"); + using (var s = Sfi.OpenSession()) + using (var t = s.BeginTransaction()) + { + 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); + + id1 = (int) s.Save(dv1); + id2 = (int) s.Save(dv2); + s.Flush(); + + s.Clear(); + s.Delete(dr3); + t.Commit(); + } + } + protected override void OnTearDown() { using (ISession s = Sfi.OpenSession()) + using (var t = s.BeginTransaction()) { s.Delete("from Device"); s.Delete("from Drive"); - s.Flush(); + t.Commit(); } } @@ -42,51 +76,17 @@ protected override void Configure(Configuration configuration) [Test] public async Task DeviceOfDriveAsync() { - int[] dvSavedId = new int[2]; - 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"); + Device dv1; + Device dv2; using (ISession s = Sfi.OpenSession()) { - await (s.SaveAsync(dr1)); - await (s.SaveAsync(dr2)); - await (s.SaveAsync(dr3)); - dvSavedId[0] = (int) await (s.SaveAsync(dv1)); - dvSavedId[1] = (int) await (s.SaveAsync(dv2)); - await (s.FlushAsync()); + dv1 = (Device) await (s.LoadAsync(typeof(Device), id1)); + dv2 = (Device) await (s.LoadAsync(typeof(Device), id2)); } - dv1.Drives.Add(dr1); - dv1.Drives.Add(dr2); - dv2.Drives.Add(dr1); - dv2.Drives.Add(dr3); - using (ISession s = Sfi.OpenSession()) - { - dvSavedId[0] = (int) await (s.SaveAsync(dv1)); - dvSavedId[1] = (int) await (s.SaveAsync(dv2)); - await (s.FlushAsync()); - } - dv1 = null; - dv2 = null; - using (ISession s = Sfi.OpenSession()) - { - await (s.DeleteAsync(dr3)); - await (s.FlushAsync()); - dv1 = (Device) await (s.LoadAsync(typeof(Device), dvSavedId[0])); - dv2 = (Device) await (s.LoadAsync(typeof(Device), dvSavedId[1])); - } - Assert.AreEqual(2, dv1.Drives.Count); + Assert.That(dv1.Drives, Has.Count.EqualTo(2).And.None.Null); // Verify one is missing - Assert.AreEqual(1, dv2.Drives.Count); - // Verify dv1 unchanged - Assert.IsTrue(dv1.Drives.Contains(dr1)); - Assert.IsTrue(dv1.Drives.Contains(dr2)); - - // Verify dv2 - Assert.IsTrue(dv2.Drives.Contains(dr1)); - Assert.IsFalse(dv2.Drives.Contains(dr3)); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); //Make sure that flush didn't touch not-found="ignore" records for not modified collection using (var s = Sfi.OpenSession()) @@ -104,7 +104,7 @@ public async Task DeviceOfDriveAsync() using (var t = s.BeginTransaction()) { dv2 = await (s.GetAsync(dv2.Id)); - dv2.Drives.Add(dr2); + dv2.Drives.Add(dv1.Drives[1]); await (t.CommitAsync()); } @@ -125,5 +125,49 @@ async Task VerifyResultAsync(int expectedInCollection, int expectedInDb, string } } } + + [Test] + public async Task QueryOverFetchAsync() + { + using (var s = OpenSession()) + { + var dv2 = await (s.QueryOver() + .Fetch(SelectMode.Fetch, x => x.Drives) + .Where(Restrictions.IdEq(id2)) + .TransformUsing(Transformers.DistinctRootEntity) + .SingleOrDefaultAsync()); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).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()); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); + } + } + + [Test] + public async Task LazyLoadAsync() + { + using (var s = OpenSession()) + { + var dv2 = await (s.GetAsync(id2)); + await (NHibernateUtil.InitializeAsync(dv2.Drives)); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); + } + } } } diff --git a/src/NHibernate.Test/Async/PropertyRef/ManyToManyPropertyRefFixture.cs b/src/NHibernate.Test/Async/PropertyRef/ManyToManyPropertyRefFixture.cs index 1bb2716733f..435758ac99e 100644 --- a/src/NHibernate.Test/Async/PropertyRef/ManyToManyPropertyRefFixture.cs +++ b/src/NHibernate.Test/Async/PropertyRef/ManyToManyPropertyRefFixture.cs @@ -8,20 +8,22 @@ //------------------------------------------------------------------------------ +using System.Linq; using NHibernate.Criterion; +using NHibernate.Linq; using NUnit.Framework; namespace NHibernate.Test.PropertyRef { using System.Threading.Tasks; - [TestFixture] + [TestFixture(Description = "NH-2180 (GH-1214)")] public class ManyToManyPropertyRefFixtureAsync : TestCase { protected override string[] Mappings => new[] { "PropertyRef.ManyToManyWithPropertyRef.hbm.xml" }; protected override string MappingsAssembly => "NHibernate.Test"; - private object _manyAId; + private long _manyAId; protected override void OnSetUp() { @@ -34,7 +36,7 @@ protected override void OnSetUp() var manyB2 = new ManyB { Number = 8, Value = "a value of b2" }; var manyB3 = new ManyB { Number = 12, Value = "a value of b3" }; - _manyAId = session.Save(manyA); + _manyAId = (long) session.Save(manyA); session.Save(manyB1); session.Save(manyB2); session.Save(manyB3); @@ -144,5 +146,20 @@ bei NHibernate.Type.EntityType.LoadByUniqueKey(String entityName, String uniqueK Assert.That(loadedManyA.ManyBs, Has.Count.EqualTo(3).And.None.Null); } + + [Test] + public async Task LinqFetchAsync() + { + using (var session = OpenSession()) + { + var manyA = (await (session + .Query() + .Where(a => a.Id == _manyAId) + .FetchMany(a => a.ManyBs) + .ToListAsync())) + .First(); + Assert.That(manyA.ManyBs, Has.Count.EqualTo(3).And.None.Null); + } + } } } diff --git a/src/NHibernate.Test/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs b/src/NHibernate.Test/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs index c127f4a91e9..9c4ae08575a 100644 --- a/src/NHibernate.Test/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/GH1994/ManyToManyFilteredFixture.cs @@ -143,6 +143,9 @@ public void TestQueryOverRestrictionWithClause() [Test] public void LazyLoad() { + if(Dialect is PostgreSQLDialect) + Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast"); + using (var s = OpenSession()) { var asset = s.Query().First(); @@ -155,6 +158,9 @@ public void LazyLoad() [Test] public void LazyLoadFiltered() { + if(Dialect is PostgreSQLDialect) + Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast"); + using (var s = OpenSession()) { s.EnableFilter("deletedFilter").SetParameter("deletedParam", false); diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs similarity index 51% rename from src/NHibernate.Test/NHSpecificTest/NH750/Fixture.cs rename to src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs index 9ff71f07057..5716d63cd32 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs @@ -1,19 +1,53 @@ using System; using NHibernate.Cfg; +using NHibernate.Criterion; +using NHibernate.Transform; using NUnit.Framework; namespace NHibernate.Test.NHSpecificTest.NH750 { [TestFixture] - public class Fixture : BugTestCase + public class ManyToManyNotFoundIgnoreFixture : BugTestCase { + private int id1; + private int id2; + + 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"); + using (var s = Sfi.OpenSession()) + using (var t = s.BeginTransaction()) + { + 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); + + id1 = (int) s.Save(dv1); + id2 = (int) s.Save(dv2); + s.Flush(); + + s.Clear(); + s.Delete(dr3); + t.Commit(); + } + } + protected override void OnTearDown() { using (ISession s = Sfi.OpenSession()) + using (var t = s.BeginTransaction()) { s.Delete("from Device"); s.Delete("from Drive"); - s.Flush(); + t.Commit(); } } @@ -31,51 +65,17 @@ protected override void Configure(Configuration configuration) [Test] public void DeviceOfDrive() { - int[] dvSavedId = new int[2]; - 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"); + Device dv1; + Device dv2; using (ISession s = Sfi.OpenSession()) { - s.Save(dr1); - s.Save(dr2); - s.Save(dr3); - dvSavedId[0] = (int) s.Save(dv1); - dvSavedId[1] = (int) s.Save(dv2); - s.Flush(); + dv1 = (Device) s.Load(typeof(Device), id1); + dv2 = (Device) s.Load(typeof(Device), id2); } - dv1.Drives.Add(dr1); - dv1.Drives.Add(dr2); - dv2.Drives.Add(dr1); - dv2.Drives.Add(dr3); - using (ISession s = Sfi.OpenSession()) - { - dvSavedId[0] = (int) s.Save(dv1); - dvSavedId[1] = (int) s.Save(dv2); - s.Flush(); - } - dv1 = null; - dv2 = null; - using (ISession s = Sfi.OpenSession()) - { - s.Delete(dr3); - s.Flush(); - dv1 = (Device) s.Load(typeof(Device), dvSavedId[0]); - dv2 = (Device) s.Load(typeof(Device), dvSavedId[1]); - } - Assert.AreEqual(2, dv1.Drives.Count); + Assert.That(dv1.Drives, Has.Count.EqualTo(2).And.None.Null); // Verify one is missing - Assert.AreEqual(1, dv2.Drives.Count); - // Verify dv1 unchanged - Assert.IsTrue(dv1.Drives.Contains(dr1)); - Assert.IsTrue(dv1.Drives.Contains(dr2)); - - // Verify dv2 - Assert.IsTrue(dv2.Drives.Contains(dr1)); - Assert.IsFalse(dv2.Drives.Contains(dr3)); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); //Make sure that flush didn't touch not-found="ignore" records for not modified collection using (var s = Sfi.OpenSession()) @@ -93,7 +93,7 @@ public void DeviceOfDrive() using (var t = s.BeginTransaction()) { dv2 = s.Get(dv2.Id); - dv2.Drives.Add(dr2); + dv2.Drives.Add(dv1.Drives[1]); t.Commit(); } @@ -114,5 +114,49 @@ void VerifyResult(int expectedInCollection, int expectedInDb, string msg) } } } + + [Test] + public void QueryOverFetch() + { + using (var s = OpenSession()) + { + var dv2 = s.QueryOver() + .Fetch(SelectMode.Fetch, x => x.Drives) + .Where(Restrictions.IdEq(id2)) + .TransformUsing(Transformers.DistinctRootEntity) + .SingleOrDefault(); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); + } + } + + [Test] + public void HqlFetch() + { + using (var s = OpenSession()) + { + var dv2 = s.CreateQuery("from Device d left join fetch d.Drives where d.id = :id") + .SetResultTransformer(Transformers.DistinctRootEntity) + .SetParameter("id", id2) + .UniqueResult(); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); + } + } + + [Test] + public void LazyLoad() + { + using (var s = OpenSession()) + { + var dv2 = s.Get(id2); + NHibernateUtil.Initialize(dv2.Drives); + + Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); + Assert.That(dv2.Drives, Has.Count.EqualTo(1).And.None.Null); + } + } } } diff --git a/src/NHibernate.Test/PropertyRef/ManyToManyPropertyRefFixture.cs b/src/NHibernate.Test/PropertyRef/ManyToManyPropertyRefFixture.cs index 054cba44957..9ecb5dd4377 100644 --- a/src/NHibernate.Test/PropertyRef/ManyToManyPropertyRefFixture.cs +++ b/src/NHibernate.Test/PropertyRef/ManyToManyPropertyRefFixture.cs @@ -1,16 +1,18 @@ +using System.Linq; using NHibernate.Criterion; +using NHibernate.Linq; using NUnit.Framework; namespace NHibernate.Test.PropertyRef { - [TestFixture] + [TestFixture(Description = "NH-2180 (GH-1214)")] public class ManyToManyPropertyRefFixture : TestCase { protected override string[] Mappings => new[] { "PropertyRef.ManyToManyWithPropertyRef.hbm.xml" }; protected override string MappingsAssembly => "NHibernate.Test"; - private object _manyAId; + private long _manyAId; protected override void OnSetUp() { @@ -23,7 +25,7 @@ protected override void OnSetUp() var manyB2 = new ManyB { Number = 8, Value = "a value of b2" }; var manyB3 = new ManyB { Number = 12, Value = "a value of b3" }; - _manyAId = session.Save(manyA); + _manyAId = (long) session.Save(manyA); session.Save(manyB1); session.Save(manyB2); session.Save(manyB3); @@ -133,5 +135,20 @@ bei NHibernate.Type.EntityType.LoadByUniqueKey(String entityName, String uniqueK Assert.That(loadedManyA.ManyBs, Has.Count.EqualTo(3).And.None.Null); } + + [Test] + public void LinqFetch() + { + using (var session = OpenSession()) + { + var manyA = session + .Query() + .Where(a => a.Id == _manyAId) + .FetchMany(a => a.ManyBs) + .ToList() + .First(); + Assert.That(manyA.ManyBs, Has.Count.EqualTo(3).And.None.Null); + } + } } }