forked from paquesid/badgerhold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_test.go
522 lines (412 loc) · 12.8 KB
/
aggregate_test.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright 2019 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package badgerhold_test
import (
"fmt"
"testing"
"github.com/timshannon/badgerhold"
)
func TestFindAggregateGroup(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
result, err := store.FindAggregate(&ItemTest{}, nil, "Category")
if err != nil {
t.Fatalf("Error finding aggregate data from badgerhold: %s", err)
}
if len(result) != 3 {
t.Fatalf("Wrong number of groupings. Wanted %d got %d", 3, len(result))
}
for i := range result {
var items []ItemTest
var group string
result[i].Reduction(&items)
result[i].Group(&group)
for j := range items {
if items[j].Category != group {
t.Fatalf("Reduction item is not in the proper grouping. Wanted %s, Got %s",
group, items[j].Category)
}
}
}
//test min / max / count
for i := range result {
min := &ItemTest{}
max := &ItemTest{}
var group string
result[i].Group(&group)
result[i].Min("ID", min)
result[i].Max("ID", max)
avg := result[i].Avg("ID")
sum := result[i].Sum("ID")
switch group {
case "animal":
if !min.equal(&testData[2]) {
t.Fatalf("Expected animal min value of %v Got %v", testData[2], min)
}
if !max.equal(&testData[14]) {
t.Fatalf("Expected animal max value of %v Got %v", testData[14], max)
}
if result[i].Count() != 7 {
t.Fatalf("Expected animal count of %d got %d", 7, result[i].Count())
}
if avg != 6.142857142857143 {
t.Fatalf("Expected animal AVG of %v got %v", 6.142857142857143, avg)
}
if sum != 43 {
t.Fatalf("Expected animal SUM of %v got %v", 43, sum)
}
case "food":
if !min.equal(&testData[7]) {
t.Fatalf("Expected food min value of %v Got %v", testData[7], min)
}
if !max.equal(&testData[15]) {
t.Fatalf("Expected food max value of %v Got %v", testData[15], max)
}
if result[i].Count() != 5 {
t.Fatalf("Expected food count of %d got %d", 5, result[i].Count())
}
if avg != 9.2 {
t.Fatalf("Expected food AVG of %v got %v", 9.2, avg)
}
if sum != 46 {
t.Fatalf("Expected food SUM of %v got %v", 46, sum)
}
case "vehicle":
if !min.equal(&testData[0]) {
t.Fatalf("Expected vehicle min value of %v Got %v", testData[0], min)
}
if !max.equal(&testData[11]) {
t.Fatalf("Expected vehicle max value of %v Got %v", testData[11], max)
}
if result[i].Count() != 5 {
t.Fatalf("Expected vehicle count of %d got %d", 5, result[i].Count())
}
if avg != 3.8 {
t.Fatalf("Expected vehicle AVG of %v got %v", 3.8, avg)
}
if sum != 19 {
t.Fatalf("Expected vehicle SUM of %v got %v", 19, sum)
}
default:
t.Fatalf(fmt.Sprintf("Unaccounted for grouping: %s", group))
}
}
})
}
func TestFindAggregateMultipleGrouping(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
result, err := store.FindAggregate(&ItemTest{}, nil, "Category", "Color")
if err != nil {
t.Fatalf("Error finding aggregate data from badgerhold: %s", err)
}
if len(result) != 7 {
t.Fatalf("Wrong number of groupings. Wanted %d got %d", 7, len(result))
}
for i := range result {
var items []*ItemTest
var category string
var color string
result[i].Reduction(&items)
result[i].Group(&category, &color)
for j := range items {
if items[j].Category != category || items[j].Color != color {
t.Fatalf("Reduction item is not in the proper grouping. Wanted %s - %s, Got %s - %s",
category, color, items[j].Category, items[j].Color)
}
}
}
//test min / max / count
for i := range result {
min := &ItemTest{}
max := &ItemTest{}
var category string
var color string
result[i].Group(&category, &color)
result[i].Min("ID", min)
result[i].Max("ID", max)
avg := result[i].Avg("ID")
sum := result[i].Sum("ID")
group := category + "-" + color
switch group {
case "animal-":
if !min.equal(&testData[2]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[2], min)
}
if !max.equal(&testData[14]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[14], max)
}
if result[i].Count() != 6 {
t.Fatalf("Expected %s count of %d got %d", group, 6, result[i].Count())
}
if avg != 7 {
t.Fatalf("Expected %s AVG of %v got %v", group, 7, avg)
}
if sum != 42 {
t.Fatalf("Expected %s SUM of %v got %v", group, 42, sum)
}
case "animal-blue":
if !min.equal(&testData[5]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[5], min)
}
if !max.equal(&testData[5]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[5], max)
}
if result[i].Count() != 1 {
t.Fatalf("Expected %s count of %d got %d", group, 1, result[i].Count())
}
if avg != 1 {
t.Fatalf("Expected %s AVG of %v got %v", group, 1, avg)
}
if sum != 1 {
t.Fatalf("Expected %s SUM of %v got %v", group, 1, sum)
}
case "food-":
if !min.equal(&testData[7]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[7], min)
}
if !max.equal(&testData[15]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[15], max)
}
if result[i].Count() != 4 {
t.Fatalf("Expected %s count of %d got %d", group, 4, result[i].Count())
}
if avg != 9.25 {
t.Fatalf("Expected %s AVG of %v got %v", group, 9.25, avg)
}
if sum != 37 {
t.Fatalf("Expected %s SUM of %v got %v", group, 37, sum)
}
case "food-orange":
if !min.equal(&testData[10]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[10], min)
}
if !max.equal(&testData[10]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[10], max)
}
if result[i].Count() != 1 {
t.Fatalf("Expected %s count of %d got %d", group, 1, result[i].Count())
}
if avg != 9 {
t.Fatalf("Expected %s AVG of %v got %v", group, 9, avg)
}
if sum != 9 {
t.Fatalf("Expected %s SUM of %v got %v", group, 9, sum)
}
case "vehicle-":
if !min.equal(&testData[0]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[0], min)
}
if !max.equal(&testData[3]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[3], max)
}
if result[i].Count() != 3 {
t.Fatalf("Expected %s count of %d got %d", group, 3, result[i].Count())
}
if avg != 1.3333333333333333 {
t.Fatalf("Expected %s AVG of %v got %v", group, 1.3333333333333333, avg)
}
if sum != 4 {
t.Fatalf("Expected %s SUM of %v got %v", group, 4, sum)
}
case "vehicle-orange":
if !min.equal(&testData[6]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[6], min)
}
if !max.equal(&testData[6]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[6], max)
}
if result[i].Count() != 1 {
t.Fatalf("Expected %s count of %d got %d", group, 1, result[i].Count())
}
if avg != 5 {
t.Fatalf("Expected %s AVG of %v got %v", group, 5, avg)
}
if sum != 5 {
t.Fatalf("Expected %s SUM of %v got %v", group, 5, sum)
}
case "vehicle-pink":
if !min.equal(&testData[11]) {
t.Fatalf("Expected %s min value of %v Got %v", group, testData[11], min)
}
if !max.equal(&testData[11]) {
t.Fatalf("Expected %s max value of %v Got %v", group, testData[11], max)
}
if result[i].Count() != 1 {
t.Fatalf("Expected %s count of %d got %d", group, 1, result[i].Count())
}
if avg != 10 {
t.Fatalf("Expected %s AVG of %v got %v", group, 10, avg)
}
if sum != 10 {
t.Fatalf("Expected %s SUM of %v got %v", group, 10, sum)
}
default:
t.Fatalf(fmt.Sprintf("Unaccounted for grouping: %s", group))
}
}
})
}
func TestFindAggregateGroupPointerPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running group without a pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
group := ""
result[0].Group(group)
})
}
func TestFindAggregateGroupLenPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running group with wrong number of groupings did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
group := ""
group2 := ""
result[0].Group(&group, &group2)
})
}
func TestFindAggregateReductionPointerPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Reduction without a pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
var items []ItemTest
result[0].Reduction(items)
})
}
func TestFindAggregateSortInvalidFieldPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Sort on a non-existent field did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
result[0].Sort("BadField")
})
}
func TestFindAggregateSortLowerFieldPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Sort on a lower case field did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
result[0].Sort("category")
})
}
func TestFindAggregateMaxPointerPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Max without a pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
var items ItemTest
result[0].Max("Category", items)
})
}
func TestFindAggregateMaxPointerNilPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Max on a nil pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
var items *ItemTest
result[0].Max("Category", items)
})
}
func TestFindAggregateMinPointerPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Min without a pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
var items ItemTest
result[0].Min("Category", items)
})
}
func TestFindAggregateMinPointerNilPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Min on a nil pointer did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
var items *ItemTest
result[0].Min("Category", items)
})
}
func TestFindAggregateBadSumFieldPanic(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
defer func() {
if r := recover(); r == nil {
t.Fatalf("Running Sum on a bad field did not panic!")
}
}()
result, _ := store.FindAggregate(&ItemTest{}, nil, "Category")
result[0].Sum("BadField")
})
}
func TestFindAggregateBadGroupField(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
_, err := store.FindAggregate(&ItemTest{}, nil, "BadField")
if err == nil {
t.Fatalf("FindAggregate didn't fail when grouped by a bad field.")
}
})
}
func TestFindAggregateWithNoResult(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
result, err := store.FindAggregate(&ItemTest{}, badgerhold.Where("Name").
Eq("Never going to match on this"), "Category")
if err != nil {
t.Fatalf("FindAggregate failed when the query produced no results")
}
if len(result) != 0 {
t.Fatalf("Incorrect result. Wanted 0 got %d", len(result))
}
})
}
func TestFindAggregateWithNoGroupBy(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
insertTestData(t, store)
result, err := store.FindAggregate(&ItemTest{}, nil)
if err != nil {
t.Fatalf("FindAggregate failed when there was no groupBy ")
}
if len(result) != 1 {
t.Fatalf("Incorrect result. Wanted 1 got %d", len(result))
}
})
}