refactor: reorganize dateFilterUtils.tx (#14309)

* refactor: reorganize dateFilterUtils.tx

* move spec to src dirctory
This commit is contained in:
Yongjie Zhao
2021-04-23 16:05:50 +08:00
committed by GitHub
parent d1afca4fa1
commit 67535bb320
7 changed files with 45 additions and 47 deletions

View File

@@ -1,298 +0,0 @@
/**
* 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.
*/
import {
customTimeRangeEncode,
customTimeRangeDecode,
} from 'src/explore/components/controls/DateFilterControl/utils';
describe('Custom TimeRange', () => {
describe('customTimeRangeEncode', () => {
it('1) specific : specific', () => {
expect(
customTimeRangeEncode({
sinceDatetime: '2021-01-20T00:00:00',
sinceMode: 'specific',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'specific',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual('2021-01-20T00:00:00 : 2021-01-27T00:00:00');
});
it('2) specific : relative', () => {
expect(
customTimeRangeEncode({
sinceDatetime: '2021-01-20T00:00:00',
sinceMode: 'specific',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-20T00:00:00',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual(
'2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, day)',
);
});
it('3) now : relative', () => {
expect(
customTimeRangeEncode({
sinceDatetime: 'now',
sinceMode: 'now',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'now',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual('now : DATEADD(DATETIME("now"), 7, day)');
});
it('4) today : relative', () => {
expect(
customTimeRangeEncode({
sinceDatetime: 'today',
sinceMode: 'today',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'today',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual('today : DATEADD(DATETIME("today"), 7, day)');
});
it('5) relative : specific', () => {
expect(
customTimeRangeEncode({
sinceDatetime: '2021-01-27T00:00:00',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'specific',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual(
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 2021-01-27T00:00:00',
);
});
it('6) relative : now', () => {
expect(
customTimeRangeEncode({
sinceDatetime: 'now',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'now',
untilMode: 'now',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual('DATEADD(DATETIME("now"), -7, day) : now');
});
it('7) relative : today', () => {
expect(
customTimeRangeEncode({
sinceDatetime: 'today',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'today',
untilMode: 'today',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual('DATEADD(DATETIME("today"), -7, day) : today');
});
it('8) relative : relative (now)', () => {
expect(
customTimeRangeEncode({
sinceDatetime: 'now',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'now',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
}),
).toEqual(
'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
);
});
it('9) relative : relative (date/time)', () => {
expect(
customTimeRangeEncode({
sinceDatetime: '2021-01-27T00:00:00',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'specific',
anchorValue: '2021-01-27T00:00:00',
}),
).toEqual(
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
);
});
});
describe('customTimeRangeDecode', () => {
it('1) specific : specific', () => {
expect(
customTimeRangeDecode('2021-01-20T00:00:00 : 2021-01-27T00:00:00'),
).toEqual({
customRange: {
sinceDatetime: '2021-01-20T00:00:00',
sinceMode: 'specific',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'specific',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
},
matchedFlag: true,
});
});
it('2) specific : relative', () => {
expect(
customTimeRangeDecode(
'2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, day)',
),
).toEqual({
customRange: {
sinceDatetime: '2021-01-20T00:00:00',
sinceMode: 'specific',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-20T00:00:00',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
},
matchedFlag: true,
});
});
it('3) relative : specific', () => {
expect(
customTimeRangeDecode(
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 2021-01-27T00:00:00',
),
).toEqual({
customRange: {
sinceDatetime: '2021-01-27T00:00:00',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'specific',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
},
matchedFlag: true,
});
});
it('4) relative : relative (now)', () => {
expect(
customTimeRangeDecode(
'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
),
).toEqual({
customRange: {
sinceDatetime: 'now',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: 'now',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'now',
anchorValue: 'now',
},
matchedFlag: true,
});
});
it('5) relative : relative (date/time)', () => {
expect(
customTimeRangeDecode(
'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
),
).toEqual({
customRange: {
sinceDatetime: '2021-01-27T00:00:00',
sinceMode: 'relative',
sinceGrain: 'day',
sinceGrainValue: -7,
untilDatetime: '2021-01-27T00:00:00',
untilMode: 'relative',
untilGrain: 'day',
untilGrainValue: 7,
anchorMode: 'specific',
anchorValue: '2021-01-27T00:00:00',
},
matchedFlag: true,
});
});
});
});

View File

@@ -26,10 +26,6 @@ import {
shouldUseLegacyApi,
getSimpleSQLExpression,
} from 'src/explore/exploreUtils';
import {
buildTimeRangeString,
formatTimeRange,
} from 'src/explore/dateFilterUtils';
import { DashboardStandaloneMode } from 'src/dashboard/util/constants';
import * as hostNamesConfig from 'src/utils/hostNamesConfig';
import { getChartMetadataRegistry } from '@superset-ui/core';
@@ -264,40 +260,6 @@ describe('exploreUtils', () => {
});
});
describe('buildTimeRangeString', () => {
it('generates proper time range string', () => {
expect(
buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'),
).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00');
expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe(
' : 2020-07-30T00:00:00',
);
expect(buildTimeRangeString('', '')).toBe(' : ');
});
});
describe('formatTimeRange', () => {
it('generates a readable time range', () => {
expect(formatTimeRange('Last 7 days')).toBe('Last 7 days');
expect(formatTimeRange('No filter')).toBe('No filter');
expect(formatTimeRange('Yesterday : Tomorrow')).toBe(
'Yesterday < col < Tomorrow',
);
expect(
formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00', [
'inclusive',
'exclusive',
]),
).toBe('2010-07-30 ≤ col < 2020-07-30');
expect(
formatTimeRange('2010-07-30T01:00:00 : ', ['exclusive', 'inclusive']),
).toBe('2010-07-30T01:00:00 < col ≤ ∞');
expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe(
'-∞ < col < 2020-07-30',
);
});
});
describe('getSimpleSQLExpression', () => {
it('returns empty string when subject is undefined', () => {
expect(getSimpleSQLExpression(undefined, '=', 10)).toBe('');