@@ -16,6 +16,7 @@ namespace SubSonic.Tests.DAL.SqlQueryProvider
1616 using Linq ;
1717 using Linq . Expressions ;
1818 using Tests . DAL . SUT ;
19+ using SubSonic . src ;
1920
2021 [ TestFixture ]
2122 public partial class SqlQueryProviderTests
@@ -1092,5 +1093,59 @@ INNER JOIN page
10921093
10931094 query . Sql . Should ( ) . Be ( expected ) ;
10941095 }
1096+
1097+ [ Test ]
1098+ public void ThrowMissingReferenceForSelectStatementForIncludedEntityWhenMissingInclude ( )
1099+ {
1100+ FluentActions . Invoking ( ( ) =>
1101+ {
1102+ Person person = Context . People . First ( ) ;
1103+
1104+ Expression select = person
1105+ . Renters
1106+ . Where ( ( Renter ) =>
1107+ DateTime . Today . Between ( Renter . StartDate , Renter . EndDate . GetValueOrDefault ( DateTime . Today . AddDays ( 1 ) ) ) )
1108+ . Select ( x => x . Unit )
1109+ . Expression ;
1110+ } ) . Should ( ) . Throw < InvalidOperationException > ( ) . WithMessage ( SubSonicErrorMessages . MissingTableReferenceFor . Format ( typeof ( Unit ) . Name ) ) ;
1111+ }
1112+
1113+ [ Test ]
1114+ public void CanGenerateSelectStatementForIncludedEntity ( )
1115+ {
1116+ string expected = @"SELECT [T1].[ID], [T1].[Bedrooms] AS [NumberOfBedrooms], [T1].[StatusID], [T1].[RealEstatePropertyID]
1117+ FROM [dbo].[Renter] AS [T2]
1118+ INNER JOIN [dbo].[Unit] AS [T1]
1119+ ON ([T1].[ID] = [T2].[UnitID])
1120+ WHERE (([T2].[PersonID] = @personid_1) AND @dt_value_2 BETWEEN [T2].[StartDate] AND COALESCE([T2].[EndDate], @dt_value_3))" ;
1121+
1122+ Person person = Context . People . First ( ) ;
1123+
1124+ Expression select = person
1125+ . Renters
1126+ . Where ( ( Renter ) =>
1127+ DateTime . Today . Between ( Renter . StartDate , Renter . EndDate . GetValueOrDefault ( DateTime . Today . AddDays ( 1 ) ) ) )
1128+ . Include ( x => x . Unit )
1129+ . Select ( x => x . Unit )
1130+ . Expression ;
1131+
1132+ IDbQuery query = null ;
1133+
1134+ var logging = Context . Instance . GetService < ISubSonicLogger < DbSelectPageExpression > > ( ) ;
1135+
1136+ using ( var perf = logging . Start ( "SQL Query Writer" ) )
1137+ {
1138+ FluentActions . Invoking ( ( ) =>
1139+ {
1140+ ISubSonicQueryProvider < Status > builder = Context . Instance . GetService < ISubSonicQueryProvider < Status > > ( ) ;
1141+
1142+ query = builder . ToQuery ( select ) ;
1143+ } ) . Should ( ) . NotThrow ( ) ;
1144+ }
1145+
1146+ query . Sql . Should ( ) . NotBeNullOrEmpty ( ) ;
1147+
1148+ query . Sql . Should ( ) . Be ( expected ) ;
1149+ }
10951150 }
10961151}
0 commit comments