Skip to content

Commit 2eab4db

Browse files
committed
Code review changes
1 parent 21ab72e commit 2eab4db

File tree

10 files changed

+421
-98
lines changed

10 files changed

+421
-98
lines changed

src/NHibernate.Test/Async/Linq/ByMethod/JoinTests.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using System.Linq;
13+
using System.Reflection;
14+
using NHibernate.Cfg;
15+
using NHibernate.Engine.Query;
16+
using NHibernate.Util;
17+
using NSubstitute;
1218
using NUnit.Framework;
1319
using NHibernate.Linq;
1420

@@ -32,30 +38,31 @@ public async Task MultipleLinqJoinsWithSameProjectionNamesAsync()
3238
Assert.IsTrue(orders.All(x => x.FirstId == x.SecondId - 1 && x.SecondId == x.ThirdId - 1));
3339
}
3440

35-
[Test]
36-
public async Task CrossJoinWithPredicateInOnStatementAsync()
41+
[TestCase(false)]
42+
[TestCase(true)]
43+
public async Task CrossJoinWithPredicateInWhereStatementAsync(bool useCrossJoin)
3744
{
38-
var result =
39-
await ((from o in db.Orders
40-
from p in db.Products
41-
join d in db.OrderLines
42-
on new { o.OrderId, p.ProductId } equals new { d.Order.OrderId, d.Product.ProductId }
43-
into details
44-
from d in details
45-
select new { o.OrderId, p.ProductId, d.UnitPrice }).Take(10).ToListAsync());
46-
47-
Assert.That(result.Count, Is.EqualTo(10));
48-
}
45+
if (useCrossJoin && !Dialect.SupportsCrossJoin)
46+
{
47+
Assert.Ignore("Dialect does not support cross join.");
48+
}
4949

50-
[Test]
51-
public async Task CrossJoinWithPredicateInWhereStatementAsync()
52-
{
53-
var result = await ((from o in db.Orders
54-
from o2 in db.Orders.Where(x => x.Freight > 50)
55-
where (o.OrderId == o2.OrderId + 1) || (o.OrderId == o2.OrderId - 1)
56-
select new { o.OrderId, OrderId2 = o2.OrderId }).ToListAsync());
50+
using (var substitute = SubstituteDialect())
51+
using (var sqlSpy = new SqlLogSpy())
52+
{
53+
ClearQueryPlanCache();
54+
substitute.Value.SupportsCrossJoin.Returns(useCrossJoin);
55+
56+
var result = await ((from o in db.Orders
57+
from o2 in db.Orders.Where(x => x.Freight > 50)
58+
where (o.OrderId == o2.OrderId + 1) || (o.OrderId == o2.OrderId - 1)
59+
select new { o.OrderId, OrderId2 = o2.OrderId }).ToListAsync());
5760

58-
Assert.That(result.Count, Is.EqualTo(720));
61+
var sql = sqlSpy.GetWholeLog();
62+
Assert.That(result.Count, Is.EqualTo(720));
63+
Assert.That(sql, Does.Contain(useCrossJoin ? "cross join" : "inner join"));
64+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(useCrossJoin ? 0 : 1));
65+
}
5966
}
6067
}
6168
}

src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs

Lines changed: 104 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010

1111
using System;
12+
using System.Collections;
1213
using System.Collections.Generic;
1314
using System.Linq;
1415
using NHibernate.DomainModel.Northwind.Entities;
16+
using NSubstitute;
1517
using NUnit.Framework;
1618
using NHibernate.Linq;
1719

@@ -757,7 +759,13 @@ from o in c.Orders
757759
where c.Address.City == "London"
758760
select o;
759761

760-
await (ObjectDumper.WriteAsync(q));
762+
using (var sqlSpy = new SqlLogSpy())
763+
{
764+
await (ObjectDumper.WriteAsync(q));
765+
766+
var sql = sqlSpy.GetWholeLog();
767+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(1));
768+
}
761769
}
762770

763771
[Category("JOIN")]
@@ -863,7 +871,13 @@ from p in db.Products
863871
where p.Supplier.Address.Country == "USA" && p.UnitsInStock == 0
864872
select p;
865873

866-
await (ObjectDumper.WriteAsync(q));
874+
using (var sqlSpy = new SqlLogSpy())
875+
{
876+
await (ObjectDumper.WriteAsync(q));
877+
878+
var sql = sqlSpy.GetWholeLog();
879+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(1));
880+
}
867881
}
868882

869883
[Category("JOIN")]
@@ -879,7 +893,16 @@ from et in e.Territories
879893
where e.Address.City == "Seattle"
880894
select new {e.FirstName, e.LastName, et.Region.Description};
881895

