-
Notifications
You must be signed in to change notification settings - Fork 7
/
GroupTests.cs
185 lines (164 loc) · 6.48 KB
/
GroupTests.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Extensions;
using PoweredSoft.DynamicQuery.Test.Mock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace PoweredSoft.DynamicQuery.Test
{
public class GroupTests
{
[Fact]
public void Simple()
{
MockContextFactory.SeedAndTestContextFor("GroupTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
{
var shouldResult = ctx.Orders
.OrderBy(t => t.CustomerId)
.ToList()
.GroupBy(t => t.CustomerId)
.Select(t => new
{
CustomerId = t.Key,
Orders = t.ToList()
})
.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 = "CustomerId" }
}
};
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
var result = queryHandler.Execute(ctx.Orders, criteria, new QueryExecutionOptions
{
GroupByInMemory = true,
GroupByInMemoryNullCheck = false
});
var groupedResult = result.GroupedResult();
// top level should have same amount of group levels.
Assert.Equal(groupedResult.Groups.Count, shouldResult.Count);
for (var i = 0; i < shouldResult.Count; i++)
{
var expected = shouldResult[0];
var actual = groupedResult.Groups[0];
Assert.Equal(expected.CustomerId, actual.GroupValue);
var expectedOrderIds = expected.Orders.Select(t => t.Id).ToList();
var actualOrderIds = actual.Data.Cast<Order>().Select(t => t.Id).ToList();
Assert.Equal(expectedOrderIds, actualOrderIds);
}
});
}
[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 }
}
};
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
var result = queryHandler.Execute(ctx.Tickets, criteria, new QueryExecutionOptions
{
GroupByInMemory = true
});
var groupedResult = result.GroupedResult();
var firstGroup = groupedResult.Groups.FirstOrDefault();
Assert.NotNull(firstGroup);
var secondGroup = groupedResult.Groups.Skip(1).FirstOrDefault();
Assert.NotNull(secondGroup);
var expected = ctx.Tickets.Select(t => t.TicketType).Distinct().Count();
var c = groupedResult.Groups.Select(t => t.GroupValue).Count();
Assert.Equal(expected, c);
});
}
[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();
var queryHandler = new QueryHandler(Enumerable.Empty<IQueryInterceptorProvider>());
queryHandler.AddInterceptor(interceptor);
var result = queryHandler.Execute<Ticket, InterceptorWithGroupingFakeModel>(ctx.Tickets, criteria, new QueryExecutionOptions
{
GroupByInMemory = true
});
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++;
}
public Task AfterReadAsync(List<Tuple<Ticket, object>> pairs, CancellationToken cancellationToken = default(CancellationToken))
{
Test2 = true;
Count++;
return Task.CompletedTask;
}
public void AfterReadEntity(List<Ticket> entities)
{
Test3 = true;
Count++;
}
public Task AfterReadEntityAsync(List<Ticket> entities, CancellationToken cancellationToken = default(CancellationToken))
{
Test4 = true;
Count++;
return Task.CompletedTask;
}
public object InterceptResultTo(Ticket entity)
{
return new InterceptorWithGroupingFakeModel();
}
}
}