2020-12-02 01:20:00 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2022-01-07 20:16:24 -05:00
|
|
|
import processFilters from '../../src/query/processFilters';
|
2019-05-23 16:05:01 -04:00
|
|
|
|
|
|
|
describe('processFilters', () => {
|
|
|
|
it('should handle non-array adhoc_filters', () => {
|
|
|
|
expect(
|
|
|
|
processFilters({
|
|
|
|
granularity: 'something',
|
|
|
|
viz_type: 'custom',
|
|
|
|
datasource: 'boba',
|
|
|
|
}),
|
2020-12-02 01:20:00 -05:00
|
|
|
).toEqual(
|
2021-11-09 07:42:28 -05:00
|
|
|
expect.objectContaining({
|
2022-07-08 11:57:03 -04:00
|
|
|
extras: { having: '', where: '' },
|
2021-11-09 07:42:28 -05:00
|
|
|
filters: [],
|
|
|
|
}),
|
2020-12-02 01:20:00 -05:00
|
|
|
);
|
2019-05-23 16:05:01 -04:00
|
|
|
});
|
|
|
|
|
2020-07-24 07:18:45 -04:00
|
|
|
it('should merge simple adhoc_filters and filters', () => {
|
|
|
|
expect(
|
|
|
|
processFilters({
|
|
|
|
granularity: 'something',
|
|
|
|
viz_type: 'custom',
|
|
|
|
datasource: 'boba',
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
col: 'name',
|
|
|
|
op: '==',
|
|
|
|
val: 'Aaron',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
adhoc_filters: [
|
|
|
|
{
|
|
|
|
expressionType: 'SIMPLE',
|
|
|
|
clause: 'WHERE',
|
|
|
|
subject: 'gender',
|
|
|
|
operator: 'IS NOT NULL',
|
|
|
|
},
|
2022-07-12 01:34:27 -04:00
|
|
|
// ignore simple having filter
|
|
|
|
{
|
|
|
|
expressionType: 'SIMPLE',
|
|
|
|
clause: 'HAVING',
|
|
|
|
subject: 'sum(sales)',
|
|
|
|
operator: '>',
|
|
|
|
comparator: '100',
|
|
|
|
},
|
2020-07-24 07:18:45 -04:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
extras: {
|
|
|
|
having: '',
|
|
|
|
where: '',
|
|
|
|
},
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
col: 'name',
|
|
|
|
op: '==',
|
|
|
|
val: 'Aaron',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
col: 'gender',
|
|
|
|
op: 'IS NOT NULL',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-23 16:05:01 -04:00
|
|
|
it('should handle an empty array', () => {
|
|
|
|
expect(
|
|
|
|
processFilters({
|
2020-07-20 23:24:34 -04:00
|
|
|
where: '1 = 1',
|
2019-05-23 16:05:01 -04:00
|
|
|
granularity: 'something',
|
|
|
|
viz_type: 'custom',
|
|
|
|
datasource: 'boba',
|
|
|
|
adhoc_filters: [],
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
filters: [],
|
2020-07-20 23:24:34 -04:00
|
|
|
extras: {
|
|
|
|
having: '',
|
|
|
|
where: '(1 = 1)',
|
|
|
|
},
|
2019-05-23 16:05:01 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should put adhoc_filters into the correct group and format accordingly', () => {
|
|
|
|
expect(
|
|
|
|
processFilters({
|
|
|
|
granularity: 'something',
|
|
|
|
viz_type: 'custom',
|
|
|
|
datasource: 'boba',
|
|
|
|
adhoc_filters: [
|
|
|
|
{
|
|
|
|
expressionType: 'SIMPLE',
|
|
|
|
clause: 'WHERE',
|
|
|
|
subject: 'milk',
|
|
|
|
operator: 'IS NOT NULL',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expressionType: 'SIMPLE',
|
|
|
|
clause: 'WHERE',
|
|
|
|
subject: 'milk',
|
2020-07-24 07:18:45 -04:00
|
|
|
operator: '==',
|
2019-05-23 16:05:01 -04:00
|
|
|
comparator: 'almond',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expressionType: 'SQL',
|
|
|
|
clause: 'WHERE',
|
2022-03-18 18:08:06 -04:00
|
|
|
sqlExpression: "tea = 'jasmine'",
|
2019-05-23 16:05:01 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expressionType: 'SQL',
|
|
|
|
clause: 'WHERE',
|
2022-03-18 18:08:06 -04:00
|
|
|
sqlExpression: "cup = 'large' -- comment",
|
2019-05-23 16:05:01 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expressionType: 'SQL',
|
|
|
|
clause: 'HAVING',
|
|
|
|
sqlExpression: 'ice = 25 OR ice = 50',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expressionType: 'SQL',
|
|
|
|
clause: 'HAVING',
|
2022-03-18 18:08:06 -04:00
|
|
|
sqlExpression: 'waitTime <= 180 -- comment',
|
2019-05-23 16:05:01 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
).toEqual({
|
2020-07-20 23:24:34 -04:00
|
|
|
extras: {
|
2022-03-18 18:08:06 -04:00
|
|
|
having: '(ice = 25 OR ice = 50) AND (waitTime <= 180 -- comment\n)',
|
|
|
|
where: "(tea = 'jasmine') AND (cup = 'large' -- comment\n)",
|
2020-07-20 23:24:34 -04:00
|
|
|
},
|
2019-05-23 16:05:01 -04:00
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
col: 'milk',
|
|
|
|
op: 'IS NOT NULL',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
col: 'milk',
|
2020-07-24 07:18:45 -04:00
|
|
|
op: '==',
|
2019-05-23 16:05:01 -04:00
|
|
|
val: 'almond',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|