@@ -14,19 +14,23 @@ namespace SubSonic.Tests.DAL.Builders
1414 using Linq . Expressions ;
1515 using Infrastructure . Builders ;
1616 using Models = Extensions . Test . Models ;
17-
17+ using System . Reflection ;
18+ using SubSonic . Infrastructure ;
19+
1820 [ TestFixture ]
1921 public class DbWherePredicateBuilderTests
2022 : SUT . BaseTestFixture
2123 {
2224 interface IPredicateTestCase
2325 {
24- LambdaExpression Predicate { get ; }
26+ Expression Predicate { get ; }
2527 Type Type { get ; }
26- DbTableExpression Table { get ; }
28+ Type DbSetType { get ; }
29+ Expression Expression { get ; }
2730 }
2831 class GetPredicateFor < TEntity >
2932 : IPredicateTestCase
33+ where TEntity : class
3034 {
3135 public GetPredicateFor ( Expression < Func < TEntity , bool > > selector )
3236 {
@@ -35,9 +39,21 @@ public GetPredicateFor(Expression<Func<TEntity, bool>> selector)
3539
3640 public Type Type => typeof ( TEntity ) ;
3741
38- public LambdaExpression Predicate { get ; }
42+ public Type DbSetType => typeof ( DbSetCollection < > ) . MakeGenericType ( Type ) ;
43+
44+ public Expression Predicate { get ; }
3945
40- public DbTableExpression Table => DbContext . DbModel . GetEntityModel < TEntity > ( ) . Table ;
46+ public Expression Expression
47+ {
48+ get
49+ {
50+ MethodInfo method = typeof ( Queryable ) . GetGenericMethod ( nameof ( Queryable . Where ) , new [ ] { DbSetType , Predicate . GetType ( ) } ) ;
51+
52+ return DbExpression . DbWhere ( method , new [ ] {
53+ DbContext . Current . Set < TEntity > ( ) ? . Expression ,
54+ Predicate } ) ;
55+ }
56+ }
4157 }
4258
4359 private static IEnumerable < IPredicateTestCase > Expressions ( )
@@ -50,14 +66,25 @@ private static IEnumerable<IPredicateTestCase> Expressions()
5066 [ Test ]
5167 public void ShouldBeAbleToGetWherePredicateBody ( )
5268 {
53- // foreach(IPredicateTestCase @case in Expressions())
54- Parallel . ForEach ( Expressions ( ) , @case =>
69+ foreach ( IPredicateTestCase @case in Expressions ( ) )
70+ // Parallel.ForEach(Expressions(), @case =>
5571 {
56- var result = DbWherePredicateBuilder . GetWherePredicate ( @case . Type , @case . Predicate , DbExpressionType . Where , @case . Table ) ;
72+ var result = DbWherePredicateBuilder . GetWhereTranslation ( @case . Expression ) ;
73+
74+ if ( result is DbWhereExpression where )
75+ {
76+ where . Parameters . Count . Should ( ) . NotBe ( 0 ) ;
77+
78+ where . Type . Should ( ) . Be ( typeof ( bool ) ) ;
79+
80+ where . GetArgument ( 0 ) . Type . Should ( ) . Be ( @case . DbSetType ) ;
81+
82+ where . Expression . Should ( ) . NotBeNull ( ) ;
83+ }
5784
5885 result . Should ( ) . NotBeNull ( ) ;
5986 }
60- ) ;
87+ // );
6188 }
6289 }
6390}
0 commit comments