This is an automated email from the ASF dual-hosted git repository. arivero pushed a commit to branch table-time-comparison-offset in repository https://gitbox.apache.org/repos/asf/superset.git
commit cf4da81ae3b0e935e8592530138761de0e42e8e7 Author: Antonio Rivero <[email protected]> AuthorDate: Wed Apr 24 01:23:37 2024 +0200 Table with Time Comparison: - Handle coverage for time range methods --- .../src/time-comparison/fetchTimeRange.ts | 6 +- .../test/time-comparison/fetchTimeRange.test.ts | 76 ++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/src/time-comparison/fetchTimeRange.ts b/superset-frontend/packages/superset-ui-core/src/time-comparison/fetchTimeRange.ts index 8de50d501b..2b344ae0d5 100644 --- a/superset-frontend/packages/superset-ui-core/src/time-comparison/fetchTimeRange.ts +++ b/superset-frontend/packages/superset-ui-core/src/time-comparison/fetchTimeRange.ts @@ -63,12 +63,12 @@ export const fetchTimeRange = async ( ) => { let query; let endpoint; - if (!isEmpty(shifts)) { - const timeRanges = shifts?.map(shift => ({ + if (shifts && !isEmpty(shifts)) { + const timeRanges = shifts.map(shift => ({ timeRange, shift, })); - query = rison.encode_uri([{ timeRange }, ...(timeRanges || [])]); + query = rison.encode_uri([{ timeRange }, ...timeRanges]); endpoint = `/api/v1/time_range/?q=${query}`; } else { query = rison.encode_uri(timeRange); diff --git a/superset-frontend/packages/superset-ui-core/test/time-comparison/fetchTimeRange.test.ts b/superset-frontend/packages/superset-ui-core/test/time-comparison/fetchTimeRange.test.ts index e07fa8617f..0fb6890c1c 100644 --- a/superset-frontend/packages/superset-ui-core/test/time-comparison/fetchTimeRange.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/time-comparison/fetchTimeRange.test.ts @@ -22,6 +22,7 @@ import { fetchTimeRange } from '@superset-ui/core'; import { buildTimeRangeString, formatTimeRange, + formatTimeRangeComparison, } from '../../src/time-comparison/fetchTimeRange'; afterEach(fetchMock.restore); @@ -51,6 +52,7 @@ it('generates a readable time range', () => { expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe( '-∞ ≤ col < 2020-07-30', ); + expect(formatTimeRange('')).toBe(''); }); it('returns a formatted time range from response', async () => { @@ -116,3 +118,77 @@ it('returns a formatted error message from response', async () => { error: 'Network error', }); }); + +it('generates a readable time range comparison', () => { + expect( + formatTimeRangeComparison( + '2021-04-13T00:00:00 : 2021-04-14T00:00:00', + '2021-04-15T00:00:00 : 2021-04-16T00:00:00', + 'col', + ) + .replace(/\s+/g, ' ') + .trim(), + ).toBe('col: 2021-04-13 to 2021-04-14 vs 2021-04-15 to 2021-04-16'); +}); + +it('uses default column placeholder when none is provided', () => { + const initialTimeRange = '2021-04-13T00:00:00 : 2021-04-14T00:00:00'; + const shiftedTimeRange = '2021-04-15T00:00:00 : 2021-04-16T00:00:00'; + const expectedOutput = + 'col: 2021-04-13 to 2021-04-14 vs 2021-04-15 to 2021-04-16'; + + // Call the function without the third parameter + const actualOutput = formatTimeRangeComparison( + initialTimeRange, + shiftedTimeRange, + ) + .replace(/\s+/g, ' ') + .trim(); + + expect(actualOutput).toBe(expectedOutput); +}); + +it('returns a comparison of formatted time ranges with shifts', async () => { + // Correcting the fetchMock to properly mock the API request and response + fetchMock.get('glob:*/api/v1/time_range/?q=*', { + body: { + result: [ + { since: '2022-01-01T00:00:00', until: '2022-01-02T00:00:00' }, + { since: '2022-01-03T00:00:00', until: '2022-01-04T00:00:00' }, + ], + }, + headers: { 'Content-Type': 'application/json' }, + }); + + const shifts = ['1 day', '2 days']; // Define shifts if your implementation requires them + const timeRange = await fetchTimeRange( + '2022-01-01T00:00:00 : 2022-01-02T00:00:00', + 'col', + shifts, + ); + + // Since `fetchTimeRange` might be returning multiple comparisons, ensure the test checks this correctly. + // Assuming `fetchTimeRange` returns an object with an array in `value`, representing each comparison + expect(timeRange).toEqual({ + value: [ + `col: 2022-01-01 to 2022-01-02 vs + 2022-01-03 to 2022-01-04`, + ], + }); +}); + +it('returns a formatted error message from response with shifts', async () => { + fetchMock.getOnce('glob:*/api/v1/time_range/?q=*', { + throws: new Response(JSON.stringify({ message: 'Network error' })), + }); + + const shifts = ['1 day', '2 days']; // Define shifts if your implementation requires them + const timeRange = await fetchTimeRange( + '2022-01-01T00:00:00 : 2022-01-02T00:00:00', + 'col', + shifts, + ); + expect(timeRange).toEqual({ + error: 'Network error', + }); +});
