9
9
10
10
11
11
using System ;
12
+ using System . Collections ;
12
13
using System . Collections . Generic ;
13
14
using System . Linq ;
14
15
using NHibernate . DomainModel . Northwind . Entities ;
16
+ using NSubstitute ;
15
17
using NUnit . Framework ;
16
18
using NHibernate . Linq ;
17
19
@@ -757,7 +759,13 @@ from o in c.Orders
757
759
where c . Address . City == "London"
758
760
select o ;
759
761
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
+ }
761
769
}
762
770
763
771
[ Category ( "JOIN" ) ]
@@ -863,7 +871,13 @@ from p in db.Products
863
871
where p . Supplier . Address . Country == "USA" && p . UnitsInStock == 0
864
872
select p ;
865
873
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
+ }
867
881
}
868
882
869
883
[ Category ( "JOIN" ) ]
@@ -879,7 +893,16 @@ from et in e.Territories
879
893
where e . Address . City == "Seattle"
880
894
select new { e . FirstName , e . LastName , et . Region . Description } ;
881
895
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
+ }
883
906
}
884
907
885
908
[ Category ( "JOIN" ) ]
@@ -903,7 +926,13 @@ from e2 in e1.Subordinates
903
926
e1 . Address . City
904
927
} ;
905
928
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
+ }
907
936
}
908
937
909
938
[ Category ( "JOIN" ) ]
@@ -918,7 +947,13 @@ from c in db.Customers
918
947
join o in db . Orders on c . CustomerId equals o . Customer . CustomerId into orders
919
948
select new { c . ContactName , OrderCount = orders . Average ( x => x . Freight ) } ;
920
949
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
+ }
922
957
}
923
958
924
959
[ Category ( "JOIN" ) ]
@@ -959,15 +994,32 @@ from c in db.Customers
959
994
}
960
995
961
996
[ 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 )
964
1000
{
1001
+ if ( useCrossJoin && ! Dialect . SupportsCrossJoin )
1002
+ {
1003
+ Assert . Ignore ( "Dialect does not support cross join." ) ;
1004
+ }
1005
+
965
1006
var q =
966
1007
from c in db . Customers
967
1008
join o in db . Orders on new { c . CustomerId , HasContractTitle = c . ContactTitle != null } equals new { o . Customer . CustomerId , HasContractTitle = o . Customer . ContactTitle != null }
968
1009
select new { c . ContactName , o . OrderId } ;
969
1010
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
+ }
971
1023
}
972
1024
973
1025
[ Category ( "JOIN" ) ]
@@ -983,7 +1035,13 @@ join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into ords
983
1035
join e in db . Employees on c . Address . City equals e . Address . City into emps
984
1036
select new { c . ContactName , ords = ords . Count ( ) , emps = emps . Count ( ) } ;
985
1037
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
+ }
987
1045
}
988
1046
989
1047
[ Category ( "JOIN" ) ]
@@ -997,14 +1055,27 @@ join o in db.Orders on c.CustomerId equals o.Customer.CustomerId into ords
997
1055
from o in ords
998
1056
select new { c . ContactName , o . OrderId , z } ;
999
1057
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
+ }
1001
1065
}
1002
1066
1003
1067
[ 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 )
1006
1071
{
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 =
1008
1079
( from o in db . Orders . ToList ( )
1009
1080
from p in db . Products . ToList ( )
1010
1081
join d in db . OrderLines . ToList ( )
@@ -1013,14 +1084,26 @@ into details
1013
1084
from d in details
1014
1085
select new { o . OrderId , p . ProductId , d . UnitPrice } ) . ToList ( ) ;
1015
1086
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
+ }
1024
1107
1025
1108
Assert . AreEqual ( expected . Count , actual . Count ) ;
1026
1109
}
0 commit comments