|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | +using NHibernate.Linq; |
| 10 | +using NHibernate.Test.ExceptionsTest; |
| 11 | +using NHibernate.Test.MappingByCode; |
| 12 | +using NUnit.Framework; |
| 13 | + |
| 14 | +namespace NHibernate.Test.NHSpecificTest.NH3800 |
| 15 | +{ |
| 16 | + [TestFixture] |
| 17 | + public class Fixture : BugTestCase |
| 18 | + { |
| 19 | + protected override void OnSetUp() |
| 20 | + { |
| 21 | + var tagA = new Tag() { Name = "A" }; |
| 22 | + var tagB = new Tag() { Name = "B" }; |
| 23 | + |
| 24 | + var project1 = new Project { Name = "ProjectOne" }; |
| 25 | + var compP1_x = new Component() { Name = "PONEx", Project = project1 }; |
| 26 | + var compP1_y = new Component() { Name = "PONEy", Project = project1 }; |
| 27 | + |
| 28 | + var project2 = new Project { Name = "ProjectTwo" }; |
| 29 | + var compP2_x = new Component() { Name = "PTWOx", Project = project2 }; |
| 30 | + var compP2_y = new Component() { Name = "PTWOy", Project = project2 }; |
| 31 | + |
| 32 | + using (var session = OpenSession()) |
| 33 | + using (var transaction = session.BeginTransaction()) |
| 34 | + { |
| 35 | + session.Save(tagA); |
| 36 | + session.Save(tagB); |
| 37 | + session.Save(project1); |
| 38 | + session.Save(compP1_x); |
| 39 | + session.Save(compP1_y); |
| 40 | + session.Save(project2); |
| 41 | + session.Save(compP2_x); |
| 42 | + session.Save(compP2_y); |
| 43 | + |
| 44 | + session.Save(new TimeRecord { TimeInHours = 1, Project = null, Components = { }, Tags = { tagA } }); |
| 45 | + session.Save(new TimeRecord { TimeInHours = 2, Project = null, Components = { }, Tags = { tagB } }); |
| 46 | + |
| 47 | + session.Save(new TimeRecord { TimeInHours = 3, Project = project1, Tags = { tagA, tagB } }); |
| 48 | + session.Save(new TimeRecord { TimeInHours = 4, Project = project1, Components = { compP1_x }, Tags = { tagB } }); |
| 49 | + session.Save(new TimeRecord { TimeInHours = 5, Project = project1, Components = { compP1_y }, Tags = { tagA } }); |
| 50 | + session.Save(new TimeRecord { TimeInHours = 6, Project = project1, Components = { compP1_x, compP1_y }, Tags = { } }); |
| 51 | + |
| 52 | + session.Save(new TimeRecord { TimeInHours = 7, Project = project2, Components = { }, Tags = { tagA, tagB } }); |
| 53 | + session.Save(new TimeRecord { TimeInHours = 8, Project = project2, Components = { compP2_x }, Tags = { tagB } }); |
| 54 | + session.Save(new TimeRecord { TimeInHours = 9, Project = project2, Components = { compP2_y }, Tags = { tagA } }); |
| 55 | + session.Save(new TimeRecord { TimeInHours = 10, Project = project2, Components = { compP2_x, compP2_y }, Tags = { } }); |
| 56 | + |
| 57 | + transaction.Commit(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected override void OnTearDown() |
| 62 | + { |
| 63 | + using (var session = OpenSession()) |
| 64 | + using (var transaction = session.BeginTransaction()) |
| 65 | + { |
| 66 | + session.Delete("from TimeRecord"); |
| 67 | + session.Delete("from Component"); |
| 68 | + session.Delete("from Project"); |
| 69 | + session.Delete("from Tag"); |
| 70 | + |
| 71 | + transaction.Commit(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public void ExpectedHql() |
| 77 | + { |
| 78 | + using (var session = OpenSession()) |
| 79 | + using (var transaction = session.BeginTransaction()) |
| 80 | + { |
| 81 | + var baseQuery = session.Query<TimeRecord>(); |
| 82 | + |
| 83 | + Assert.That(baseQuery.Sum(x => x.TimeInHours), Is.EqualTo(55)); |
| 84 | + |
| 85 | + var query = session.CreateQuery(@" |
| 86 | + select c.Id, count(t), sum(cast(t.TimeInHours as big_decimal)) |
| 87 | + from TimeRecord t |
| 88 | + left join t.Components as c |
| 89 | + group by c.Id"); |
| 90 | + |
| 91 | + var results = query.List<object[]>(); |
| 92 | + Assert.That(results.Select(x => x[1]), Is.EquivalentTo(new[] { 4, 2, 2, 2, 2 })); |
| 93 | + Assert.That(results.Select(x => x[2]), Is.EquivalentTo(new[] { 13, 10, 11, 18, 19 })); |
| 94 | + |
| 95 | + Assert.That(results.Sum(x => (decimal?)x[2]), Is.EqualTo(71)); |
| 96 | + |
| 97 | + transaction.Rollback(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void PureLinq() |
| 103 | + { |
| 104 | + using (var session = OpenSession()) |
| 105 | + using (var transaction = session.BeginTransaction()) |
| 106 | + { |
| 107 | + var baseQuery = session.Query<TimeRecord>(); |
| 108 | + var query = from t in baseQuery |
| 109 | + from c in t.Components.Select(x => (object)x.Id).DefaultIfEmpty() |
| 110 | + let r = new object[] { c, t } |
| 111 | + group r by r[0] |
| 112 | + into g |
| 113 | + select new[] { g.Key, g.Select(x => x[1]).Count(), g.Select(x => x[1]).Sum(x => (decimal?)((TimeRecord)x).TimeInHours) }; |
| 114 | + |
| 115 | + var results = query.ToList(); |
| 116 | + Assert.That(results.Select(x => x[1]), Is.EquivalentTo(new[] { 4, 2, 2, 2, 2 })); |
| 117 | + Assert.That(results.Select(x => x[2]), Is.EquivalentTo(new[] { 13, 10, 11, 18, 19 })); |
| 118 | + |
| 119 | + Assert.That(results.Sum(x => (decimal?)x[2]), Is.EqualTo(71)); |
| 120 | + |
| 121 | + transaction.Rollback(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void MethodGroup() |
| 127 | + { |
| 128 | + using (var session = OpenSession()) |
| 129 | + using (var transaction = session.BeginTransaction()) |
| 130 | + { |
| 131 | + var baseQuery = session.Query<TimeRecord>(); |
| 132 | + var query = baseQuery |
| 133 | + .SelectMany(t => t.Components.Select(c => c.Id).DefaultIfEmpty().Select(c => new object[] { c, t })) |
| 134 | + .GroupBy(g => g[0], g => (TimeRecord)g[1]) |
| 135 | + .Select(g => new[] { g.Key, g.Count(), g.Sum(x => (decimal?)x.TimeInHours) }); |
| 136 | + |
| 137 | + var results = query.ToList(); |
| 138 | + Assert.That(results.Select(x => x[1]), Is.EquivalentTo(new[] { 4, 2, 2, 2, 2 })); |
| 139 | + Assert.That(results.Select(x => x[2]), Is.EquivalentTo(new[] { 13, 10, 11, 18, 19 })); |
| 140 | + |
| 141 | + Assert.That(results.Sum(x => (decimal?)x[2]), Is.EqualTo(71)); |
| 142 | + |
| 143 | + transaction.Rollback(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [Test] |
| 148 | + public void ComplexExample() |
| 149 | + { |
| 150 | + using (var session = OpenSession()) |
| 151 | + using (var transaction = session.BeginTransaction()) |
| 152 | + { |
| 153 | + var baseQuery = session.Query<TimeRecord>(); |
| 154 | + |
| 155 | + Assert.That(baseQuery.Sum(x => x.TimeInHours), Is.EqualTo(55)); |
| 156 | + |
| 157 | + var query = baseQuery.Select(t => new object[] { t }) |
| 158 | + .SelectMany(t => ((TimeRecord)t[0]).Components.Select(c => (object)c.Id).DefaultIfEmpty().Select(c => new[] { t[0], c })) |
| 159 | + .SelectMany(t => ((TimeRecord)t[0]).Tags.Select(x => (object)x.Id).DefaultIfEmpty().Select(x => new[] { t[0], t[1], x })) |
| 160 | + .GroupBy(j => new[] { ((TimeRecord)j[0]).Project.Id, j[1], j[2] }, j => (TimeRecord)j[0]) |
| 161 | + .Select(g => new object[] { g.Key, g.Count(), g.Sum(t => (decimal?)t.TimeInHours) }); |
| 162 | + |
| 163 | + var results = query.ToList(); |
| 164 | + Assert.That(results.Select(x => x[1]), Is.EquivalentTo(new[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 })); |
| 165 | + Assert.That(results.Select(x => x[2]), Is.EquivalentTo(new[] { 1, 2, 3, 3, 4, 5, 6, 6, 7, 7, 8, 9, 10, 10 })); |
| 166 | + |
| 167 | + Assert.That(results.Sum(x => (decimal?)x[2]), Is.EqualTo(81)); |
| 168 | + |
| 169 | + transaction.Rollback(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + [Test] |
| 174 | + public void OuterJoinGroupingWithSubQueryInProjection() |
| 175 | + { |
| 176 | + using (var session = OpenSession()) |
| 177 | + using (var transaction = session.BeginTransaction()) |
| 178 | + { |
| 179 | + var baseQuery = session.Query<TimeRecord>(); |
| 180 | + var query = baseQuery |
| 181 | + .SelectMany(t => t.Components.Select(c => c.Name).DefaultIfEmpty().Select(c => new object[] { c, t })) |
| 182 | + .GroupBy(g => g[0], g => (TimeRecord)g[1]) |
| 183 | + .Select(g => new[] { g.Key, g.Count(), session.Query<Component>().Count(c => c.Name == (string)g.Key) }); |
| 184 | + |
| 185 | + var results = query.ToList(); |
| 186 | + Assert.That(results.Select(x => x[1]), Is.EquivalentTo(new[] { 4, 2, 2, 2, 2 })); |
| 187 | + Assert.That(results.Select(x => x[2]), Is.EquivalentTo(new[] { 0, 1, 1, 1, 1 })); |
| 188 | + |
| 189 | + transaction.Rollback(); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments