2019-11-27 21:08:51 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using PoweredSoft.DynamicQuery.Core;
|
2018-10-23 22:55:41 -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
|
|
|
|
|
{
|
|
|
|
|
public class AggregateInterceptorTests
|
|
|
|
|
{
|
|
|
|
|
private class MockAggregateInterceptor : IAggregateInterceptor
|
|
|
|
|
{
|
|
|
|
|
public IAggregate InterceptAggregate(IAggregate aggregate) => new Aggregate
|
|
|
|
|
{
|
2019-11-27 21:08:51 -05:00
|
|
|
|
Path = "Price",
|
2018-10-23 22:55:41 -04:00
|
|
|
|
Type = AggregateType.Avg
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Simple()
|
|
|
|
|
{
|
|
|
|
|
MockContextFactory.SeedAndTestContextFor("AggregatorInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
|
|
|
|
|
{
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var expected = ctx.Items
|
|
|
|
|
.GroupBy(t => true)
|
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
PriceAtTheTime = t.Average(t2 => t2.Price)
|
|
|
|
|
}).First();
|
2018-10-23 22:55:41 -04:00
|
|
|
|
|
|
|
|
|
var criteria = new QueryCriteria();
|
|
|
|
|
criteria.Aggregates.Add(new Aggregate
|
|
|
|
|
{
|
|
|
|
|
Type = AggregateType.Avg,
|
|
|
|
|
Path = "ItemPrice"
|
|
|
|
|
});
|
2019-12-12 18:37:32 -05:00
|
|
|
|
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
|
2018-10-23 22:55:41 -04:00
|
|
|
|
queryHandler.AddInterceptor(new MockAggregateInterceptor());
|
2019-11-27 21:08:51 -05:00
|
|
|
|
var result = queryHandler.Execute(ctx.Items, criteria);
|
2018-10-23 22:55:41 -04:00
|
|
|
|
Assert.Equal(expected.PriceAtTheTime, result.Aggregates.First().Value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|