1 | import { aggregate } from '../src';
|
2 | import { RowData } from '../src/util/helper';
|
3 |
|
4 | const rows: RowData[] = [
|
5 | { quantity: 1, type: 'A' },
|
6 | { quantity: 3, type: 'B' },
|
7 | { quantity: 4, type: 'B' },
|
8 | { quantity: 9, type: 'A' },
|
9 | { quantity: 8, type: 'B' },
|
10 | { quantity: 1, type: 'A' },
|
11 | { quantity: 2, type: 'A' },
|
12 | { quantity: 5, type: 'C' },
|
13 | { quantity: 0, type: 'A' },
|
14 | { quantity: 7, type: 'C' },
|
15 | ];
|
16 |
|
17 | test('aggregate', () => {
|
18 | expect(
|
19 | aggregate(rows, {
|
20 | groupBy: 'type',
|
21 | as: ['quantity'],
|
22 | op: ['count'],
|
23 | })
|
24 | ).toEqual([
|
25 | { quantity: 5, type: 'A' },
|
26 | { quantity: 3, type: 'B' },
|
27 | { quantity: 2, type: 'C' },
|
28 | ]);
|
29 | });
|
30 |
|
31 | test('aggregate', () => {
|
32 | expect(
|
33 | aggregate(rows, {
|
34 | fields: ['quantity'],
|
35 | groupBy: 'type',
|
36 | as: ['quantity_sum'],
|
37 | op: ['sum'],
|
38 | })
|
39 | ).toEqual([
|
40 | { quantity_sum: 13, quantity: 1, type: 'A' },
|
41 | { quantity_sum: 15, quantity: 3, type: 'B' },
|
42 | { quantity_sum: 12, quantity: 5, type: 'C' },
|
43 | ]);
|
44 | });
|
45 |
|
46 | test('aggregate', () => {
|
47 | expect(
|
48 | aggregate([{ uid: 'a' }, { uid: 'a' }, { uid: 'b' }, { uid: 'c' }, { uid: 'a' }], {
|
49 | fields: ['uid'],
|
50 | as: ['count'],
|
51 | op: ['distinct'],
|
52 | })
|
53 | ).toEqual([{ count: 3, uid: 'a' }]);
|
54 | });
|