2020-10-29 03:21:10 -04: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.
|
|
|
|
*/
|
2020-11-10 13:45:42 -05:00
|
|
|
import {
|
2021-09-16 11:12:51 -04:00
|
|
|
AnnotationLayer,
|
2022-01-21 14:23:23 -05:00
|
|
|
AnnotationData,
|
2021-09-16 11:12:51 -04:00
|
|
|
AnnotationOpacity,
|
|
|
|
AnnotationSourceType,
|
|
|
|
AnnotationStyle,
|
|
|
|
AnnotationType,
|
|
|
|
FormulaAnnotationLayer,
|
|
|
|
TimeseriesDataRecord,
|
2022-07-13 04:42:11 -04:00
|
|
|
DataRecord,
|
2021-09-16 11:12:51 -04:00
|
|
|
} from '@superset-ui/core';
|
|
|
|
import {
|
|
|
|
evalFormula,
|
2020-11-10 13:45:42 -05:00
|
|
|
extractAnnotationLabels,
|
|
|
|
formatAnnotationLabel,
|
|
|
|
parseAnnotationOpacity,
|
|
|
|
} from '../../src/utils/annotation';
|
|
|
|
|
|
|
|
describe('formatAnnotationLabel', () => {
|
|
|
|
it('should handle default cases properly', () => {
|
|
|
|
expect(formatAnnotationLabel('name')).toEqual('name');
|
|
|
|
expect(formatAnnotationLabel('name', 'title')).toEqual('name - title');
|
|
|
|
expect(formatAnnotationLabel('name', 'title', ['description'])).toEqual(
|
|
|
|
'name - title\n\ndescription',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle missing cases properly', () => {
|
|
|
|
expect(formatAnnotationLabel()).toEqual('');
|
|
|
|
expect(formatAnnotationLabel(undefined, 'title')).toEqual('title');
|
|
|
|
expect(formatAnnotationLabel('name', undefined, ['description'])).toEqual(
|
|
|
|
'name\n\ndescription',
|
|
|
|
);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
formatAnnotationLabel(undefined, undefined, ['description']),
|
|
|
|
).toEqual('description');
|
2020-11-10 13:45:42 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle multiple descriptions properly', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
formatAnnotationLabel('name', 'title', [
|
|
|
|
'description 1',
|
|
|
|
'description 2',
|
|
|
|
]),
|
|
|
|
).toEqual('name - title\n\ndescription 1\ndescription 2');
|
|
|
|
expect(
|
|
|
|
formatAnnotationLabel(undefined, undefined, [
|
|
|
|
'description 1',
|
|
|
|
'description 2',
|
|
|
|
]),
|
|
|
|
).toEqual('description 1\ndescription 2');
|
2020-11-10 13:45:42 -05:00
|
|
|
});
|
|
|
|
});
|
2020-10-29 03:21:10 -04:00
|
|
|
|
|
|
|
describe('extractForecastSeriesContext', () => {
|
|
|
|
it('should extract the correct series name and type', () => {
|
2021-09-16 11:12:51 -04:00
|
|
|
expect(parseAnnotationOpacity(AnnotationOpacity.Low)).toEqual(0.2);
|
|
|
|
expect(parseAnnotationOpacity(AnnotationOpacity.Medium)).toEqual(0.5);
|
|
|
|
expect(parseAnnotationOpacity(AnnotationOpacity.High)).toEqual(0.8);
|
|
|
|
expect(parseAnnotationOpacity(AnnotationOpacity.Undefined)).toEqual(1);
|
2020-10-29 03:21:10 -04:00
|
|
|
expect(parseAnnotationOpacity(undefined)).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
2020-11-10 13:45:42 -05:00
|
|
|
|
|
|
|
describe('extractAnnotationLabels', () => {
|
|
|
|
it('should extract all annotations that can be added to the legend', () => {
|
|
|
|
const layers: AnnotationLayer[] = [
|
|
|
|
{
|
2021-09-16 11:12:51 -04:00
|
|
|
annotationType: AnnotationType.Formula,
|
2020-11-10 13:45:42 -05:00
|
|
|
name: 'My Formula',
|
|
|
|
show: true,
|
2021-09-16 11:12:51 -04:00
|
|
|
style: AnnotationStyle.Solid,
|
2020-11-10 13:45:42 -05:00
|
|
|
value: 'sin(x)',
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2020-11-10 13:45:42 -05:00
|
|
|
},
|
|
|
|
{
|
2021-09-16 11:12:51 -04:00
|
|
|
annotationType: AnnotationType.Formula,
|
2020-11-10 13:45:42 -05:00
|
|
|
name: 'My Hidden Formula',
|
|
|
|
show: false,
|
2021-09-16 11:12:51 -04:00
|
|
|
style: AnnotationStyle.Solid,
|
2020-11-10 13:45:42 -05:00
|
|
|
value: 'sin(2x)',
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2020-11-10 13:45:42 -05:00
|
|
|
},
|
|
|
|
{
|
2021-09-16 11:12:51 -04:00
|
|
|
annotationType: AnnotationType.Interval,
|
2020-11-10 13:45:42 -05:00
|
|
|
name: 'My Interval',
|
2021-09-16 11:12:51 -04:00
|
|
|
sourceType: AnnotationSourceType.Table,
|
2020-11-10 13:45:42 -05:00
|
|
|
show: true,
|
2021-09-16 11:12:51 -04:00
|
|
|
style: AnnotationStyle.Solid,
|
2020-11-10 13:45:42 -05:00
|
|
|
value: 1,
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2020-11-10 13:45:42 -05:00
|
|
|
},
|
|
|
|
{
|
2021-09-16 11:12:51 -04:00
|
|
|
annotationType: AnnotationType.Timeseries,
|
2020-11-10 13:45:42 -05:00
|
|
|
name: 'My Line',
|
|
|
|
show: true,
|
2021-09-16 11:12:51 -04:00
|
|
|
style: AnnotationStyle.Dashed,
|
|
|
|
sourceType: AnnotationSourceType.Line,
|
2020-11-10 13:45:42 -05:00
|
|
|
value: 1,
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2020-11-10 13:45:42 -05:00
|
|
|
},
|
|
|
|
{
|
2021-09-16 11:12:51 -04:00
|
|
|
annotationType: AnnotationType.Timeseries,
|
2020-11-10 13:45:42 -05:00
|
|
|
name: 'My Hidden Line',
|
|
|
|
show: false,
|
2021-09-16 11:12:51 -04:00
|
|
|
style: AnnotationStyle.Dashed,
|
|
|
|
sourceType: AnnotationSourceType.Line,
|
2020-11-10 13:45:42 -05:00
|
|
|
value: 1,
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2020-11-10 13:45:42 -05:00
|
|
|
},
|
|
|
|
];
|
2022-01-21 14:23:23 -05:00
|
|
|
const results: AnnotationData = {
|
2020-11-10 13:45:42 -05:00
|
|
|
'My Interval': {
|
|
|
|
columns: ['col'],
|
|
|
|
records: [{ col: 1 }],
|
|
|
|
},
|
|
|
|
'My Line': [
|
|
|
|
{ key: 'Line 1', values: [] },
|
|
|
|
{ key: 'Line 2', values: [] },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(extractAnnotationLabels(layers, results)).toEqual([
|
|
|
|
'My Formula',
|
|
|
|
'Line 1',
|
|
|
|
'Line 2',
|
|
|
|
]);
|
2020-11-10 13:45:42 -05:00
|
|
|
});
|
|
|
|
});
|
2021-09-16 11:12:51 -04:00
|
|
|
|
|
|
|
describe('evalFormula', () => {
|
|
|
|
const layer: FormulaAnnotationLayer = {
|
|
|
|
annotationType: AnnotationType.Formula,
|
|
|
|
name: 'My Formula',
|
|
|
|
show: true,
|
|
|
|
style: AnnotationStyle.Solid,
|
|
|
|
value: 'x+1',
|
2022-01-21 14:23:23 -05:00
|
|
|
showLabel: true,
|
2021-09-16 11:12:51 -04:00
|
|
|
};
|
|
|
|
it('Should evaluate a regular formula', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
const data: TimeseriesDataRecord[] = [
|
|
|
|
{ __timestamp: 0 },
|
|
|
|
{ __timestamp: 10 },
|
|
|
|
];
|
2021-09-16 11:12:51 -04:00
|
|
|
|
2022-07-13 04:42:11 -04:00
|
|
|
expect(evalFormula(layer, data, '__timestamp', 'time')).toEqual([
|
2022-01-21 14:23:23 -05:00
|
|
|
[0, 1],
|
|
|
|
[10, 11],
|
2021-09-16 11:12:51 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should evaluate a formula containing redundant characters', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
const data: TimeseriesDataRecord[] = [
|
|
|
|
{ __timestamp: 0 },
|
|
|
|
{ __timestamp: 10 },
|
|
|
|
];
|
2021-09-16 11:12:51 -04:00
|
|
|
|
2022-07-13 04:42:11 -04:00
|
|
|
expect(
|
|
|
|
evalFormula(
|
|
|
|
{ ...layer, value: 'y = x* 2 -1' },
|
|
|
|
data,
|
|
|
|
'__timestamp',
|
|
|
|
'time',
|
|
|
|
),
|
|
|
|
).toEqual([
|
2022-01-21 14:23:23 -05:00
|
|
|
[0, -1],
|
|
|
|
[10, 19],
|
2021-09-16 11:12:51 -04:00
|
|
|
]);
|
|
|
|
});
|
2022-07-13 04:42:11 -04:00
|
|
|
|
|
|
|
it('Should evaluate a formula if axis type is category', () => {
|
|
|
|
const data: DataRecord[] = [{ gender: 'boy' }, { gender: 'girl' }];
|
|
|
|
|
|
|
|
expect(
|
|
|
|
evalFormula({ ...layer, value: 'y = 1000' }, data, 'gender', 'category'),
|
|
|
|
).toEqual([
|
|
|
|
['boy', 1000],
|
|
|
|
['girl', 1000],
|
|
|
|
]);
|
|
|
|
});
|
2021-09-16 11:12:51 -04:00
|
|
|
});
|