1// Copyright (C) 2022 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 { 16 argColumn, 17 Column, 18 columnFromSqlTableColumn, 19 formatSqlProjection, 20 sqlProjectionsForColumn, 21} from './column'; 22import { 23 ArgSetIdColumn, 24 SqlTableColumn, 25 SqlTableDescription, 26} from './table_description'; 27 28const table: SqlTableDescription = { 29 name: 'table', 30 displayName: 'Table', 31 columns: [ 32 { 33 name: 'id', 34 }, 35 { 36 name: 'name', 37 title: 'Name', 38 }, 39 { 40 name: 'ts', 41 display: { 42 type: 'timestamp', 43 }, 44 }, 45 { 46 name: 'arg_set_id', 47 type: 'arg_set_id', 48 title: 'Arg', 49 }, 50 ], 51}; 52 53test('fromSqlTableColumn', () => { 54 expect(columnFromSqlTableColumn(table.columns[0])).toEqual({ 55 expression: 'id', 56 alias: 'id', 57 title: 'id', 58 }); 59 60 expect(columnFromSqlTableColumn(table.columns[1])).toEqual({ 61 expression: 'name', 62 alias: 'name', 63 title: 'Name', 64 }); 65 66 expect(columnFromSqlTableColumn(table.columns[2])).toEqual({ 67 expression: 'ts', 68 alias: 'ts', 69 title: 'ts', 70 display: { 71 type: 'timestamp', 72 }, 73 }); 74 75 expect( 76 argColumn('slice', table.columns[3] as ArgSetIdColumn, 'foo.bar'), 77 ).toEqual({ 78 expression: "extract_arg(slice.arg_set_id, 'foo.bar')", 79 alias: '_arg_arg_set_id_foo_bar', 80 title: 'Arg foo.bar', 81 }); 82}); 83 84function formatSqlProjectionsForColumn(c: Column): string { 85 return sqlProjectionsForColumn(c).map(formatSqlProjection).join(', '); 86} 87 88test('sqlProjections', () => { 89 const format = (c: SqlTableColumn) => 90 formatSqlProjectionsForColumn(columnFromSqlTableColumn(c)); 91 92 expect(format(table.columns[0])).toEqual('id as id'); 93 expect(format(table.columns[1])).toEqual('name as name'); 94 expect(format(table.columns[2])).toEqual('ts as ts'); 95}); 96