dotnet-dynamic-query/PoweredSoft.DynamicQuery.Test/GroupInterceptorTests.cs

77 lines
2.9 KiB
C#
Raw Normal View History

2019-11-27 21:08:51 -05:00
using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Extensions;
2018-10-23 22:38:28 -04:00
using PoweredSoft.DynamicQuery.Test.Mock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace PoweredSoft.DynamicQuery.Test
{
2019-11-27 21:08:51 -05:00
public partial class GroupInterceptorTests
2018-10-23 22:38:28 -04:00
{
private class MockGroupInterceptor : IGroupInterceptor
{
public IGroup InterceptGroup(IGroup group)
{
return new Group()
{
Path = "Customer.FirstName"
};
}
}
[Fact]
public void Simple()
{
MockContextFactory.SeedAndTestContextFor("GroupInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
{
var expected = ctx.Orders
.OrderBy(t => t.Customer.FirstName)
.GroupBy(t => t.Customer.FirstName)
.Select(t => t.Key)
.ToList();
var criteria = new QueryCriteria();
criteria.Groups.Add(new Group { Path = "CustomerFirstName" });
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
2018-10-23 22:38:28 -04:00
queryHandler.AddInterceptor(new MockGroupInterceptor());
2019-11-27 21:08:51 -05:00
var result = queryHandler.Execute(ctx.Orders.Include(t => t.Customer), criteria, new QueryExecutionOptions
{
GroupByInMemory = true
});
var groupedResult = result.GroupedResult();
var actual = groupedResult.Groups.Select(t => t.GroupValue).ToList();
Assert.Equal(expected, actual);
});
}
[Fact]
public void WithInterptorSimple()
{
MockContextFactory.SeedAndTestContextFor("GroupInterceptorTests_WithInterptorSimple", TestSeeders.SimpleSeedScenario, ctx =>
2019-11-27 21:08:51 -05:00
{
var expected = ctx.Orders
.OrderBy(t => t.Customer.FirstName)
.GroupBy(t => t.Customer.FirstName)
.Select(t => t.Key)
.ToList();
var criteria = new QueryCriteria();
criteria.Groups.Add(new Group { Path = "CustomerFirstName" });
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
2019-11-27 21:08:51 -05:00
queryHandler.AddInterceptor(new MockGroupInterceptor());
queryHandler.AddInterceptor(new MockQueryExecutionOptionsInterceptor());
var result = queryHandler.Execute(ctx.Orders.Include(t => t.Customer), criteria);
var groupedResult = result.GroupedResult();
var actual = groupedResult.Groups.Select(t => t.GroupValue).ToList();
2018-10-23 22:38:28 -04:00
Assert.Equal(expected, actual);
});
}
}
}