• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 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  constraintsToQueryPrefix,
17  constraintsToQuerySuffix,
18} from '../trace_processor/sql_utils';
19
20// Clean up repeated whitespaces to allow for easier testing.
21function normalize(s: string): string {
22  return s.replace(/\s+/g, ' ');
23}
24
25test('constraintsToQueryPrefix: empty', () => {
26  expect(normalize(constraintsToQueryPrefix({}))).toEqual('');
27});
28
29test('constraintsToQueryPrefix: one CTE', () => {
30  expect(
31    normalize(
32      constraintsToQueryPrefix({
33        commonTableExpressions: {foo: 'select * from bar'},
34      }),
35    ),
36  ).toEqual('WITH foo AS (select * from bar)');
37});
38
39test('constraintsToQueryPrefix: one CTE', () => {
40  expect(
41    normalize(
42      constraintsToQueryPrefix({
43        commonTableExpressions: {
44          foo1: 'select * from bar1',
45          foo2: 'select * from bar2',
46        },
47      }),
48    ),
49  ).toEqual('WITH foo1 AS (select * from bar1), foo2 AS (select * from bar2)');
50});
51
52test('constraintsToQuerySuffix: where', () => {
53  expect(
54    normalize(
55      constraintsToQuerySuffix({
56        filters: ['ts > 1000', 'dur != 0'],
57      }),
58    ),
59  ).toEqual('WHERE ts > 1000 and dur != 0');
60});
61
62test('constraintsToQuerySuffix: order by', () => {
63  expect(
64    normalize(
65      constraintsToQuerySuffix({
66        orderBy: [
67          {fieldName: 'name'},
68          {fieldName: 'count', direction: 'DESC'},
69          undefined,
70          'value',
71        ],
72      }),
73    ),
74  ).toEqual('ORDER BY name, count DESC, value');
75});
76
77test('constraintsToQuerySuffix: limit', () => {
78  expect(normalize(constraintsToQuerySuffix({limit: 3}))).toEqual('LIMIT 3');
79});
80
81test('constraintsToQuerySuffix: group by', () => {
82  expect(
83    normalize(
84      constraintsToQuerySuffix({
85        groupBy: ['foo', undefined, 'bar'],
86      }),
87    ),
88  ).toEqual('GROUP BY foo, bar');
89});
90
91test('constraintsToQuerySuffix: all', () => {
92  expect(
93    normalize(
94      constraintsToQuerySuffix({
95        filters: ['id != 1'],
96        groupBy: ['track_id'],
97        orderBy: [{fieldName: 'ts'}],
98        limit: 1,
99      }),
100    ),
101  ).toEqual('WHERE id != 1 GROUP BY track_id ORDER BY ts LIMIT 1');
102});
103
104test('constraintsToQuerySuffix: all undefined', () => {
105  expect(
106    normalize(
107      constraintsToQuerySuffix({
108        filters: [undefined],
109        orderBy: [undefined, undefined],
110        groupBy: [undefined, undefined],
111      }),
112    ),
113  ).toEqual('');
114});
115