UNPKG

6.18 kBPlain TextView Raw
1import { autoTransform, rename } from '../src/autoTransform';
2
3test('rename', () => {
4 expect(rename('field1', 'sum')).toEqual('SUM(field1)');
5 expect(rename('field2', 'average', 'brackets')).toEqual('AVERAGE(field2)');
6 expect(rename('field3', 'count', 'underline')).toEqual('COUNT_field3');
7 expect(rename('field4', 'max', 'origin')).toEqual('field4');
8 expect(rename('field5', 'max', false)).toEqual('field5');
9 expect(rename('field6', 'max', true)).toEqual('MAX(field6)');
10 expect(rename('field7', 'sum', (originStr: any, aggType: any) => `${originStr}+${aggType}`)).toEqual('field7+sum');
11 expect(rename('field8', 'max', () => `nothing`)).toEqual('nothing');
12 expect(rename('field9', 'max', () => false)).toEqual('false');
13});
14
15test('autoTransform', () => {
16 expect(
17 autoTransform([
18 { gender: 'Male', height: 180 },
19 { gender: 'Female', height: 165 },
20 { gender: 'Male', height: 170 },
21 ]).schemas
22 ).toEqual([
23 {
24 groupBy: ['gender'],
25 actions: [
26 {
27 type: 'sum',
28 field: 'height',
29 as: 'SUM(height)',
30 },
31 ],
32 },
33 ]);
34});
35
36test('autoTransform', () => {
37 expect(
38 autoTransform([
39 { gender: 'Male', height: 180 },
40 { gender: 'Female', height: 165 },
41 { gender: 'Male', height: 170 },
42 ]).result
43 ).toEqual([
44 { gender: 'Male', 'SUM(height)': 350 },
45 { gender: 'Female', 'SUM(height)': 165 },
46 ]);
47});
48
49test('autoTransform', () => {
50 expect(
51 autoTransform(
52 [
53 { gender: 'Male', height: 180 },
54 { gender: 'Female', height: 165 },
55 { gender: 'Male', height: 170 },
56 ],
57 'underline'
58 ).schemas
59 ).toEqual([
60 {
61 groupBy: ['gender'],
62 actions: [
63 {
64 type: 'sum',
65 field: 'height',
66 as: 'SUM_height',
67 },
68 ],
69 },
70 ]);
71});
72
73test('autoTransform', () => {
74 expect(
75 autoTransform(
76 [
77 { gender: 'Male', height: 180 },
78 { gender: 'Female', height: 165 },
79 { gender: 'Male', height: 170 },
80 ],
81 'underline'
82 ).result
83 ).toEqual([
84 { gender: 'Male', SUM_height: 350 },
85 { gender: 'Female', SUM_height: 165 },
86 ]);
87});
88
89test('autoTransform', () => {
90 const data = [
91 { date1: '2019-12-01 08:00:00', count1: 379.44, type1: 'HOTEL1' },
92 { date1: '2019-12-01 08:00:00', count1: 140.24, type1: 'GAME1' },
93 { date1: '2019-12-01 08:00:00', count1: 407.84, type1: 'PLAIN1' },
94 { date1: '2019-12-01 08:00:00', count1: 624.99, type1: 'DIGITAL1' },
95 { date1: '2019-12-08 08:00:00', count1: 610.2, type1: 'HOTEL1' },
96 { date1: '2019-12-08 08:00:00', count1: 6.6, type1: 'GAME1' },
97 { date1: '2019-12-08 08:00:00', count1: 440.54, type1: 'PLAIN1' },
98 { date1: '2019-12-08 08:00:00', count1: 308.34, type1: 'DIGITAL1' },
99 { date1: '2019-12-15 08:00:00', count1: 647.41, type1: 'HOTEL1' },
100 { date1: '2019-12-15 08:00:00', count1: 21.58, type1: 'GAME1' },
101 { date1: '2019-12-15 08:00:00', count1: 904.91, type1: 'PLAIN1' },
102 { date1: '2019-12-15 08:00:00', count1: 851.28, type1: 'DIGITAL1' },
103 { date1: '2019-12-22 08:00:00', count1: 599.93, type1: 'HOTEL1' },
104 { date1: '2019-12-22 08:00:00', count1: 750.45, type1: 'GAME1' },
105 { date1: '2019-12-22 08:00:00', count1: 945.67, type1: 'PLAIN1' },
106 { date1: '2019-12-22 08:00:00', count1: 626.37, type1: 'DIGITAL1' },
107 { date1: '2019-12-29 08:00:00', count1: 83.11, type1: 'HOTEL1' },
108 { date1: '2019-12-29 08:00:00', count1: 403.6, type1: 'GAME1' },
109 { date1: '2019-12-29 08:00:00', count1: 789.67, type1: 'PLAIN1' },
110 { date1: '2019-12-29 08:00:00', count1: 850.6, type1: 'DIGITAL1' },
111 { date1: '2020-01-05 08:00:00', count1: 334.24, type1: 'HOTEL1' },
112 { date1: '2020-01-05 08:00:00', count1: 214.66, type1: 'GAME1' },
113 { date1: '2020-01-05 08:00:00', count1: 901.88, type1: 'PLAIN1' },
114 { date1: '2020-01-05 08:00:00', count1: 206.69, type1: 'DIGITAL1' },
115 { date1: '2020-01-12 08:00:00', count1: 368.82, type1: 'HOTEL1' },
116 { date1: '2020-01-12 08:00:00', count1: 719.22, type1: 'GAME1' },
117 { date1: '2020-01-12 08:00:00', count1: 900.63, type1: 'PLAIN1' },
118 { date1: '2020-01-12 08:00:00', count1: 649.94, type1: 'DIGITAL1' },
119 { date1: '2020-01-19 08:00:00', count1: 797.56, type1: 'HOTEL1' },
120 { date1: '2020-01-19 08:00:00', count1: 187.26, type1: 'GAME1' },
121 { date1: '2020-01-19 08:00:00', count1: 229.12, type1: 'PLAIN1' },
122 { date1: '2020-01-19 08:00:00', count1: 267.03, type1: 'DIGITAL1' },
123 { date1: '2020-01-26 08:00:00', count1: 683.9, type1: 'HOTEL1' },
124 { date1: '2020-01-26 08:00:00', count1: 549.96, type1: 'GAME1' },
125 { date1: '2020-01-26 08:00:00', count1: 262.16, type1: 'PLAIN1' },
126 { date1: '2020-01-26 08:00:00', count1: 265.62, type1: 'DIGITAL1' },
127 { date1: '2020-02-02 08:00:00', count1: 456.68, type1: 'HOTEL1' },
128 { date1: '2020-02-02 08:00:00', count1: 501.63, type1: 'GAME1' },
129 { date1: '2020-02-02 08:00:00', count1: 640.06, type1: 'PLAIN1' },
130 { date1: '2020-02-02 08:00:00', count1: 598.63, type1: 'DIGITAL1' },
131 { date1: '2020-02-09 08:00:00', count1: 654.61, type1: 'HOTEL1' },
132 { date1: '2020-02-09 08:00:00', count1: 924.48, type1: 'GAME1' },
133 { date1: '2020-02-09 08:00:00', count1: 875.01, type1: 'PLAIN1' },
134 { date1: '2020-02-09 08:00:00', count1: 57.06, type1: 'DIGITAL1' },
135 { date1: '2020-02-16 08:00:00', count1: 768.87, type1: 'HOTEL1' },
136 { date1: '2020-02-16 08:00:00', count1: 787.32, type1: 'GAME1' },
137 { date1: '2020-02-16 08:00:00', count1: 582.22, type1: 'PLAIN1' },
138 { date1: '2020-02-16 08:00:00', count1: 546.38, type1: 'DIGITAL1' },
139 { date1: '2020-02-23 08:00:00', count1: 20.74, type1: 'HOTEL1' },
140 { date1: '2020-02-23 08:00:00', count1: 100.86, type1: 'GAME1' },
141 { date1: '2020-02-23 08:00:00', count1: 997.45, type1: 'PLAIN1' },
142 { date1: '2020-02-23 08:00:00', count1: 916.24, type1: 'DIGITAL1' },
143 ];
144
145 expect(autoTransform(data, false).schemas).toEqual([
146 {
147 actions: [
148 {
149 type: 'sum',
150 field: 'count1',
151 as: 'count1',
152 },
153 ],
154 groupBy: ['date1', 'type1'],
155 },
156 ]);
157});