882-
await (ObjectDumper.WriteAsync(q));
896+
using (var sqlSpy = new SqlLogSpy())
897+
{
898+
await (ObjectDumper.WriteAsync(q));
899+
900+
var sql = sqlSpy.GetWholeLog();
901+
// EmployeeTerritories and Territories
902+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(2));
903+
// Region
904+
Assert.That(GetTotalOccurrences(sql, "left outer join"), Is.EqualTo(1));
905+
}
883906
}
884907

885908
[Category("JOIN")]
@@ -903,7 +926,13 @@ from e2 in e1.Subordinates
903926
e1.Address.City
904927
};
905928

906-
await (ObjectDumper.WriteAsync(q));
929+
using (var sqlSpy = new SqlLogSpy())
930+
{
931+
await (ObjectDumper.WriteAsync(q));
932+
933+
var sql = sqlSpy.GetWholeLog();
934+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(1));
935+
}
907936
}
908937

909938
[Category("JOIN")]
@@ -918,7 +947,13 @@ from c in db.Customers
918947
join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into orders
919948
select new {c.ContactName, OrderCount = orders.Average(x => x.Freight)};
920949

921-
await (ObjectDumper.WriteAsync(q));
950+
using (var sqlSpy = new SqlLogSpy())
951+
{
952+
await (ObjectDumper.WriteAsync(q));
953+
954+
var sql = sqlSpy.GetWholeLog();
955+
Assert.That(GetTotalOccurrences(sql, "join"), Is.EqualTo(0));
956+
}
922957
}
923958

924959
[Category("JOIN")]
@@ -959,15 +994,32 @@ from c in db.Customers
959994
}
960995

961996
[Category("JOIN")]
962-
[Test(Description = "This sample explictly joins two tables with a composite key and projects results from both tables.")]
963-
public async Task DLinqJoin5dAsync()
997+
[TestCase(true, Description = "This sample explictly joins two tables with a composite key and projects results from both tables.")]
998+
[TestCase(false, Description = "This sample explictly joins two tables with a composite key and projects results from both tables.")]
999+
public async Task DLinqJoin5dAsync(bool useCrossJoin)
9641000
{
1001+
if (useCrossJoin && !Dialect.SupportsCrossJoin)
1002+
{
1003+
Assert.Ignore("Dialect does not support cross join.");
1004+
}
1005+
9651006
var q =
9661007
from c in db.Customers
9671008
join o in db.Orders on new {c.CustomerId, HasContractTitle = c.ContactTitle != null} equals new {o.Customer.CustomerId, HasContractTitle = o.Customer.ContactTitle != null }
9681009
select new { c.ContactName, o.OrderId };
9691010

970-
await (ObjectDumper.WriteAsync(q));
1011+
using (var substitute = SubstituteDialect())
1012+
using (var sqlSpy = new SqlLogSpy())
1013+
{
1014+
ClearQueryPlanCache();
1015+
substitute.Value.SupportsCrossJoin.Returns(useCrossJoin);
1016+
1017+
await (ObjectDumper.WriteAsync(q));
1018+
1019+
var sql = sqlSpy.GetWholeLog();
1020+
Assert.That(sql, Does.Contain(useCrossJoin ? "cross join" : "inner join"));
1021+
Assert.That(GetTotalOccurrences(sql, "left outer join"), Is.EqualTo(1));
1022+
}
9711023
}
9721024

9731025
[Category("JOIN")]
@@ -983,7 +1035,13 @@ join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into ords
9831035
join e in db.Employees on c.Address.City equals e.Address.City into emps
9841036
select new {c.ContactName, ords = ords.Count(), emps = emps.Count()};
9851037

986-
await (ObjectDumper.WriteAsync(q));
1038+
using (var sqlSpy = new SqlLogSpy())
1039+
{
1040+
await (ObjectDumper.WriteAsync(q));
1041+
1042+
var sql = sqlSpy.GetWholeLog();
1043+
Assert.That(GetTotalOccurrences(sql, "join"), Is.EqualTo(0));
1044+
}
9871045
}
9881046

9891047
[Category("JOIN")]
@@ -997,14 +1055,27 @@ join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into ords
9971055
from o in ords
9981056
select new {c.ContactName, o.OrderId, z};
9991057

1000-
await (ObjectDumper.WriteAsync(q));
1058+
using (var sqlSpy = new SqlLogSpy())
1059+
{
1060+
await (ObjectDumper.WriteAsync(q));
1061+
1062+
var sql = sqlSpy.GetWholeLog();
1063+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(1));
1064+
}
10011065
}
10021066

