Skip to content

Commit

Permalink
add unit tests for calculateTimeSeriesStartTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
MGJamJam committed Jul 10, 2024
1 parent 247c677 commit 5687221
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
87 changes: 87 additions & 0 deletions src/utils/dataUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
import {
calculateTimeSeriesStartTimestamp,
padAndSortTimeSeries,
transformGroupedTimeSeriesData,
transformSimpleTimeSeries,
transformTableData,
} from './dataUtils';
import { FieldType } from '@grafana/data';

describe('calculateTimeSeriesStartTimestamp', () => {
it('should return correct timestamp for MINUTE interval', () => {
//arrange
const referenceDataTimestamp = 1720598700000; // Wednesday, 10 July 2024 08:05:00
const intervalStartTimestamp = 1720598508400; // Wednesday, 10 July 2024 08:01:48.400

//act
const result = calculateTimeSeriesStartTimestamp(referenceDataTimestamp, intervalStartTimestamp, 'MINUTE');

//assert
expect(result).toEqual(1720598460000); //Wednesday, 10 July 2024 08:01:00
});

it('should return correct timestamp for HOUR interval', () => {
//arrange
const referenceDataTimestamp = 1720598400000; // Wednesday, 10 July 2024 08:00:00
const intervalStartTimestamp = 1720591381300; // Wednesday, 10 July 2024 03:03:01.300

//act
const result = calculateTimeSeriesStartTimestamp(referenceDataTimestamp, intervalStartTimestamp, 'HOUR');

//assert
expect(result).toEqual(1720591200000); // Wednesday, 10 July 2024 06:00:00
});

it('should return correct timestamp for DAY interval', () => {
//arrange
const referenceDataTimestamp = 1720650600000; // Wednesday, 10 July 2024 22:30:00
const intervalStartTimestamp = 1720442041020; // Monday, 8 July 2024 12:34:01.020

//act
const result = calculateTimeSeriesStartTimestamp(referenceDataTimestamp, intervalStartTimestamp, 'DAY');

//assert
expect(result).toEqual(1720477800000); // Monday, 8 July 2024 22:30:00
});

it('should return correct timestamp for MONTH interval with dataTimestamp being the first day of a month', () => {
//arrange
const referenceDataTimestamp = 1719873000000; // Monday, 1 July 2024 22:30:00
const intervalStartTimestamp = 1714915205040; // Sunday, 5 May 2024 13:20:05.040

//act
const result = calculateTimeSeriesStartTimestamp(referenceDataTimestamp, intervalStartTimestamp, 'MONTH');

//assert
expect(result).toEqual(1714602600000); // Wednesday, 1 May 2024 22:30:00
});

it('should return correct timestamp for MONTH interval with dataTimestamp being the last day of a month', () => {
//arrange
const referenceDataTimestamp = 1719786600000; // Sunday, 30 June 2024 22:30:00
const intervalStartTimestamp = 1711374785001; // Monday, 25 March 2024 13:53:05.001

//act
const result = calculateTimeSeriesStartTimestamp(referenceDataTimestamp, intervalStartTimestamp, 'MONTH');

//assert
expect(result).toEqual(1709245800000); // Thursday, 29 February 2024 22:30:00
});
});

describe('padAndSortTimeSeries', () => {
it('should return sorted and padded data for simple time series data for MINUTE interval', () => {
//arrange
Expand Down Expand Up @@ -79,6 +142,30 @@ describe('padAndSortTimeSeries', () => {
]);
});

it('should return sorted and padded data for simple time series data for DAY interval', () => {
//arrange
const data = [
[1712917800000, 1], //Friday, 12 April 2024 10:30:00
[1713004200000, 2], //Saturday, 13 April 2024 10:30:00
[1713263400000, 5], //Tuesday, 16 April 2024 10:30:00
];

const startTimestamp = 1712919560000; //Friday, 12 April 2024 10:59:20
const endTimestamp = 1713351560000; //Wednesday, 17 April 2024 10:59:20

//act
const result = padAndSortTimeSeries(data, startTimestamp, endTimestamp, 'DAY');

//assert
expect(result).toEqual([
[1713004200000, 2], //Saturday, 13 April 2024 10:30:00
[1713090600000, 0], //Sunday, 14 April 2024 10:30:00
[1713177000000, 0], //Monday, 15 April 2024 10:30:00
[1713263400000, 5], //Tuesday, 16 April 2024 10:30:00
[1713349800000, 0], //Wednesday, 17 April 2024 10:30:00
]);
});

it('should return sorted and padded data for grouped time series data for MINUTE interval', () => {
//arrange
const data = [
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function calculateTimeSeriesStartTimestamp(
case 'MONTH':
referenceDataDate.getDate() === 1
? intervalStartDate.setDate(referenceDataDate.getDate())
: intervalStartDate.setDate(0);
: intervalStartDate.setDate(0); // sets the date to the last day of the previous month
return intervalStartDate.setHours(referenceDataDate.getHours(), referenceDataDate.getMinutes(), 0, 0);
}
}
Expand Down

0 comments on commit 5687221

Please sign in to comment.