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-22 23:58:58 -04:00
|
|
|
|
using PoweredSoft.DynamicQuery.Test.Mock;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2019-02-23 23:47:50 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-10-22 23:58:58 -04:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicQuery.Test
|
|
|
|
|
{
|
|
|
|
|
public class GroupTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Simple()
|
|
|
|
|
{
|
|
|
|
|
MockContextFactory.SeedAndTestContextFor("GroupTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
|
|
|
|
|
{
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var shouldResult = ctx.Orders
|
|
|
|
|
.OrderBy(t => t.CustomerId)
|
|
|
|
|
.ToList()
|
|
|
|
|
.GroupBy(t => t.CustomerId)
|
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
CustomerId = t.Key,
|
|
|
|
|
Orders = t.ToList()
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2018-10-22 23:58:58 -04:00
|
|
|
|
|
|
|
|
|
// query handler that is empty should be the same as running to list.
|
|
|
|
|
var criteria = new QueryCriteria()
|
|
|
|
|
{
|
|
|
|
|
Groups = new List<IGroup>
|
|
|
|
|
{
|
2019-11-27 21:08:51 -05:00
|
|
|
|
new Group { Path = "CustomerId" }
|
2018-10-22 23:58:58 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var result = queryHandler.Execute(ctx.Orders, criteria, new QueryExecutionOptions
|
|
|
|
|
{
|
|
|
|
|
GroupByInMemory = true,
|
|
|
|
|
GroupByInMemoryNullCheck = false
|
|
|
|
|
});
|
2019-03-19 23:54:15 -04:00
|
|
|
|
var groupedResult = result.GroupedResult();
|
2018-10-22 23:58:58 -04:00
|
|
|
|
|
|
|
|
|
// top level should have same amount of group levels.
|
2019-03-19 23:54:15 -04:00
|
|
|
|
Assert.Equal(groupedResult.Groups.Count, shouldResult.Count);
|
2019-02-23 23:47:50 -05:00
|
|
|
|
for (var i = 0; i < shouldResult.Count; i++)
|
2018-10-22 23:58:58 -04:00
|
|
|
|
{
|
|
|
|
|
var expected = shouldResult[0];
|
2019-03-19 23:54:15 -04:00
|
|
|
|
var actual = groupedResult.Groups[0];
|
2019-11-27 21:08:51 -05:00
|
|
|
|
Assert.Equal(expected.CustomerId, actual.GroupValue);
|
2018-10-22 23:58:58 -04:00
|
|
|
|
|
|
|
|
|
var expectedOrderIds = expected.Orders.Select(t => t.Id).ToList();
|
|
|
|
|
var actualOrderIds = actual.Data.Cast<Order>().Select(t => t.Id).ToList();
|
|
|
|
|
Assert.Equal(expectedOrderIds, actualOrderIds);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-11-15 20:42:30 -05:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GroupComplex()
|
|
|
|
|
{
|
|
|
|
|
MockContextFactory.SeedAndTestContextFor("GroupTests_Complex", TestSeeders.SeedTicketScenario, ctx =>
|
|
|
|
|
{
|
|
|
|
|
var criteria = new QueryCriteria()
|
|
|
|
|
{
|
|
|
|
|
Groups = new List<IGroup>()
|
|
|
|
|
{
|
|
|
|
|
new Group { Path = "TicketType" },
|
|
|
|
|
new Group { Path = "Priority" }
|
|
|
|
|
},
|
|
|
|
|
Aggregates = new List<IAggregate>()
|
|
|
|
|
{
|
|
|
|
|
new Aggregate { Type = AggregateType.Count }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var result = queryHandler.Execute(ctx.Tickets, criteria, new QueryExecutionOptions
|
|
|
|
|
{
|
|
|
|
|
GroupByInMemory = true
|
|
|
|
|
});
|
2018-11-15 20:42:30 -05:00
|
|
|
|
|
2019-03-19 23:54:15 -04:00
|
|
|
|
var groupedResult = result.GroupedResult();
|
|
|
|
|
|
|
|
|
|
var firstGroup = groupedResult.Groups.FirstOrDefault();
|
2018-11-15 20:42:30 -05:00
|
|
|
|
Assert.NotNull(firstGroup);
|
2019-03-19 23:54:15 -04:00
|
|
|
|
var secondGroup = groupedResult.Groups.Skip(1).FirstOrDefault();
|
2018-11-15 20:42:30 -05:00
|
|
|
|
Assert.NotNull(secondGroup);
|
|
|
|
|
|
|
|
|
|
var expected = ctx.Tickets.Select(t => t.TicketType).Distinct().Count();
|
2019-03-19 23:54:15 -04:00
|
|
|
|
var c = groupedResult.Groups.Select(t => t.GroupValue).Count();
|
2018-11-15 20:42:30 -05:00
|
|
|
|
Assert.Equal(expected, c);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-02-23 23:47:50 -05:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void InterceptorsWithGrouping()
|
|
|
|
|
{
|
|
|
|
|
MockContextFactory.SeedAndTestContextFor("GroupTests_InterceptorsWithGrouping", TestSeeders.SeedTicketScenario, ctx =>
|
|
|
|
|
{
|
|
|
|
|
var criteria = new QueryCriteria()
|
|
|
|
|
{
|
|
|
|
|
Groups = new List<IGroup>()
|
|
|
|
|
{
|
|
|
|
|
new Group { Path = "TicketType" }
|
|
|
|
|
},
|
|
|
|
|
Aggregates = new List<IAggregate>()
|
|
|
|
|
{
|
|
|
|
|
new Aggregate { Type = AggregateType.Count }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var interceptor = new InterceptorsWithGrouping();
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2019-02-23 23:47:50 -05:00
|
|
|
|
queryHandler.AddInterceptor(interceptor);
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var result = queryHandler.Execute<Ticket, InterceptorWithGroupingFakeModel>(ctx.Tickets, criteria, new QueryExecutionOptions
|
|
|
|
|
{
|
|
|
|
|
GroupByInMemory = true
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-23 23:47:50 -05:00
|
|
|
|
Assert.Equal(4, interceptor.Count);
|
|
|
|
|
Assert.True(interceptor.Test);
|
|
|
|
|
Assert.True(interceptor.Test2);
|
|
|
|
|
Assert.True(interceptor.Test3);
|
|
|
|
|
Assert.True(interceptor.Test4);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class InterceptorWithGroupingFakeModel
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class InterceptorsWithGrouping :
|
|
|
|
|
IAfterReadEntityInterceptor<Ticket>,
|
|
|
|
|
IAfterReadEntityInterceptorAsync<Ticket>,
|
|
|
|
|
IAfterReadInterceptor<Ticket>,
|
|
|
|
|
IAfterReadInterceptorAsync<Ticket>,
|
|
|
|
|
IQueryConvertInterceptor<Ticket>
|
|
|
|
|
{
|
|
|
|
|
public int Count { get; set; } = 0;
|
|
|
|
|
public bool Test { get; set; } = false;
|
|
|
|
|
public bool Test2 { get; set; } = false;
|
|
|
|
|
public bool Test3 { get; set; } = false;
|
|
|
|
|
public bool Test4 { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
public void AfterRead(List<Tuple<Ticket, object>> pairs)
|
|
|
|
|
{
|
|
|
|
|
Test = true;
|
|
|
|
|
Count++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 15:11:57 -04:00
|
|
|
|
public Task AfterReadAsync(List<Tuple<Ticket, object>> pairs, CancellationToken cancellationToken = default(CancellationToken))
|
2019-02-23 23:47:50 -05:00
|
|
|
|
{
|
|
|
|
|
Test2 = true;
|
|
|
|
|
Count++;
|
2019-10-13 15:11:57 -04:00
|
|
|
|
return Task.CompletedTask;
|
2019-02-23 23:47:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AfterReadEntity(List<Ticket> entities)
|
|
|
|
|
{
|
|
|
|
|
Test3 = true;
|
|
|
|
|
Count++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 15:11:57 -04:00
|
|
|
|
public Task AfterReadEntityAsync(List<Ticket> entities, CancellationToken cancellationToken = default(CancellationToken))
|
2019-02-23 23:47:50 -05:00
|
|
|
|
{
|
|
|
|
|
Test4 = true;
|
|
|
|
|
Count++;
|
2019-10-13 15:11:57 -04:00
|
|
|
|
return Task.CompletedTask;
|
2019-02-23 23:47:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object InterceptResultTo(Ticket entity)
|
|
|
|
|
{
|
|
|
|
|
return new InterceptorWithGroupingFakeModel();
|
|
|
|
|
}
|
2018-10-22 23:58:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|