diff --git a/PoweredSoft.DynamicQuery.Test/ConvertibleInterceptorTests.cs b/PoweredSoft.DynamicQuery.Test/ConvertibleInterceptorTests.cs new file mode 100644 index 0000000..d43990d --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/ConvertibleInterceptorTests.cs @@ -0,0 +1,84 @@ +using PoweredSoft.DynamicQuery.Core; +using PoweredSoft.DynamicQuery.Test.Mock; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace PoweredSoft.DynamicQuery.Test +{ + public class QueryConvertInterceptorTests + { + private class CustomerModel + { + public long Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string FullName => $"{FirstName} {LastName}"; + } + + private class MockQueryConvertInterceptor : IQueryConvertInterceptor + { + public object InterceptResultTo(object entity) + { + var customer = entity as Customer; + var personModel = new CustomerModel + { + Id = customer.Id, + FirstName = customer.FirstName, + LastName = customer.LastName + }; + return personModel; + } + } + + private class MockQueryConvertGenericInterceptor : + IQueryConvertInterceptor, + IQueryConvertInterceptor + { + public object InterceptResultTo(Customer entity) + { + var customer = entity; + var personModel = new CustomerModel + { + Id = customer.Id, + FirstName = customer.FirstName, + LastName = customer.LastName + }; + return personModel; + } + + public object InterceptResultTo(Order entity) + { + // leave the throw, its on purpose to match the type testing. + throw new NotImplementedException(); + } + } + + [Fact] + public void NonGeneric() + { + MockContextFactory.SeedAndTestContextFor("QueryConvertInterceptorTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var queryHandler = new QueryHandler(); + queryHandler.AddInterceptor(new MockQueryConvertInterceptor()); + var result = queryHandler.Execute(ctx.Customers, criteria); + Assert.All(result.Data, t => Assert.IsType(t)); + }); + } + + [Fact] + public void Generic() + { + MockContextFactory.SeedAndTestContextFor("ConvertibleIntereceptorTests_Generic", TestSeeders.SimpleSeedScenario, ctx => + { + var criteria = new QueryCriteria(); + var queryHandler = new QueryHandler(); + queryHandler.AddInterceptor(new MockQueryConvertGenericInterceptor()); + var result = queryHandler.Execute(ctx.Customers, criteria); + Assert.All(result.Data, t => Assert.IsType(t)); + }); + } + } +} diff --git a/README.md b/README.md index 2a5e94d..2177c6a 100644 --- a/README.md +++ b/README.md @@ -107,5 +107,5 @@ IAggregateInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IAgg Interceptor | Interface | Example | Description ----------------------------------|---------------------------------------------------------------------------------------|-------------------------------------------------------------|------------------------------------------------------------------------------------------------ -IQueryConvertInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | This interceptor allows you to replace the object that is being returned by the query, by another object instance -IQueryConvertInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | This interceptor allows you to replace the object that is being returned by the query, by another object instance +IQueryConvertInterceptor | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/ConvertibleInterceptorTests.cs) | This interceptor allows you to replace the object that is being returned by the query, by another object instance +IQueryConvertInterceptor<T> | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryConvertInterceptor.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/ConvertibleInterceptorTests.cs#L72) | This interceptor allows you to replace the object that is being returned by the query, by another object instance