2019-11-27 21:08:51 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using PoweredSoft.DynamicQuery.Core;
|
2019-03-19 23:54:15 -04:00
|
|
|
|
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" });
|
2019-12-12 18:37:32 -05:00
|
|
|
|
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()
|
|
|
|
|
{
|
2019-12-12 18:37:32 -05:00
|
|
|
|
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" });
|
2019-12-12 18:37:32 -05:00
|
|
|
|
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);
|
2019-03-19 23:54:15 -04:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|