1/* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {PivotTableTreeBuilder} from './pivot_table_controller'; 18 19describe('Pivot Table tree builder', () => { 20 test('aggregates averages correctly', () => { 21 const builder = new PivotTableTreeBuilder( 22 { 23 pivotColumns: [ 24 {kind: 'regular', table: 'slice', column: 'category'}, 25 {kind: 'regular', table: 'slice', column: 'name'}, 26 ], 27 aggregationColumns: [ 28 { 29 aggregationFunction: 'AVG', 30 column: {kind: 'regular', table: 'slice', column: 'dur'}, 31 }, 32 ], 33 countIndex: 1, 34 }, 35 ['cat1', 'name1', 80.0, 2], 36 ); 37 38 builder.ingestRow(['cat1', 'name2', 20.0, 1]); 39 builder.ingestRow(['cat2', 'name3', 20.0, 1]); 40 41 // With two rows of average value 80.0, and two of average value 20.0; 42 // the total sum is 80.0 * 2 + 20.0 + 20.0 = 200.0 over four slices. The 43 // average value should be 200.0 / 4 = 50.0 44 expect(builder.build().aggregates[0]).toBeCloseTo(50.0); 45 }); 46}); 47