From 4e8e072796017a6e8a9c799f8c416de3e2c12cbb Mon Sep 17 00:00:00 2001 From: owerkop Date: Mon, 25 Apr 2016 12:08:00 +0200 Subject: [PATCH 1/2] NH-3864 Testcase. Failing tests. # Conflicts: # src/NHibernate.Test/NHibernate.Test.csproj --- .../NHSpecificTest/NH3864/Fixture.cs | 169 ++++++++++++++++++ .../NHSpecificTest/NH3864/Mappings.hbm.xml | 18 ++ .../NHSpecificTest/NH3864/Person.cs | 15 ++ 3 files changed, 202 insertions(+) create mode 100644 src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs create mode 100644 src/NHibernate.Test/NHSpecificTest/NH3864/Mappings.hbm.xml create mode 100644 src/NHibernate.Test/NHSpecificTest/NH3864/Person.cs diff --git a/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs new file mode 100644 index 00000000000..6efd5b0abe2 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs @@ -0,0 +1,169 @@ +using NUnit.Framework; +using System.Collections.Generic; +using System.Linq; + +namespace NHibernate.Test.NHSpecificTest.NH3864 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnSetUp() + { + Clear2ndLevelCache(); + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + var p1 = new Person() { Name = "A" }; + var p1c1 = new Person() { Name = "AA" }; + var p1c2 = new Person() { Name = "AB" }; + var p1c3 = new Person() { Name = "AC" }; + p1.Children = new HashSet(new[] { p1c1, p1c2, p1c3 }); + s.Save(p1); + + var p2 = new Person() { Name = "B" }; + var p2c1 = new Person() { Name = "BA" }; + var p2c2 = new Person() { Name = "BB" }; + var p2c3 = new Person() { Name = "BC" }; + p2.Children = new HashSet(new[] { p2c1, p2c2, p2c3 }); + s.Save(p2); + + tx.Commit(); + } + } + + protected override void OnTearDown() + { + base.OnTearDown(); + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + s.Delete("from Person"); + tx.Commit(); + } + } + + [Test] + public void CacheableMulticriteria_QueryOverWithAliasedJoinQueryOver() + { + ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + { + using (var s = Sfi.OpenSession()) + { + var query = CreateQueryOverWithAliasedJoinQueryOver(s); + + var multiCriteria = s.CreateMultiCriteria(); + multiCriteria.Add("myQuery", query); + multiCriteria.SetCacheable(true); + + var list = (IList)multiCriteria.GetResult("myQuery"); + AssertQueryResult(list); + } + }); + } + + [Test] + public void CacheableFuture_QueryOverWithAliasedJoinQueryOver() + { + ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + { + using (var s = Sfi.OpenSession()) + { + var query = CreateQueryOverWithAliasedJoinQueryOver(s) + .Cacheable() + .Future(); + + var list = query.ToList(); + AssertQueryResult(list); + } + }); + } + + [Test] + public void CacheableMulticriteria_QueryOverWithJoinAlias() + { + ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + { + using (var s = Sfi.OpenSession()) + { + var query = CreateQueryOverWithJoinAlias(s); + + var multiCriteria = s.CreateMultiCriteria(); + multiCriteria.Add("myQuery", query); + multiCriteria.SetCacheable(true); + + var list = (IList)multiCriteria.GetResult("myQuery"); + AssertQueryResult(list); + } + }); + } + + [Test] + public void CacheableFuture_QueryOverWithJoinAlias() + { + ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + { + using (var s = Sfi.OpenSession()) + { + var query = CreateQueryOverWithJoinAlias(s) + .Cacheable() + .Future(); + + var list = query.ToList(); + AssertQueryResult(list); + } + }); + } + + private static void AssertQueryResult(IList list) + { + Assert.AreEqual(6, list.Count, "Returned records count is wrong."); + var person1 = list.FirstOrDefault(p => p.Name == "A"); + Assert.NotNull(person1); + var person2 = list.FirstOrDefault(p => p.Name == "B"); + Assert.NotNull(person2); + + CollectionAssert.AreEquivalent(person1.Children.Select(c => c.Name), new[] { "AA", "AB", "AC" }); + CollectionAssert.AreEquivalent(person2.Children.Select(c => c.Name), new[] { "BA", "BB", "BC" }); + } + + private static IQueryOver CreateQueryOverWithJoinAlias(ISession session) + { + Person childAlias = null; + return session.QueryOver() + .Where(p => p.Parent == null) + .JoinAlias(x => x.Children, () => childAlias); + } + + private static IQueryOver CreateQueryOverWithAliasedJoinQueryOver(ISession session) + { + Person childAlias = null; + return session.QueryOver() + .Where(p => p.Parent == null) + .JoinQueryOver(x => x.Children, () => childAlias); + } + + private void Clear2ndLevelCache() + { + Sfi.EvictQueries(); + Sfi.Evict(typeof(Person)); + } + + private static void ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(System.Action action) + { + action(); + EnsureNoSqlExecutedOnDb(action); + } + + private static void EnsureNoSqlExecutedOnDb(System.Action action) + { + using (var sqlLogSpy = new SqlLogSpy()) + { + action(); + if (!string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH3864/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/NH3864/Mappings.hbm.xml new file mode 100644 index 00000000000..13a053bcf8c --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3864/Mappings.hbm.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NHibernate.Test/NHSpecificTest/NH3864/Person.cs b/src/NHibernate.Test/NHSpecificTest/NH3864/Person.cs new file mode 100644 index 00000000000..7de6076091d --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3864/Person.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace NHibernate.Test.NHSpecificTest.NH3864 +{ + public class Person + { + public virtual int Id { get; set; } + + public virtual string Name { get; set; } + + public virtual Person Parent { get; set; } + + public virtual ISet Children { get; set; } + } +} \ No newline at end of file From 5f3efb72f4715f99c672fa6b749f320c0e97521c Mon Sep 17 00:00:00 2001 From: Francois Botha Date: Tue, 5 Jun 2018 16:21:55 +0200 Subject: [PATCH 2/2] Add CacheableFuture_QueryWithSubQuery test --- .../Async/NHSpecificTest/NH3864/Fixture.cs | 157 ++++++++++++++++++ .../NHSpecificTest/NH3864/Fixture.cs | 125 +++++++++----- 2 files changed, 243 insertions(+), 39 deletions(-) create mode 100644 src/NHibernate.Test/Async/NHSpecificTest/NH3864/Fixture.cs diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH3864/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH3864/Fixture.cs new file mode 100644 index 00000000000..14e612e9e89 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH3864/Fixture.cs @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using NHibernate.Linq; +using NUnit.Framework; +using System.Collections.Generic; +using System.Linq; + +namespace NHibernate.Test.NHSpecificTest.NH3864 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : BugTestCase + { + protected override void OnSetUp() + { + Clear2ndLevelCache(); + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + var p1 = new Person() { Name = "A" }; + var p1c1 = new Person() { Name = "AA" }; + var p1c2 = new Person() { Name = "AB" }; + var p1c3 = new Person() { Name = "AC" }; + p1.Children = new HashSet(new[] { p1c1, p1c2, p1c3 }); + session.Save(p1); + + var p2 = new Person() { Name = "B" }; + var p2c1 = new Person() { Name = "BA" }; + var p2c2 = new Person() { Name = "BB" }; + var p2c3 = new Person() { Name = "BC" }; + p2.Children = new HashSet(new[] { p2c1, p2c2, p2c3 }); + session.Save(p2); + + transaction.Commit(); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.Delete("from Person"); + transaction.Commit(); + } + } + + [Test] + public async Task CacheableMulticriteria_QueryOverWithAliasedJoinQueryOverAsync() + { + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) + { + using (var session = Sfi.OpenSession()) + { + var query = CreateQueryOverWithAliasedJoinQueryOver(session); + + var multiCriteria = session.CreateMultiCriteria(); + multiCriteria.Add("myQuery", query); + multiCriteria.SetCacheable(true); + + var list = (IList)await (multiCriteria.GetResultAsync("myQuery")); + AssertQueryResult(list); + } + + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } + } + + [Test] + public async Task CacheableMulticriteria_QueryOverWithJoinAliasAsync() + { + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) + { + using (var s = Sfi.OpenSession()) + { + var query = CreateQueryOverWithJoinAlias(s); + + var multiCriteria = s.CreateMultiCriteria(); + multiCriteria.Add("myQuery", query); + multiCriteria.SetCacheable(true); + + var list = (IList)await (multiCriteria.GetResultAsync("myQuery")); + AssertQueryResult(list); + } + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } + } + + private static void AssertQueryResult(IList list) + { + Assert.AreEqual(6, list.Count, "Returned records count is wrong."); + var person1 = list.FirstOrDefault(p => p.Name == "A"); + Assert.NotNull(person1); + var person2 = list.FirstOrDefault(p => p.Name == "B"); + Assert.NotNull(person2); + + CollectionAssert.AreEquivalent(person1.Children.Select(c => c.Name), new[] { "AA", "AB", "AC" }); + CollectionAssert.AreEquivalent(person2.Children.Select(c => c.Name), new[] { "BA", "BB", "BC" }); + } + + private static IFutureEnumerable CreateCacheableQueryWithSubquery(ISession session) + { + var subQuery = session.Query() + .WithOptions(p => p.SetCacheable(true)); + + var query = session.Query() + .FetchMany(p => p.Children) + .Where(p => subQuery.Contains(p) && p.Parent == null) + .WithOptions(o => o.SetCacheable(true)) + .ToFuture(); + + return query; + } + + private static IQueryOver CreateQueryOverWithJoinAlias(ISession session) + { + Person childAlias = null; + return session.QueryOver() + .Where(p => p.Parent == null) + .JoinAlias(x => x.Children, () => childAlias); + } + + private static IQueryOver CreateQueryOverWithAliasedJoinQueryOver(ISession session) + { + Person childAlias = null; + return session.QueryOver() + .Where(p => p.Parent == null) + .JoinQueryOver(x => x.Children, () => childAlias); + } + + private void Clear2ndLevelCache() + { + Sfi.EvictQueries(); + Sfi.Evict(typeof(Person)); + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs index 6efd5b0abe2..3c6076f7bc6 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH3864/Fixture.cs @@ -1,3 +1,4 @@ +using NHibernate.Linq; using NUnit.Framework; using System.Collections.Generic; using System.Linq; @@ -10,61 +11,70 @@ public class Fixture : BugTestCase protected override void OnSetUp() { Clear2ndLevelCache(); - using (ISession s = OpenSession()) - using (ITransaction tx = s.BeginTransaction()) + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) { var p1 = new Person() { Name = "A" }; var p1c1 = new Person() { Name = "AA" }; var p1c2 = new Person() { Name = "AB" }; var p1c3 = new Person() { Name = "AC" }; p1.Children = new HashSet(new[] { p1c1, p1c2, p1c3 }); - s.Save(p1); + session.Save(p1); var p2 = new Person() { Name = "B" }; var p2c1 = new Person() { Name = "BA" }; var p2c2 = new Person() { Name = "BB" }; var p2c3 = new Person() { Name = "BC" }; p2.Children = new HashSet(new[] { p2c1, p2c2, p2c3 }); - s.Save(p2); + session.Save(p2); - tx.Commit(); + transaction.Commit(); } } protected override void OnTearDown() { - base.OnTearDown(); - using (ISession s = OpenSession()) - using (ITransaction tx = s.BeginTransaction()) + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) { - s.Delete("from Person"); - tx.Commit(); + session.Delete("from Person"); + transaction.Commit(); } } [Test] public void CacheableMulticriteria_QueryOverWithAliasedJoinQueryOver() { - ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) { - using (var s = Sfi.OpenSession()) + using (var session = Sfi.OpenSession()) { - var query = CreateQueryOverWithAliasedJoinQueryOver(s); + var query = CreateQueryOverWithAliasedJoinQueryOver(session); - var multiCriteria = s.CreateMultiCriteria(); + var multiCriteria = session.CreateMultiCriteria(); multiCriteria.Add("myQuery", query); multiCriteria.SetCacheable(true); var list = (IList)multiCriteria.GetResult("myQuery"); AssertQueryResult(list); } - }); + + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } } [Test] public void CacheableFuture_QueryOverWithAliasedJoinQueryOver() { - ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) { using (var s = Sfi.OpenSession()) { @@ -75,13 +85,20 @@ public void CacheableFuture_QueryOverWithAliasedJoinQueryOver() var list = query.ToList(); AssertQueryResult(list); } - }); + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } } [Test] public void CacheableMulticriteria_QueryOverWithJoinAlias() { - ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) { using (var s = Sfi.OpenSession()) { @@ -94,13 +111,20 @@ public void CacheableMulticriteria_QueryOverWithJoinAlias() var list = (IList)multiCriteria.GetResult("myQuery"); AssertQueryResult(list); } - }); + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } } [Test] public void CacheableFuture_QueryOverWithJoinAlias() { - ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(() => + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) { using (var s = Sfi.OpenSession()) { @@ -111,7 +135,34 @@ public void CacheableFuture_QueryOverWithJoinAlias() var list = query.ToList(); AssertQueryResult(list); } - }); + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } + } + + [Test] + public void CacheableFuture_QueryWithSubQuery() + { + foreach (var checkCache in new[] { false, true }) + { + using (var sqlLogSpy = new SqlLogSpy()) + { + using (var session = Sfi.OpenSession()) + { + var query = CreateCacheableQueryWithSubquery(session); + + var list = query.ToList(); + AssertQueryResult(list); + } + if (checkCache && !string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) + { + Assert.Fail("SQL executed. 2nd level cache should be used instead."); + } + } + } } private static void AssertQueryResult(IList list) @@ -126,6 +177,20 @@ private static void AssertQueryResult(IList list) CollectionAssert.AreEquivalent(person2.Children.Select(c => c.Name), new[] { "BA", "BB", "BC" }); } + private static IFutureEnumerable CreateCacheableQueryWithSubquery(ISession session) + { + var subQuery = session.Query() + .WithOptions(p => p.SetCacheable(true)); + + var query = session.Query() + .FetchMany(p => p.Children) + .Where(p => subQuery.Contains(p) && p.Parent == null) + .WithOptions(o => o.SetCacheable(true)) + .ToFuture(); + + return query; + } + private static IQueryOver CreateQueryOverWithJoinAlias(ISession session) { Person childAlias = null; @@ -147,23 +212,5 @@ private void Clear2ndLevelCache() Sfi.EvictQueries(); Sfi.Evict(typeof(Person)); } - - private static void ExecuteActionTwiceSecondRunEnsureNoSqlExecuted(System.Action action) - { - action(); - EnsureNoSqlExecutedOnDb(action); - } - - private static void EnsureNoSqlExecutedOnDb(System.Action action) - { - using (var sqlLogSpy = new SqlLogSpy()) - { - action(); - if (!string.IsNullOrEmpty(sqlLogSpy.GetWholeLog())) - { - Assert.Fail("SQL executed. 2nd level cache should be used instead."); - } - } - } } }