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