1// Copyright (C) 2024 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {createFakeTraceImpl} from '../../../../core/fake_trace_impl'; 16import {tableColumnId} from './table_column'; 17import {SqlTableState} from './state'; 18import {SqlTableDescription} from './table_description'; 19import {StandardColumn, TimestampColumn} from './columns'; 20 21const idColumn = new StandardColumn('id'); 22const nameColumn = new StandardColumn('name'); 23const tsColumn = new TimestampColumn('ts'); 24 25const table: SqlTableDescription = { 26 name: 'table', 27 displayName: 'Table', 28 columns: [idColumn, nameColumn, tsColumn], 29}; 30 31test('sqlTableState: columnManupulation', () => { 32 const trace = createFakeTraceImpl({allowQueries: true}); 33 const state = new SqlTableState(trace, table); 34 35 state.addColumn(tsColumn, 0); 36 37 expect(state.getSelectedColumns().map((c) => tableColumnId(c))).toEqual([ 38 'id', 39 'ts', 40 'name', 41 'ts', 42 ]); 43 44 state.hideColumnAtIndex(0); 45 46 expect(state.getSelectedColumns().map((c) => tableColumnId(c))).toEqual([ 47 'ts', 48 'name', 49 'ts', 50 ]); 51}); 52 53test('sqlTableState: sortedColumns', () => { 54 const trace = createFakeTraceImpl({allowQueries: true}); 55 const state = new SqlTableState(trace, table); 56 57 // Verify that we have three columns: "id", "name" and "ts" and save 58 // references to them. 59 expect(state.getSelectedColumns().map((c) => tableColumnId(c))).toEqual([ 60 'id', 61 'name', 62 'ts', 63 ]); 64 65 // Sort by name column and verify that it is sorted by. 66 state.sortBy({ 67 column: nameColumn, 68 direction: 'ASC', 69 }); 70 expect(state.isSortedBy(idColumn)).toBe(undefined); 71 expect(state.isSortedBy(nameColumn)).toBe('ASC'); 72 73 // Sort by the same column in the opposite direction. 74 state.sortBy({ 75 column: nameColumn, 76 direction: 'DESC', 77 }); 78 expect(state.isSortedBy(idColumn)).toBe(undefined); 79 expect(state.isSortedBy(nameColumn)).toBe('DESC'); 80 81 // Sort by the id column. 82 state.sortBy({ 83 column: idColumn, 84 direction: 'ASC', 85 }); 86 expect(state.isSortedBy(idColumn)).toBe('ASC'); 87 expect(state.isSortedBy(nameColumn)).toBe(undefined); 88 89 // When the column is hidden, it should no longer be sorted by 90 // and we should fall back to the previously sorted by column. 91 state.hideColumnAtIndex(0); 92 expect(state.isSortedBy(nameColumn)).toBe('DESC'); 93 94 // Remove the sorting and verify that we are no sorted by. 95 state.sortBy({column: nameColumn, direction: undefined}); 96 expect(state.isSortedBy(nameColumn)).toBe(undefined); 97}); 98 99// Clean up repeated whitespaces to allow for easier testing. 100function normalize(s: string): string { 101 return s.replace(/\s+/g, ' ').trim(); 102} 103 104test('sqlTableState: sqlStatement', () => { 105 const trace = createFakeTraceImpl({allowQueries: true}); 106 const state = new SqlTableState(trace, table); 107 108 // Check the generated SQL statement. 109 expect(normalize(state.getCurrentRequest().query)).toBe( 110 'SELECT table_0.id AS id, table_0.name AS name, table_0.ts AS ts FROM table AS table_0 LIMIT 101 OFFSET 0', 111 ); 112}); 113