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

49 lines
1.6 KiB
C#
Raw Normal View History

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"
});
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);
});
}
}
}