From 90197f46ba02ea2fa21d29d74a42a0f2f69aa976 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Tue, 23 Oct 2018 17:08:32 -0500 Subject: [PATCH] include strategy testings. --- .../IncludeStrategyTests.cs | 89 +++++++++++++++++++ README.md | 4 +- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs diff --git a/PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs b/PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs new file mode 100644 index 0000000..64133ce --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/IncludeStrategyTests.cs @@ -0,0 +1,89 @@ +using Microsoft.EntityFrameworkCore; +using PoweredSoft.DynamicQuery.Core; +using PoweredSoft.DynamicQuery.Test.Mock; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Xunit; + +namespace PoweredSoft.DynamicQuery.Test +{ + public class IncludeStrategyTests + { + private class MockIncludeStrategyInterceptor : IIncludeStrategyInterceptor + { + public IQueryable InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable queryable) + { + queryable = ((IQueryable)queryable).Include(t => t.Customer); + return queryable; + } + } + + private class MockIncludeStrategyGenericInterceptor : + IIncludeStrategyInterceptor, + IIncludeStrategyInterceptor + { + public IQueryable InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable queryable) + { + return queryable.Include(t => t.Customer); + } + + public IQueryable InterceptIncludeStrategy(IQueryCriteria criteria, IQueryable queryable) + { + // should not go in here. + throw new NotImplementedException(); + } + } + + [Fact] + public void NonGeneric() + { + MockContextFactory.SeedAndTestContextFor("IncludeStrategyTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var interceptor = new MockIncludeStrategyInterceptor(); + + // queryable of orders. + var queryable = ctx.Orders.AsQueryable(); + + // pass into the interceptor. + queryable = (IQueryable)interceptor.InterceptIncludeStrategy(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("IncludeStrategyTests_Generic", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var interceptor = new MockIncludeStrategyGenericInterceptor(); + + // queryable of orders. + var queryable = ctx.Orders.AsQueryable(); + + // pass into the interceptor. + queryable = interceptor.InterceptIncludeStrategy(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 cfc18aa..d83eec9 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,8 @@ The following is documented in the order of what they are called by the **defaul Interceptor | Interface | Example | Description ---------------------------------------|--------------------------------------------------------------------------------------------|-------------------------------------------------------------|------------------------------------------------------------------------------------------------------------ -IIncludeStrategyInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IIncludeStrategyInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | 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/TBT.md) | This is to allow you to specify include paths for the queryable +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 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