| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /**
- * 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 { customTimeRangeDecode } from '@superset-ui/core';
- 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,
- });
- });
- it('6) specific : relative (now)', () => {
- expect(
- customTimeRangeDecode('now : DATEADD(DATETIME("now"), 7, day)'),
- ).toEqual({
- customRange: {
- sinceDatetime: 'now',
- sinceMode: 'now',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: 'now',
- untilMode: 'relative',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: true,
- });
- });
- it('7) default', () => {
- const SEVEN_DAYS_AGO = new Date();
- const MIDNIGHT = new Date();
- SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
- MIDNIGHT.setHours(0, 0, 0, 0);
- expect(
- customTimeRangeDecode('now : DATEADD(DATETIME("TODAY"), -7, day)'),
- ).toEqual({
- customRange: {
- sinceDatetime: SEVEN_DAYS_AGO.setDate(
- SEVEN_DAYS_AGO.getDate() - 7,
- ).toString(),
- sinceMode: 'relative',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: MIDNIGHT.toString(),
- untilMode: 'specific',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: false,
- });
- });
- it('8) relative : relative return default', () => {
- const SEVEN_DAYS_AGO = new Date();
- SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
- const MIDNIGHT = new Date();
- MIDNIGHT.setHours(0, 0, 0, 0);
- expect(
- customTimeRangeDecode(
- 'DATEADD(DATETIME("2021-01-26T00:00:00"), -55, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
- ),
- ).toEqual({
- customRange: {
- sinceDatetime: SEVEN_DAYS_AGO.setDate(
- SEVEN_DAYS_AGO.getDate() - 7,
- ).toString(),
- sinceMode: 'relative',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: MIDNIGHT.toString(),
- untilMode: 'specific',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: false,
- });
- });
- it('9) empty string returns default', () => {
- const SEVEN_DAYS_AGO = new Date();
- SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
- const MIDNIGHT = new Date();
- MIDNIGHT.setHours(0, 0, 0, 0);
- expect(customTimeRangeDecode('')).toEqual({
- customRange: {
- sinceDatetime: SEVEN_DAYS_AGO.setDate(
- SEVEN_DAYS_AGO.getDate() - 7,
- ).toString(),
- sinceMode: 'relative',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: MIDNIGHT.toString(),
- untilMode: 'specific',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: false,
- });
- });
- it('10) both undefined returns default', () => {
- const SEVEN_DAYS_AGO = new Date();
- SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
- const MIDNIGHT = new Date();
- MIDNIGHT.setHours(0, 0, 0, 0);
- expect(customTimeRangeDecode('undefined : undefined')).toEqual({
- customRange: {
- sinceDatetime: SEVEN_DAYS_AGO.setDate(
- SEVEN_DAYS_AGO.getDate() - 7,
- ).toString(),
- sinceMode: 'relative',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: MIDNIGHT.toString(),
- untilMode: 'specific',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: false,
- });
- });
- it('11) 1 side undefined returns default', () => {
- const SEVEN_DAYS_AGO = new Date();
- SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
- const MIDNIGHT = new Date();
- MIDNIGHT.setHours(0, 0, 0, 0);
- expect(customTimeRangeDecode('undefined : now')).toEqual({
- customRange: {
- sinceDatetime: SEVEN_DAYS_AGO.setDate(
- SEVEN_DAYS_AGO.getDate() - 7,
- ).toString(),
- sinceMode: 'relative',
- sinceGrain: 'day',
- sinceGrainValue: -7,
- untilDatetime: MIDNIGHT.toString(),
- untilMode: 'specific',
- untilGrain: 'day',
- untilGrainValue: 7,
- anchorMode: 'now',
- anchorValue: 'now',
- },
- matchedFlag: false,
- });
- });
- });
|