Skip to content

Commit 5f236c3

Browse files
committed
NH-3801 - Tests for grouping and joins using case statements that should produce joins
1 parent f20a5e3 commit 5f236c3

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/NHibernate.Test/Linq/ByMethod/GroupByTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,55 @@ public void GroupByComputedValueInObjectArray()
549549
Assert.AreEqual(830, orderGroups.Sum(g => g.Count));
550550
}
551551

552+
[Test(Description = "NH-3801")]
553+
public void GroupByComputedValueWithJoinOnObject()
554+
{
555+
var orderGroups = db.OrderLines.GroupBy(o => o.Order.Customer == null ? 0 : 1).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
556+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
557+
}
558+
559+
[Test(Description = "NH-3801")]
560+
public void GroupByComputedValueWithJoinOnId()
561+
{
562+
var orderGroups = db.OrderLines.GroupBy(o => o.Order.Customer.CustomerId == null ? 0 : 1).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
563+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
564+
}
565+
566+
[Test(Description = "NH-3801")]
567+
public void GroupByComputedValueInAnonymousTypeWithJoinOnObject()
568+
{
569+
var orderGroups = db.OrderLines.GroupBy(o => new { Key = o.Order.Customer == null ? 0 : 1 }).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
570+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
571+
}
572+
573+
[Test(Description = "NH-3801")]
574+
public void GroupByComputedValueInAnonymousTypeWithJoinOnId()
575+
{
576+
var orderGroups = db.OrderLines.GroupBy(o => new { Key = o.Order.Customer.CustomerId == null ? 0 : 1 }).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
577+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
578+
}
579+
580+
[Test(Description = "NH-3801")]
581+
public void GroupByComputedValueInObjectArrayWithJoinOnObject()
582+
{
583+
var orderGroups = db.OrderLines.GroupBy(o => new[] { o.Order.Customer == null ? 0 : 1 }).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
584+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
585+
}
586+
587+
[Test(Description = "NH-3801")]
588+
public void GroupByComputedValueInObjectArrayWithJoinOnId()
589+
{
590+
var orderGroups = db.OrderLines.GroupBy(o => new[] { o.Order.Customer.CustomerId == null ? 0 : 1 }).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
591+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
592+
}
593+
594+
[Test(Description = "NH-3801")]
595+
public void GroupByComputedValueInObjectArrayWithJoinInRightSideOfCase()
596+
{
597+
var orderGroups = db.OrderLines.GroupBy(o => new[] { o.Order.Customer.CustomerId == null ? "unknown" : o.Order.Customer.CompanyName }).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
598+
Assert.AreEqual(2155, orderGroups.Sum(g => g.Count));
599+
}
600+
552601
private static void CheckGrouping<TKey, TElement>(IEnumerable<IGrouping<TKey, TElement>> groupedItems, Func<TElement, TKey> groupBy)
553602
{
554603
var used = new HashSet<object>();

src/NHibernate.Test/Linq/JoinTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,58 @@ public void OrderLinesWithSelectingOrderIdAndDateShouldProduceOneJoin()
260260
}
261261
}
262262

263+
[Test(Description = "NH-3801")]
264+
public void OrderLinesWithSelectingCustomerIdInCaseShouldProduceOneJoin()
265+
{
266+
using (var spy = new SqlLogSpy())
267+
{
268+
(from l in db.OrderLines
269+
select new { CustomerKnown = l.Order.Customer.CustomerId == null ? 0 : 1, l.Order.OrderDate }).ToList();
270+
271+
var countJoins = CountJoins(spy);
272+
Assert.That(countJoins, Is.EqualTo(1));
273+
}
274+
}
275+
276+
[Test(Description = "NH-3801")]
277+
public void OrderLinesWithSelectingCustomerInCaseShouldProduceOneJoin()
278+
{
279+
using (var spy = new SqlLogSpy())
280+
{
281+
(from l in db.OrderLines
282+
select new { CustomerKnown = l.Order.Customer == null ? 0 : 1, l.Order.OrderDate }).ToList();
283+
284+
var countJoins = CountJoins(spy);
285+
Assert.That(countJoins, Is.EqualTo(1));
286+
}
287+
}
288+
289+
[Test(Description = "NH-3801")]
290+
public void OrderLinesWithSelectingCustomerNameInCaseShouldProduceTwoJoins()
291+
{
292+
using (var spy = new SqlLogSpy())
293+
{
294+
(from l in db.OrderLines
295+
select new { CustomerKnown = l.Order.Customer.CustomerId == null ? "unknown" : l.Order.Customer.CompanyName, l.Order.OrderDate }).ToList();
296+
297+
var countJoins = CountJoins(spy);
298+
Assert.That(countJoins, Is.EqualTo(2));
299+
}
300+
}
301+
302+
[Test(Description = "NH-3801")]
303+
public void OrderLinesWithSelectingCustomerNameInCaseShouldProduceTwoJoinsAlternate()
304+
{
305+
using (var spy = new SqlLogSpy())
306+
{
307+
(from l in db.OrderLines
308+
select new { CustomerKnown = l.Order.Customer == null ? "unknown" : l.Order.Customer.CompanyName, l.Order.OrderDate }).ToList();
309+
310+
var countJoins = CountJoins(spy);
311+
Assert.That(countJoins, Is.EqualTo(2));
312+
}
313+
}
314+
263315
private static int CountJoins(LogSpy sqlLog)
264316
{
265317
return Count(sqlLog, "join");

0 commit comments

Comments
 (0)