fetchTimeRange.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 fetchMock from 'fetch-mock';
  20. import { fetchTimeRange } from '@superset-ui/core';
  21. import {
  22. buildTimeRangeString,
  23. formatTimeRange,
  24. formatTimeRangeComparison,
  25. } from '../../src/time-comparison/fetchTimeRange';
  26. afterEach(() => fetchMock.restore());
  27. test('generates proper time range string', () => {
  28. expect(
  29. buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'),
  30. ).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00');
  31. expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe(
  32. ' : 2020-07-30T00:00:00',
  33. );
  34. expect(buildTimeRangeString('', '')).toBe(' : ');
  35. });
  36. test('generates a readable time range', () => {
  37. expect(formatTimeRange('Last 7 days')).toBe('Last 7 days');
  38. expect(formatTimeRange('No filter')).toBe('No filter');
  39. expect(formatTimeRange('Yesterday : Tomorrow')).toBe(
  40. 'Yesterday ≤ col < Tomorrow',
  41. );
  42. expect(formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00')).toBe(
  43. '2010-07-30 ≤ col < 2020-07-30',
  44. );
  45. expect(formatTimeRange('2010-07-30T01:00:00 : ')).toBe(
  46. '2010-07-30T01:00:00 ≤ col < ∞',
  47. );
  48. expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe(
  49. '-∞ ≤ col < 2020-07-30',
  50. );
  51. expect(formatTimeRange('')).toBe('');
  52. });
  53. test('returns a formatted time range from response', async () => {
  54. fetchMock.get('glob:*/api/v1/time_range/?q=%27Last+day%27', {
  55. result: [
  56. {
  57. since: '2021-04-13T00:00:00',
  58. until: '2021-04-14T00:00:00',
  59. timeRange: 'Last day',
  60. },
  61. ],
  62. });
  63. const timeRange = await fetchTimeRange('Last day', 'temporal_col');
  64. expect(timeRange).toEqual({
  65. value: '2021-04-13 ≤ temporal_col < 2021-04-14',
  66. });
  67. });
  68. test('returns a formatted time range from empty response', async () => {
  69. fetchMock.get('glob:*/api/v1/time_range/?q=%27Last+day%27', {
  70. result: [],
  71. });
  72. const timeRange = await fetchTimeRange('Last day');
  73. expect(timeRange).toEqual({
  74. value: '-∞ ≤ col < ∞',
  75. });
  76. });
  77. test('returns a formatted error message from response', async () => {
  78. fetchMock.get('glob:*/api/v1/time_range/?q=%27Last+day%27', {
  79. throws: new Response(JSON.stringify({ message: 'Network error' })),
  80. });
  81. let timeRange = await fetchTimeRange('Last day');
  82. expect(timeRange).toEqual({
  83. error: 'Network error',
  84. });
  85. fetchMock.get(
  86. 'glob:*/api/v1/time_range/?q=%27Last+day%27',
  87. {
  88. throws: new Error('Internal Server Error'),
  89. },
  90. { overwriteRoutes: true },
  91. );
  92. timeRange = await fetchTimeRange('Last day');
  93. expect(timeRange).toEqual({
  94. error: 'Internal Server Error',
  95. });
  96. fetchMock.get(
  97. 'glob:*/api/v1/time_range/?q=%27Last+day%27',
  98. {
  99. throws: new Response(JSON.stringify({ statusText: 'Network error' }), {
  100. statusText: 'Network error',
  101. }),
  102. },
  103. { overwriteRoutes: true },
  104. );
  105. timeRange = await fetchTimeRange('Last day');
  106. expect(timeRange).toEqual({
  107. error: 'Network error',
  108. });
  109. });
  110. test('fetchTimeRange with shift', async () => {
  111. fetchMock.getOnce(
  112. 'glob:*/api/v1/time_range/?q=!((timeRange:%27Last+day%27),(shift%3A%27last%20month%27%2CtimeRange%3A%27Last%20day%27))',
  113. {
  114. result: [
  115. {
  116. since: '2021-04-13T00:00:00',
  117. until: '2021-04-14T00:00:00',
  118. timeRange: 'Last day',
  119. shift: null,
  120. },
  121. {
  122. since: '2021-03-13T00:00:00',
  123. until: '2021-03-14T00:00:00',
  124. timeRange: 'Last day',
  125. shift: 'last month',
  126. },
  127. ],
  128. },
  129. );
  130. const timeRange = await fetchTimeRange('Last day', 'temporal_col', [
  131. 'last month',
  132. ]);
  133. expect(timeRange).toEqual({
  134. value: [
  135. 'temporal_col: 2021-04-13 to 2021-04-14 vs\n 2021-03-13 to 2021-03-14',
  136. ],
  137. });
  138. });
  139. test('formatTimeRangeComparison', () => {
  140. expect(
  141. formatTimeRangeComparison(
  142. '2021-04-13T00:00:00 : 2021-04-14T00:00:00',
  143. '2021-03-13T00:00:00 : 2021-03-14T00:00:00',
  144. ),
  145. ).toEqual('col: 2021-04-13 to 2021-04-14 vs\n 2021-03-13 to 2021-03-14');
  146. expect(
  147. formatTimeRangeComparison(
  148. '2021-04-13T00:00:00 : 2021-04-14T00:00:00',
  149. '2021-03-13T00:00:00 : 2021-03-14T00:00:00',
  150. 'col_name',
  151. ),
  152. ).toEqual(
  153. 'col_name: 2021-04-13 to 2021-04-14 vs\n 2021-03-13 to 2021-03-14',
  154. );
  155. });