Skip to content

Commit

Permalink
add and fix unit tests for padAndSortTimeSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
MGJamJam committed Jul 10, 2024
1 parent d2e46b1 commit 39a1fbc
Showing 1 changed file with 102 additions and 32 deletions.
134 changes: 102 additions & 32 deletions src/utils/dataUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,43 @@ describe('padAndSortTimeSeries', () => {
it('should return sorted and padded data for simple time series data for MINUTE interval', () => {
//arrange
const data = [
[1712919540000, 1], //Friday, 12 April 2024 10:59:00
[1712919600000, 2], //Friday, 12 April 2024 11:00:00
[1712919720000, 5], //Friday, 12 April 2024 11:02:00
];

const startTimestamp = 1712919560000; //Friday, 12 April 2024 10:59:20
const startTimestamp = 1712919540000; //Friday, 12 April 2024 10:59:00
const endTimestamp = 1712919780000; //Friday, 12 April 2024 11:03:00

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

//assert
expect(result).toEqual([
[1712919600000, 2],
[1712919660000, 0],
[1712919720000, 5],
[1712919780000, 0],
[1712919540000, 0], //Friday, 12 April 2024 10:59:00
[1712919600000, 2], //Friday, 12 April 2024 11:00:00
[1712919660000, 0], //Friday, 12 April 2024 11:01:00
[1712919720000, 5], //Friday, 12 April 2024 11:02:00
[1712919780000, 0], //Friday, 12 April 2024 11:03:00
]);
});

it('should return sorted and padded data for simple time series data for HOUR Interval', () => {
//arrange
const data = [
[1712916000000, 1], //Friday, 12 April 2024 10:00:00
[1712919600000, 7], //Friday, 12 April 2024 11:00:00
[1712930400000, 5], //Friday, 12 April 2024 14:00:00
[1712934000000, 2], //Friday, 12 April 2024 15:00:00
];

const startTimestamp = 1712917444000; //Friday, 12 April 2024 10:24:04
const startTimestamp = 1712916000000; //Friday, 12 April 2024 10:00:00
const endTimestamp = 1712935444000; //Friday, 12 April 2024 15:24:04

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

//assert
expect(result).toEqual([
[1712916000000, 0], //Friday, 12 April 2024 10:00:00
[1712919600000, 7], //Friday, 12 April 2024 11:00:00
[1712923200000, 0], //Friday, 12 April 2024 12:00:00
[1712926800000, 0], //Friday, 12 April 2024 13:00:00
Expand All @@ -121,19 +121,19 @@ 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 startTimestamp = 1712917800000; //Friday, 12 April 2024 10:30:00
const endTimestamp = 1713351560000; //Wednesday, 17 April 2024 10:59:20

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

//assert
expect(result).toEqual([
[1712917800000, 0], //Friday, 12 April 2024 10:30:00
[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
Expand All @@ -142,70 +142,93 @@ describe('padAndSortTimeSeries', () => {
]);
});

it('should return sorted and padded data for simple time series data for DAY interval', () => {
it('should return sorted and padded data for simple time series data for MONTH interval with start Day being the last of a month', () => {
//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
[1706740200000, 1], //Wednesday, 31 January 2024 22:30:00
[1711924200000, 2], //Sunday, 31 March 2024 22:30:00
[1714516200000, 5], //Tuesday, 30 April 2024 22:30:00
];

const startTimestamp = 1712919560000; //Friday, 12 April 2024 10:59:20
const endTimestamp = 1713351560000; //Wednesday, 17 April 2024 10:59:20
const startTimestamp = 1706740200000; //Wednesday, 31 January 2024 22:30:00
const endTimestamp = 1718454244000; //Saturday, 15 June 2024 12:24:04

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

//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
[1706740200000, 1], //Wednesday, 31 January 2024 22:30:00
[1709245800000, 0], //Thursday, 29 February 2024 22:30:00
[1711924200000, 2], //Sunday, 31 March 2024 22:30:00
[1714516200000, 5], //Tuesday, 30 April 2024 22:30:00
[1717194600000, 0], // Friday, 31 May 2024 22:30:00
]);
});

it('should return sorted and padded data for simple time series data for MONTH interval with start Day being the first of a month', () => {
//arrange
const data = [
[1712010600000, 2], //Monday, 1 April 2024 22:30:00
[1714602600000, 5], //Wednesday, 1 May 2024 22:30:00
];

const startTimestamp = 1706826600000; //Thursday, 1 February 2024 22:30:00
const endTimestamp = 1718454244000; //Saturday, 15 June 2024 12:24:04

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

//assert
expect(result).toEqual([
[1706826600000, 0], //Thursday, 1 February 2024 22:30:00
[1709332200000, 0], //Friday, 1 March 2024 22:30:00
[1712010600000, 2], //Monday, 1 April 2024 22:30:00
[1714602600000, 5], //Wednesday, 1 May 2024 22:30:00
[1717281000000, 0], //Saturday, 1 June 2024 22:30:00
]);
});

it('should return sorted and padded data for grouped time series data for MINUTE interval', () => {
//arrange
const data = [
[1712919540000, 'BROWSER', 'DEVICE_TYPE', 1], //Friday, 12 April 2024 10:59:00
[1712919600000, 'BROWSER', 'DEVICE_TYPE', 2], //Friday, 12 April 2024 11:00:00
[1712919720000, 'BROWSER', 'DEVICE_TYPE', 5], //Friday, 12 April 2024 11:02:00
];

const startTimestamp = 1712919560000; //Friday, 12 April 2024 10:59:20
const startTimestamp = 1712919540000; //Friday, 12 April 2024 10:59:00
const endTimestamp = 1712919780000; //Friday, 12 April 2024 11:03:00

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

//assert
expect(result).toEqual([
[1712919600000, 'BROWSER', 'DEVICE_TYPE', 2],
[1712919660000, 'BROWSER', 'DEVICE_TYPE', 0],
[1712919720000, 'BROWSER', 'DEVICE_TYPE', 5],
[1712919780000, 'BROWSER', 'DEVICE_TYPE', 0],
[1712919540000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 10:59:00
[1712919600000, 'BROWSER', 'DEVICE_TYPE', 2], //Friday, 12 April 2024 11:00:00
[1712919660000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 11:01:00
[1712919720000, 'BROWSER', 'DEVICE_TYPE', 5], //Friday, 12 April 2024 11:02:00
[1712919780000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 11:03:00
]);
});

it('should return sorted and padded data for grouped time series data for HOUR interval', () => {
//arrange
const data = [
[1712916000000, 'BROWSER', 'DEVICE_TYPE', 1], //Friday, 12 April 2024 10:00:00
[1712919600000, 'BROWSER', 'DEVICE_TYPE', 7], //Friday, 12 April 2024 11:00:00
[1712930400000, 'BROWSER', 'DEVICE_TYPE', 5], //Friday, 12 April 2024 14:00:00
[1712934000000, 'BROWSER', 'DEVICE_TYPE', 2], //Friday, 12 April 2024 15:00:00
];

const startTimestamp = 1712917444000; //Friday, 12 April 2024 10:24:04
const startTimestamp = 1712916000000; //Friday, 12 April 2024 10:00:00
const endTimestamp = 1712935444000; //Friday, 12 April 2024 15:24:04

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

//assert
expect(result).toEqual([
[1712916000000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 10:00:00
[1712919600000, 'BROWSER', 'DEVICE_TYPE', 7], //Friday, 12 April 2024 11:00:00
[1712923200000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 12:00:00
[1712926800000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 13:00:00
Expand All @@ -217,19 +240,19 @@ describe('padAndSortTimeSeries', () => {
it('should return sorted and padded data for grouped time series data for DAY interval', () => {
//arrange
const data = [
[1712917800000, 'BROWSER', 'DEVICE_TYPE', 1], //Friday, 12 April 2024 10:30:00
[1713004200000, 'BROWSER', 'DEVICE_TYPE', 2], //Saturday, 13 April 2024 10:30:00
[1713263400000, 'BROWSER', 'DEVICE_TYPE', 5], //Tuesday, 16 April 2024 10:30:00
];

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

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

//assert
expect(result).toEqual([
[1712917800000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 12 April 2024 10:30:00
[1713004200000, 'BROWSER', 'DEVICE_TYPE', 2], //Saturday, 13 April 2024 10:30:00
[1713090600000, 'BROWSER', 'DEVICE_TYPE', 0], //Sunday, 14 April 2024 10:30:00
[1713177000000, 'BROWSER', 'DEVICE_TYPE', 0], //Monday, 15 April 2024 10:30:00
Expand All @@ -238,6 +261,53 @@ describe('padAndSortTimeSeries', () => {
]);
});

it('should return sorted and padded data for grouped time series data for MONTH interval with start Day being the last of a month', () => {
//arrange
const data = [
[1706740200000, 'BROWSER', 'DEVICE_TYPE', 1], //Wednesday, 31 January 2024 22:30:00
[1711924200000, 'BROWSER', 'DEVICE_TYPE', 2], //Sunday, 31 March 2024 22:30:00
[1714516200000, 'BROWSER', 'DEVICE_TYPE', 5], //Tuesday, 30 April 2024 22:30:00
];

const startTimestamp = 1706740200000; //Wednesday, 31 January 2024 22:30:00
const endTimestamp = 1718454244000; //Saturday, 15 June 2024 12:24:04

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

//assert
expect(result).toEqual([
[1706740200000, 'BROWSER', 'DEVICE_TYPE', 1], //Wednesday, 31 January 2024 22:30:00
[1709245800000, 'BROWSER', 'DEVICE_TYPE', 0], //Thursday, 29 February 2024 22:30:00
[1711924200000, 'BROWSER', 'DEVICE_TYPE', 2], //Sunday, 31 March 2024 22:30:00
[1714516200000, 'BROWSER', 'DEVICE_TYPE', 5], //Tuesday, 30 April 2024 22:30:00
[1717194600000, 'BROWSER', 'DEVICE_TYPE', 0], // Friday, 31 May 2024 22:30:00
]);
});

it('should return sorted and padded data for grouped time series data for MONTH interval with start Day being the first of a month', () => {
//arrange
const data = [
[1712010600000, 'BROWSER', 'DEVICE_TYPE', 2], //Monday, 1 April 2024 22:30:00
[1714602600000, 'BROWSER', 'DEVICE_TYPE', 5], //Wednesday, 1 May 2024 22:30:00
];

const startTimestamp = 1706826600000; //Thursday, 1 February 2024 22:30:00
const endTimestamp = 1718454244000; //Saturday, 15 June 2024 12:24:04

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

//assert
expect(result).toEqual([
[1706826600000, 'BROWSER', 'DEVICE_TYPE', 0], //Thursday, 1 February 2024 22:30:00
[1709332200000, 'BROWSER', 'DEVICE_TYPE', 0], //Friday, 1 March 2024 22:30:00
[1712010600000, 'BROWSER', 'DEVICE_TYPE', 2], //Monday, 1 April 2024 22:30:00
[1714602600000, 'BROWSER', 'DEVICE_TYPE', 5], //Wednesday, 1 May 2024 22:30:00
[1717281000000, 'BROWSER', 'DEVICE_TYPE', 0], //Saturday, 1 June 2024 22:30:00
]);
});

it('should throw error when interval is not valid', () => {
//arrange
const data = [[0, 0]];
Expand Down

0 comments on commit 39a1fbc

Please sign in to comment.