|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using NUnit.Framework; |
| 4 | + |
| 5 | +namespace NHibernate.Test.NHSpecificTest.GH3334 |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class Fixture : BugTestCase |
| 9 | + { |
| 10 | + [OneTimeSetUp] |
| 11 | + public void OneTimeSetUp() |
| 12 | + { |
| 13 | + using (var session = OpenSession()) |
| 14 | + using (var t = session.BeginTransaction()) |
| 15 | + { |
| 16 | + var parent = new Entity |
| 17 | + { |
| 18 | + Name = "Parent1", |
| 19 | + Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "GrandChild" } } } |
| 20 | + }; |
| 21 | + session.Save(parent); |
| 22 | + parent = new Entity |
| 23 | + { |
| 24 | + Name = "Parent2", |
| 25 | + Children = { new ChildEntity { Name = "Child", Child = new GrandChildEntity { Name = "XGrandChild" } } } |
| 26 | + }; |
| 27 | + var other = new OtherEntity { Name = "ABC", Entities = {parent}}; |
| 28 | + parent.OtherEntity = other; |
| 29 | + session.Save(parent); |
| 30 | + session.Save(other); |
| 31 | + t.Commit(); |
| 32 | + } |
| 33 | + |
| 34 | + Sfi.Statistics.IsStatisticsEnabled = true; |
| 35 | + } |
| 36 | + |
| 37 | + [OneTimeTearDown] |
| 38 | + public void OneTimeTearDown() |
| 39 | + { |
| 40 | + Sfi.Statistics.IsStatisticsEnabled = false; |
| 41 | + |
| 42 | + using var session = OpenSession(); |
| 43 | + using var transaction = session.BeginTransaction(); |
| 44 | + |
| 45 | + session.CreateQuery("delete from ChildEntity").ExecuteUpdate(); |
| 46 | + session.CreateQuery("delete from GrandChildEntity").ExecuteUpdate(); |
| 47 | + session.CreateQuery("delete from Entity").ExecuteUpdate(); |
| 48 | + session.CreateQuery("delete from OtherEntity").ExecuteUpdate(); |
| 49 | + |
| 50 | + transaction.Commit(); |
| 51 | + } |
| 52 | + |
| 53 | + public class TestCaseItem |
| 54 | + { |
| 55 | + public string Name { get; } |
| 56 | + public string Hql { get; } |
| 57 | + public int LineNumber { get; } |
| 58 | + |
| 59 | + public TestCaseItem(string name, string hql, [CallerLineNumber] int lineNumber = 0) |
| 60 | + { |
| 61 | + Name = name; |
| 62 | + Hql = hql; |
| 63 | + LineNumber = lineNumber; |
| 64 | + } |
| 65 | + |
| 66 | + public override string ToString() => $"{LineNumber:0000}: {Name}"; |
| 67 | + } |
| 68 | + |
| 69 | + public static IEnumerable<TestCaseItem> GetNoExceptionOnExecuteQueryTestCases() |
| 70 | + { |
| 71 | + /* does not work because of inner join or theta join created for many-to-one |
| 72 | + @" |
| 73 | + SELECT ROOT |
| 74 | + FROM Entity AS ROOT |
| 75 | + WHERE |
| 76 | + EXISTS |
| 77 | + (FROM ELEMENTS(ROOT.Children) AS child |
| 78 | + WHERE |
| 79 | + child.Child.Name like 'G%' |
| 80 | + OR ROOT.OtherEntity.Name like 'A%' |
| 81 | + )");*/ |
| 82 | + |
| 83 | + yield return new("Basic Elements case 1 FoundViaGrandChildG", @" |
| 84 | + SELECT ROOT |
| 85 | + FROM Entity AS ROOT |
| 86 | + WHERE |
| 87 | + EXISTS |
| 88 | + (FROM ELEMENTS(ROOT.Children) AS child |
| 89 | + LEFT JOIN child.Child AS grandChild |
| 90 | + WHERE |
| 91 | + grandChild.Name like 'G%' |
| 92 | + )"); |
| 93 | + yield return new("Basic Elements case 2 FoundViaOtherEntityA", @" |
| 94 | + SELECT ROOT |
| 95 | + FROM Entity AS ROOT |
| 96 | + WHERE |
| 97 | + EXISTS |
| 98 | + (FROM ELEMENTS(ROOT.OtherEntity) AS otherEntity |
| 99 | + WHERE |
| 100 | + otherEntity.Name like 'A%' |
| 101 | + )"); |
| 102 | + yield return new("HQL Elements FoundViaGrandChildG", @" |
| 103 | + SELECT ROOT |
| 104 | + FROM Entity AS ROOT |
| 105 | + WHERE |
| 106 | + EXISTS |
| 107 | + (FROM ELEMENTS(ROOT.Children) AS child |
| 108 | + LEFT JOIN child.Child AS grandChild |
| 109 | + LEFT JOIN ROOT.OtherEntity AS otherEntity |
| 110 | + WHERE |
| 111 | + grandChild.Name like 'G%' |
| 112 | + OR otherEntity.Name like 'G%' |
| 113 | + )"); |
| 114 | + yield return new("HQL Elements FoundViaOtherEntityA", @" |
| 115 | + SELECT ROOT |
| 116 | + FROM Entity AS ROOT |
| 117 | + WHERE |
| 118 | + EXISTS |
| 119 | + (FROM ELEMENTS(ROOT.Children) AS child |
| 120 | + LEFT JOIN child.Child AS grandChild |
| 121 | + LEFT JOIN ROOT.OtherEntity AS otherEntity |
| 122 | + WHERE |
| 123 | + grandChild.Name like 'A%' |
| 124 | + OR otherEntity.Name like 'A%' |
| 125 | + )"); |
| 126 | + yield return new("HQL Entity FoundViaGrandChildG", @" |
| 127 | + SELECT ROOT |
| 128 | + FROM Entity AS ROOT |
| 129 | + WHERE |
| 130 | + EXISTS |
| 131 | + (FROM ChildEntity AS child |
| 132 | + LEFT JOIN child.Child AS grandChild |
| 133 | + LEFT JOIN ROOT.OtherEntity AS otherEntity |
| 134 | + WHERE |
| 135 | + child.Parent = ROOT |
| 136 | + AND ( |
| 137 | + grandChild.Name like 'G%' |
| 138 | + OR otherEntity.Name like 'G%' |
| 139 | + ) |
| 140 | + )"); |
| 141 | + yield return new("HQL Entity FoundViaOtherEntityA", @" |
| 142 | + SELECT ROOT |
| 143 | + FROM Entity AS ROOT |
| 144 | + WHERE |
| 145 | + EXISTS |
| 146 | + (FROM ChildEntity AS child |
| 147 | + LEFT JOIN child.Child AS grandChild |
| 148 | + LEFT JOIN ROOT.OtherEntity AS otherEntity |
| 149 | + WHERE |
| 150 | + child.Parent = ROOT |
| 151 | + AND ( |
| 152 | + grandChild.Name like 'A%' |
| 153 | + OR otherEntity.Name like 'A%' |
| 154 | + ) |
| 155 | + )"); |
| 156 | + } |
| 157 | + |
| 158 | + [Test, TestCaseSource(nameof(GetNoExceptionOnExecuteQueryTestCases))] |
| 159 | + public void NoExceptionOnExecuteQuery(TestCaseItem testCase) |
| 160 | + { |
| 161 | + using var session = OpenSession(); |
| 162 | + using var _ = session.BeginTransaction(); |
| 163 | + |
| 164 | + var q = session.CreateQuery(testCase.Hql); |
| 165 | + Assert.That(q.List(), Has.Count.EqualTo(1)); |
| 166 | + } |
| 167 | + |
| 168 | + protected override bool CheckDatabaseWasCleaned() |
| 169 | + { |
| 170 | + // same set of objects for each test |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments