diff --git a/PoweredSoft.DynamicQuery.Test/BeforeFilterTests.cs b/PoweredSoft.DynamicQuery.Test/BeforeFilterTests.cs new file mode 100644 index 0000000..67f339b --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/BeforeFilterTests.cs @@ -0,0 +1,88 @@ +using PoweredSoft.DynamicQuery.Core; +using PoweredSoft.DynamicQuery.Test.Mock; +using PoweredSoft.DynamicLinq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Xunit; + +namespace PoweredSoft.DynamicQuery.Test +{ + public class BeforeFilterTests + { + private class MockBeforeQueryFilterInterceptor : IBeforeQueryFilterInterceptor + { + public IQueryable InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable queryable) + { + return queryable.Where(t => t.Equal("Customer.FirstName", "David")); + } + } + + private class MockBeforeQueryFilterGenericInterceptor : + IBeforeQueryFilterInterceptor, + IBeforeQueryFilterInterceptor + { + public IQueryable InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable queryable) + { + return queryable.Where(t => t.Customer.FirstName == "David"); + } + + public IQueryable InterceptBeforeFiltering(IQueryCriteria criteria, IQueryable queryable) + { + // leave throw it validates the test, if it gets in here it shoulnd't + throw new NotImplementedException(); + } + } + + [Fact] + public void NonGeneric() + { + MockContextFactory.SeedAndTestContextFor("BeforeFilterTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var interceptor = new MockBeforeQueryFilterInterceptor(); + + // queryable of orders. + var queryable = ctx.Orders.AsQueryable(); + + // pass into the interceptor. + queryable = (IQueryable)interceptor.InterceptBeforeFiltering(criteria, queryable); + + // query handler should pass by the same interceptor. + var queryHandler = new QueryHandler(); + queryHandler.AddInterceptor(interceptor); + var result = queryHandler.Execute(ctx.Orders, criteria); + + // compare results. + var expected = queryable.ToList(); + Assert.Equal(expected, result.Data); + }); + } + + [Fact] + public void Generic() + { + MockContextFactory.SeedAndTestContextFor("BeforeFilterTests_Generic", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var interceptor = new MockBeforeQueryFilterGenericInterceptor(); + + // queryable of orders. + var queryable = ctx.Orders.AsQueryable(); + + // pass into the interceptor. + queryable = interceptor.InterceptBeforeFiltering(criteria, queryable); + + // query handler should pass by the same interceptor. + var queryHandler = new QueryHandler(); + queryHandler.AddInterceptor(interceptor); + var result = queryHandler.Execute(ctx.Orders, criteria); + + // compare results. + var expected = queryable.ToList(); + Assert.Equal(expected, result.Data); + }); + } + } +} diff --git a/README.md b/README.md index d83eec9..ca21b1f 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,8 @@ Interceptor | Interface ---------------------------------------|--------------------------------------------------------------------------------------------|-------------------------------------------------------------|------------------------------------------------------------------------------------------------------------ IIncludeStrategyInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IIncludeStrategyInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs) | This is to allow you to specify include paths for the queryable IIncludeStrategyInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/IIncludeStrategyInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs#L65) | This is to allow you to specify include paths for the queryable -IBeforeQueryFilterInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IBeforeQueryFilterInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Before adding the filters to the expression -IBeforeQueryFilterInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/IBeforeQueryFilterInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Before adding the filters to the expression +IBeforeQueryFilterInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IBeforeQueryFilterInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/BeforeFilterTests.cs) | Before adding the filters to the expression +IBeforeQueryFilterInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/IBeforeQueryFilterInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/BeforeFilterTests.cs#L39) | Before adding the filters to the expression INoSortInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/INoSortInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | This is called to allow you to specify an OrderBy in case none is specified, to avoid paging crash with EF6 INoSortInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/INoSortInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | This is called to allow you to specify an OrderBy in case none is specified, to avoid paging crash with EF6