10031067
[Category("JOIN")]
1004-
[Test(Description = "This sample shows a group join with a composite key.")]
1005-
public async Task DLinqJoin9Async()
1068+
[TestCase(true, Description = "This sample shows a group join with a composite key.")]
1069+
[TestCase(false, Description = "This sample shows a group join with a composite key.")]
1070+
public async Task DLinqJoin9Async(bool useCrossJoin)
10061071
{
1007-
var expected =
1072+
if (useCrossJoin && !Dialect.SupportsCrossJoin)
1073+
{
1074+
Assert.Ignore("Dialect does not support cross join.");
1075+
}
1076+
1077+
ICollection expected, actual;
1078+
expected =
10081079
(from o in db.Orders.ToList()
10091080
from p in db.Products.ToList()
10101081
join d in db.OrderLines.ToList()
@@ -1013,14 +1084,26 @@ into details
10131084
from d in details
10141085
select new {o.OrderId, p.ProductId, d.UnitPrice}).ToList();
10151086

1016-
var actual =
1017-
await ((from o in db.Orders
1018-
from p in db.Products
1019-
join d in db.OrderLines
1020-
on new {o.OrderId, p.ProductId} equals new {d.Order.OrderId, d.Product.ProductId}
1021-
into details
1022-
from d in details
1023-
select new {o.OrderId, p.ProductId, d.UnitPrice}).ToListAsync());
1087+
using (var substitute = SubstituteDialect())
1088+
using (var sqlSpy = new SqlLogSpy())
1089+
{
1090+
ClearQueryPlanCache();
1091+
substitute.Value.SupportsCrossJoin.Returns(useCrossJoin);
1092+
1093+
actual =
1094+
await ((from o in db.Orders
1095+
from p in db.Products
1096+
join d in db.OrderLines
1097+
on new { o.OrderId, p.ProductId } equals new { d.Order.OrderId, d.Product.ProductId }
1098+
into details
1099+
from d in details
1100+
select new { o.OrderId, p.ProductId, d.UnitPrice }).ToListAsync());
1101+
1102+
var sql = sqlSpy.GetWholeLog();
1103+
Assert.That(actual.Count, Is.EqualTo(2155));
1104+
Assert.That(sql, Does.Contain(useCrossJoin ? "cross join" : "inner join"));
1105+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(useCrossJoin ? 1 : 2));
1106+
}
10241107

10251108
Assert.AreEqual(expected.Count, actual.Count);
10261109
}
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using NHibernate.Cfg;
5+
using NHibernate.Engine.Query;
6+
using NHibernate.Util;
7+
using NSubstitute;
28
using NUnit.Framework;
39

410
namespace NHibernate.Test.Linq.ByMethod
@@ -20,30 +26,31 @@ public void MultipleLinqJoinsWithSameProjectionNames()
2026
Assert.IsTrue(orders.All(x => x.FirstId == x.SecondId - 1 && x.SecondId == x.ThirdId - 1));
2127
}
2228

23-
[Test]
24-
public void CrossJoinWithPredicateInOnStatement()
29+
[TestCase(false)]
30+
[TestCase(true)]
31+
public void CrossJoinWithPredicateInWhereStatement(bool useCrossJoin)
2532
{
26-
var result =
27-
(from o in db.Orders
28-
from p in db.Products
29-
join d in db.OrderLines
30-
on new { o.OrderId, p.ProductId } equals new { d.Order.OrderId, d.Product.ProductId }
31-
into details
32-
from d in details
33-
select new { o.OrderId, p.ProductId, d.UnitPrice }).Take(10).ToList();
33+
if (useCrossJoin && !Dialect.SupportsCrossJoin)
34+
{
35+
Assert.Ignore("Dialect does not support cross join.");
36+
}
3437

35-
Assert.That(result.Count, Is.EqualTo(10));
36-
}
38+
using (var substitute = SubstituteDialect())
39+
using (var sqlSpy = new SqlLogSpy())
40+
{
41+
ClearQueryPlanCache();
42+
substitute.Value.SupportsCrossJoin.Returns(useCrossJoin);
3743

38-
[Test]
39-
public void CrossJoinWithPredicateInWhereStatement()
40-
{
41-
var result = (from o in db.Orders
42-
from o2 in db.Orders.Where(x => x.Freight > 50)
43-
where (o.OrderId == o2.OrderId + 1) || (o.OrderId == o2.OrderId - 1)
44-
select new { o.OrderId, OrderId2 = o2.OrderId }).ToList();
44+
var result = (from o in db.Orders
45+
from o2 in db.Orders.Where(x => x.Freight > 50)
46+
where (o.OrderId == o2.OrderId + 1) || (o.OrderId == o2.OrderId - 1)
47+
select new { o.OrderId, OrderId2 = o2.OrderId }).ToList();
4548

46-
Assert.That(result.Count, Is.EqualTo(720));
49+
var sql = sqlSpy.GetWholeLog();
50+
Assert.That(result.Count, Is.EqualTo(720));
51+
Assert.That(sql, Does.Contain(useCrossJoin ? "cross join" : "inner join"));
52+
Assert.That(GetTotalOccurrences(sql, "inner join"), Is.EqualTo(useCrossJoin ? 0 : 1));
53+
}
4754
}
4855
}
4956
}

0 commit comments

Comments
 (0)