Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time-sync test coverage #564

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/__tests__/unit/builtins/time-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ const {InputValidationError} = ERRORS;

const {INVALID_OBSERVATION_OVERLAP, INVALID_TIME_NORMALIZATION} = STRINGS;

jest.mock('luxon', () => {
const originalModule = jest.requireActual('luxon');
return {
...originalModule,
Interval: {
...originalModule.Interval,
fromDateTimes: jest.fn((start, end) => ({
start,
end,
splitBy: jest.fn(() => {
const intervals = [];
let current = start;

while (current < end) {
intervals.push({
start: process.env.MOCK_INTERVAL === 'true' ? null : current,
end: current.plus({seconds: 1}),
});

current = current.plus({seconds: 1});
}

return intervals;
}),
})),
},
};
});

describe('builtins/time-sync:', () => {
describe('time-sync: ', () => {
const basicConfig = {
Expand Down Expand Up @@ -234,6 +263,71 @@ describe('execute(): ', () => {
}
});

it('throws error if `timestamp` is missing.', async () => {
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
'end-time': '2023-12-12T00:01:00.000Z',
interval: 5,
'allow-padding': true,
};

const timeModel = TimeSync(basicConfig);

try {
await timeModel.execute([
{
duration: 15,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilization': 20,
},
]);
} catch (error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error).toStrictEqual(
new InputValidationError(
'"timestamp" parameter is invalid input. Error code: invalid_union.'
)
);
}
});

it('throws error if the seconds `timestamp` is above 60.', async () => {
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
'end-time': '2023-12-12T00:01:00.000Z',
interval: 5,
'allow-padding': true,
};

const timeModel = TimeSync(basicConfig);

try {
await timeModel.execute([
{
timestamp: '2023-12-12T00:00:90.000Z',
duration: 15,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilization': 20,
},
]);
} catch (error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error).toStrictEqual(
new InputValidationError(
'"timestamp" parameter is invalid input. Error code: invalid_union.'
)
);
}
});

it('throws an error if the `timestamp` is not valid date.', async () => {
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
Expand Down Expand Up @@ -489,7 +583,37 @@ describe('execute(): ', () => {
expect(result).toStrictEqual(expectedResult);
});

it('throws an error when `start-time` is wrong.', async () => {
process.env.MOCK_INTERVAL = 'true';
const basicConfig = {
'start-time': '2023-12-12T00:00:90.000Z',
'end-time': '2023-12-12T00:01:09.000Z',
interval: 5,
'allow-padding': true,
};

const timeModel = TimeSync(basicConfig);

try {
await timeModel.execute([
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 30,
'cpu/utilization': 20,
},
]);
} catch (error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error).toStrictEqual(
new InputValidationError(
'"timestamp" parameter is invalid datetime in input[1]. Error code: invalid_string.'
)
);
}
});

it('returns a result when the first timestamp in the input has time padding.', async () => {
process.env.MOCK_INTERVAL = 'false';
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
'end-time': '2023-12-12T00:00:09.000Z',
Expand Down Expand Up @@ -619,6 +743,7 @@ describe('execute(): ', () => {
});

it('checks that timestamps in return object are ISO 8061 and timezone UTC.', async () => {
process.env.MOCK_INTERVAL = 'false';
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
'end-time': '2023-12-12T00:00:03.000Z',
Expand Down
Loading