forked from PoweredSoft/DynamicQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregateTests.cs
160 lines (142 loc) · 7.88 KB
/
AggregateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicQuery.Core;
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 AggregateTests
{
[Fact]
public void WithoutGrouping()
{
MockContextFactory.SeedAndTestContextFor("AggregateTests_WithoutGrouping", TestSeeders.SimpleSeedScenario, ctx =>
{
var shouldResult = ctx.OrderItems
.GroupBy(t => true)
.Select(t => new
{
Count = t.Count(),
ItemQuantityMin = t.Min(t2 => t2.Quantity),
ItemQuantityMax = t.Min(t2 => t2.Quantity),
ItemQuantityAverage = t.Average(t2 => t2.Quantity),
ItemQuantitySum = t.Sum(t2 => t2.Quantity),
AvgOfPrice = t.Average(t2 => t2.PriceAtTheTime),
/* not supported by ef core 3.0
First = t.First(),
FirstOrDefault = t.FirstOrDefault(),
Last = t.Last(),
LastOrDefault = t.LastOrDefault()*/
})
.First();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Aggregates = new List<Core.IAggregate>
{
new Aggregate { Type = AggregateType.Count },
new Aggregate { Type = AggregateType.Avg, Path = "Quantity" },
new Aggregate { Type = AggregateType.Sum, Path = "Quantity" },
new Aggregate { Type = AggregateType.Avg, Path = "PriceAtTheTime"},
new Aggregate { Type = AggregateType.Min, Path = "Quantity"},
new Aggregate { Type = AggregateType.Max, Path = "Quantity" },
/*not support by ef core 3.0
new Aggregate { Type = AggregateType.First },
new Aggregate { Type = AggregateType.FirstOrDefault },
new Aggregate { Type = AggregateType.Last },
new Aggregate { Type = AggregateType.LastOrDefault },
*/
}
};
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
var result = queryHandler.Execute(ctx.OrderItems, criteria, new QueryExecutionOptions
{
GroupByInMemory = true
});
var aggCount = result.Aggregates.First(t => t.Type == AggregateType.Count);
/*
var aggFirst = result.Aggregates.First(t => t.Type == AggregateType.First);
var aggFirstOrDefault = result.Aggregates.First(t => t.Type == AggregateType.FirstOrDefault);
var aggLast = result.Aggregates.First(t => t.Type == AggregateType.Last);
var aggLastOrDefault = result.Aggregates.First(t => t.Type == AggregateType.LastOrDefault);*/
var aggItemQuantityMin = result.Aggregates.First(t => t.Type == AggregateType.Min && t.Path == "Quantity");
var aggItemQuantityMax = result.Aggregates.First(t => t.Type == AggregateType.Max && t.Path == "Quantity");
var aggItemQuantityAverage = result.Aggregates.First(t => t.Type == AggregateType.Avg && t.Path == "Quantity");
var aggItemQuantitySum = result.Aggregates.First(t => t.Type == AggregateType.Sum && t.Path == "Quantity");
var aggAvgOfPrice = result.Aggregates.First(t => t.Type == AggregateType.Avg && t.Path == "PriceAtTheTime");
Assert.Equal(shouldResult.Count, aggCount.Value);
/*
Assert.Equal(shouldResult.First?.Id, (aggFirst.Value as OrderItem)?.Id);
Assert.Equal(shouldResult.FirstOrDefault?.Id, (aggFirstOrDefault.Value as OrderItem)?.Id);
Assert.Equal(shouldResult.Last?.Id, (aggLast.Value as OrderItem)?.Id);
Assert.Equal(shouldResult.LastOrDefault?.Id, (aggLastOrDefault.Value as OrderItem)?.Id);*/
Assert.Equal(shouldResult.ItemQuantityAverage, aggItemQuantityAverage.Value);
Assert.Equal(shouldResult.ItemQuantitySum, aggItemQuantitySum.Value);
Assert.Equal(shouldResult.AvgOfPrice, aggAvgOfPrice.Value);
});
}
[Fact]
public void WithGrouping()
{
MockContextFactory.SeedAndTestContextFor("AggregateTests_WithGrouping", TestSeeders.SimpleSeedScenario, ctx =>
{
var shouldResults = ctx.OrderItems
.GroupBy(t => t.Order.CustomerId)
.Select(t => new
{
GroupValue = t.Key,
Count = t.Count(),
ItemQuantityAverage = t.Average(t2 => t2.Quantity),
ItemQuantitySum = t.Sum(t2 => t2.Quantity),
AvgOfPrice = t.Average(t2 => t2.PriceAtTheTime)
})
.ToList();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Groups = new List<IGroup>
{
new Group { Path = "Order.CustomerId" }
},
Aggregates = new List<Core.IAggregate>
{
new Aggregate { Type = AggregateType.Count },
new Aggregate { Type = AggregateType.Avg, Path = "Quantity" },
new Aggregate { Type = AggregateType.Sum, Path = "Quantity" },
new Aggregate { Type = AggregateType.Avg, Path = "PriceAtTheTime"}
}
};
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
var queryable = ctx.OrderItems.Include(t => t.Order);
var result = queryHandler.Execute(queryable, criteria, new QueryExecutionOptions
{
GroupByInMemory = true
});
var groupedResult = result as IQueryExecutionGroupResult<OrderItem>;
Assert.NotNull(groupedResult);
var groups = groupedResult.Groups;
// validate group and aggregates of groups.
Assert.Equal(groups.Count, shouldResults.Count);
Assert.All(groups, g =>
{
var index = groups.IndexOf(g);
var shouldResult = shouldResults[index];
// validate the group value.
Assert.Equal(g.GroupValue, shouldResult.GroupValue);
// validate the group aggregates.
var aggCount = g.Aggregates.First(t => t.Type == AggregateType.Count);
var aggItemQuantityAverage = g.Aggregates.First(t => t.Type == AggregateType.Avg && t.Path == "Quantity");
var aggItemQuantitySum = g.Aggregates.First(t => t.Type == AggregateType.Sum && t.Path == "Quantity");
var aggAvgOfPrice = g.Aggregates.First(t => t.Type == AggregateType.Avg && t.Path == "PriceAtTheTime");
Assert.Equal(shouldResult.Count, aggCount.Value);
Assert.Equal(shouldResult.ItemQuantityAverage, aggItemQuantityAverage.Value);
Assert.Equal(shouldResult.ItemQuantitySum, aggItemQuantitySum.Value);
Assert.Equal(shouldResult.AvgOfPrice, aggAvgOfPrice.Value);
});
});
}
}
}