customTimeRangeDecode.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { customTimeRangeDecode } from '@superset-ui/core';
  20. describe('customTimeRangeDecode', () => {
  21. it('1) specific : specific', () => {
  22. expect(
  23. customTimeRangeDecode('2021-01-20T00:00:00 : 2021-01-27T00:00:00'),
  24. ).toEqual({
  25. customRange: {
  26. sinceDatetime: '2021-01-20T00:00:00',
  27. sinceMode: 'specific',
  28. sinceGrain: 'day',
  29. sinceGrainValue: -7,
  30. untilDatetime: '2021-01-27T00:00:00',
  31. untilMode: 'specific',
  32. untilGrain: 'day',
  33. untilGrainValue: 7,
  34. anchorMode: 'now',
  35. anchorValue: 'now',
  36. },
  37. matchedFlag: true,
  38. });
  39. });
  40. it('2) specific : relative', () => {
  41. expect(
  42. customTimeRangeDecode(
  43. '2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, day)',
  44. ),
  45. ).toEqual({
  46. customRange: {
  47. sinceDatetime: '2021-01-20T00:00:00',
  48. sinceMode: 'specific',
  49. sinceGrain: 'day',
  50. sinceGrainValue: -7,
  51. untilDatetime: '2021-01-20T00:00:00',
  52. untilMode: 'relative',
  53. untilGrain: 'day',
  54. untilGrainValue: 7,
  55. anchorMode: 'now',
  56. anchorValue: 'now',
  57. },
  58. matchedFlag: true,
  59. });
  60. });
  61. it('3) relative : specific', () => {
  62. expect(
  63. customTimeRangeDecode(
  64. 'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 2021-01-27T00:00:00',
  65. ),
  66. ).toEqual({
  67. customRange: {
  68. sinceDatetime: '2021-01-27T00:00:00',
  69. sinceMode: 'relative',
  70. sinceGrain: 'day',
  71. sinceGrainValue: -7,
  72. untilDatetime: '2021-01-27T00:00:00',
  73. untilMode: 'specific',
  74. untilGrain: 'day',
  75. untilGrainValue: 7,
  76. anchorMode: 'now',
  77. anchorValue: 'now',
  78. },
  79. matchedFlag: true,
  80. });
  81. });
  82. it('4) relative : relative (now)', () => {
  83. expect(
  84. customTimeRangeDecode(
  85. 'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
  86. ),
  87. ).toEqual({
  88. customRange: {
  89. sinceDatetime: 'now',
  90. sinceMode: 'relative',
  91. sinceGrain: 'day',
  92. sinceGrainValue: -7,
  93. untilDatetime: 'now',
  94. untilMode: 'relative',
  95. untilGrain: 'day',
  96. untilGrainValue: 7,
  97. anchorMode: 'now',
  98. anchorValue: 'now',
  99. },
  100. matchedFlag: true,
  101. });
  102. });
  103. it('5) relative : relative (date/time)', () => {
  104. expect(
  105. customTimeRangeDecode(
  106. 'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
  107. ),
  108. ).toEqual({
  109. customRange: {
  110. sinceDatetime: '2021-01-27T00:00:00',
  111. sinceMode: 'relative',
  112. sinceGrain: 'day',
  113. sinceGrainValue: -7,
  114. untilDatetime: '2021-01-27T00:00:00',
  115. untilMode: 'relative',
  116. untilGrain: 'day',
  117. untilGrainValue: 7,
  118. anchorMode: 'specific',
  119. anchorValue: '2021-01-27T00:00:00',
  120. },
  121. matchedFlag: true,
  122. });
  123. });
  124. it('6) specific : relative (now)', () => {
  125. expect(
  126. customTimeRangeDecode('now : DATEADD(DATETIME("now"), 7, day)'),
  127. ).toEqual({
  128. customRange: {
  129. sinceDatetime: 'now',
  130. sinceMode: 'now',
  131. sinceGrain: 'day',
  132. sinceGrainValue: -7,
  133. untilDatetime: 'now',
  134. untilMode: 'relative',
  135. untilGrain: 'day',
  136. untilGrainValue: 7,
  137. anchorMode: 'now',
  138. anchorValue: 'now',
  139. },
  140. matchedFlag: true,
  141. });
  142. });
  143. it('7) default', () => {
  144. const SEVEN_DAYS_AGO = new Date();
  145. const MIDNIGHT = new Date();
  146. SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
  147. MIDNIGHT.setHours(0, 0, 0, 0);
  148. expect(
  149. customTimeRangeDecode('now : DATEADD(DATETIME("TODAY"), -7, day)'),
  150. ).toEqual({
  151. customRange: {
  152. sinceDatetime: SEVEN_DAYS_AGO.setDate(
  153. SEVEN_DAYS_AGO.getDate() - 7,
  154. ).toString(),
  155. sinceMode: 'relative',
  156. sinceGrain: 'day',
  157. sinceGrainValue: -7,
  158. untilDatetime: MIDNIGHT.toString(),
  159. untilMode: 'specific',
  160. untilGrain: 'day',
  161. untilGrainValue: 7,
  162. anchorMode: 'now',
  163. anchorValue: 'now',
  164. },
  165. matchedFlag: false,
  166. });
  167. });
  168. it('8) relative : relative return default', () => {
  169. const SEVEN_DAYS_AGO = new Date();
  170. SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
  171. const MIDNIGHT = new Date();
  172. MIDNIGHT.setHours(0, 0, 0, 0);
  173. expect(
  174. customTimeRangeDecode(
  175. 'DATEADD(DATETIME("2021-01-26T00:00:00"), -55, day) : DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
  176. ),
  177. ).toEqual({
  178. customRange: {
  179. sinceDatetime: SEVEN_DAYS_AGO.setDate(
  180. SEVEN_DAYS_AGO.getDate() - 7,
  181. ).toString(),
  182. sinceMode: 'relative',
  183. sinceGrain: 'day',
  184. sinceGrainValue: -7,
  185. untilDatetime: MIDNIGHT.toString(),
  186. untilMode: 'specific',
  187. untilGrain: 'day',
  188. untilGrainValue: 7,
  189. anchorMode: 'now',
  190. anchorValue: 'now',
  191. },
  192. matchedFlag: false,
  193. });
  194. });
  195. it('9) empty string returns default', () => {
  196. const SEVEN_DAYS_AGO = new Date();
  197. SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
  198. const MIDNIGHT = new Date();
  199. MIDNIGHT.setHours(0, 0, 0, 0);
  200. expect(customTimeRangeDecode('')).toEqual({
  201. customRange: {
  202. sinceDatetime: SEVEN_DAYS_AGO.setDate(
  203. SEVEN_DAYS_AGO.getDate() - 7,
  204. ).toString(),
  205. sinceMode: 'relative',
  206. sinceGrain: 'day',
  207. sinceGrainValue: -7,
  208. untilDatetime: MIDNIGHT.toString(),
  209. untilMode: 'specific',
  210. untilGrain: 'day',
  211. untilGrainValue: 7,
  212. anchorMode: 'now',
  213. anchorValue: 'now',
  214. },
  215. matchedFlag: false,
  216. });
  217. });
  218. it('10) both undefined returns default', () => {
  219. const SEVEN_DAYS_AGO = new Date();
  220. SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
  221. const MIDNIGHT = new Date();
  222. MIDNIGHT.setHours(0, 0, 0, 0);
  223. expect(customTimeRangeDecode('undefined : undefined')).toEqual({
  224. customRange: {
  225. sinceDatetime: SEVEN_DAYS_AGO.setDate(
  226. SEVEN_DAYS_AGO.getDate() - 7,
  227. ).toString(),
  228. sinceMode: 'relative',
  229. sinceGrain: 'day',
  230. sinceGrainValue: -7,
  231. untilDatetime: MIDNIGHT.toString(),
  232. untilMode: 'specific',
  233. untilGrain: 'day',
  234. untilGrainValue: 7,
  235. anchorMode: 'now',
  236. anchorValue: 'now',
  237. },
  238. matchedFlag: false,
  239. });
  240. });
  241. it('11) 1 side undefined returns default', () => {
  242. const SEVEN_DAYS_AGO = new Date();
  243. SEVEN_DAYS_AGO.setHours(0, 0, 0, 0);
  244. const MIDNIGHT = new Date();
  245. MIDNIGHT.setHours(0, 0, 0, 0);
  246. expect(customTimeRangeDecode('undefined : now')).toEqual({
  247. customRange: {
  248. sinceDatetime: SEVEN_DAYS_AGO.setDate(
  249. SEVEN_DAYS_AGO.getDate() - 7,
  250. ).toString(),
  251. sinceMode: 'relative',
  252. sinceGrain: 'day',
  253. sinceGrainValue: -7,
  254. untilDatetime: MIDNIGHT.toString(),
  255. untilMode: 'specific',
  256. untilGrain: 'day',
  257. untilGrainValue: 7,
  258. anchorMode: 'now',
  259. anchorValue: 'now',
  260. },
  261. matchedFlag: false,
  262. });
  263. });
  264. });