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

Add unit tests for utils #537

Merged
merged 14 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 14 additions & 12 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {expect} from '@jest/globals';

import * as YAML from 'js-yaml';

export const readFile = async (_filePath: string, _format: string) => {
export const readFile = async (filePath: string, _format: string) => {

Check warning on line 3 in src/__mocks__/fs/index.ts

View workflow job for this annotation

GitHub Actions / build

'_format' is defined but never used
/** mock for util/json */
if (filePath.includes('json-reject')) {
return Promise.reject(new Error('rejected'));
}
if (filePath.includes('json')) {
return JSON.stringify(filePath);
}

/** mock for util/yaml */
return `
name: gsf-demo
description:
Expand All @@ -14,12 +20,8 @@
category: cloud
initialize:
models:
- name: boavizta-cpu
kind: builtin
config:
allocation: LINEAR
verbose: true
graph:
boavizta-cpu:
tree:
children:
front-end:
pipeline:
Expand All @@ -29,15 +31,15 @@
core-units: 24
processor: Intel® Core™ i7-1185G7
inputs:
- timestamp: 2023-07-06T00:00 # [KEYWORD] [NO-SUBFIELDS] time when measurement occurred
- timestamp: 2023-07-06T00:00
duration: 3600 # Secs
cpu/utilization: 18.392
- timestamp: 2023-08-06T00:00 # [KEYWORD] [NO-SUBFIELDS] time when measurement occurred
- timestamp: 2023-08-06T00:00
duration: 3600 # Secs
cpu/utilization: 16`;
};

export const mkdir = (dirPath: string, _object: any) => dirPath;

Check warning on line 42 in src/__mocks__/fs/index.ts

View workflow job for this annotation

GitHub Actions / build

'_object' is defined but never used

export const writeFile = async (pathToFile: string, content: string) => {
if (pathToFile === 'reject') {
Expand Down
52 changes: 26 additions & 26 deletions src/__tests__/unit/builtins/time-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 10,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand Down Expand Up @@ -80,12 +80,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 10,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 10,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
]);
} catch (error) {
Expand Down Expand Up @@ -163,12 +163,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 15,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand All @@ -193,12 +193,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 15,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand All @@ -223,12 +223,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 15,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand All @@ -254,25 +254,25 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 1,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: new Date('2023-12-12T00:00:01.000Z'),
duration: 1,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
]);

const expectedResult = [
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 1,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:01.000Z',
duration: 1,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
];

Expand Down Expand Up @@ -367,26 +367,26 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 3,
'total-resources': 10,
'resources-total': 10,
},
{
timestamp: '2023-12-12T00:00:05.000Z',
duration: 3,
'total-resources': 10,
'resources-total': 10,
},
]);

/**In each 5 second interval, 60% of the time cpu/utilizaition = 10, 40% of the time it is 0, so cpu/utilizaition in the averaged result be 6 */
/**In each 5 second interval, 60% of the time cpu/utilization = 10, 40% of the time it is 0, so cpu/utilization in the averaged result be 6 */
const expectedResult = [
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 5,
'total-resources': 10,
'resources-total': 10,
},
{
timestamp: '2023-12-12T00:00:05.000Z',
duration: 5,
'total-resources': 10,
'resources-total': 10,
},
];

Expand All @@ -408,12 +408,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:02.000Z',
duration: 15,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand All @@ -438,12 +438,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 10,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand All @@ -468,12 +468,12 @@ describe('execute(): ', () => {
{
timestamp: '2023-12-12T00:00:02.000Z',
duration: 10,
'cpu/utilizaition': 10,
'cpu/utilization': 10,
},
{
timestamp: '2023-12-12T00:00:08.000Z',
duration: 1,
'cpu/utilizaition': 20,
'cpu/utilization': 20,
},
]);
} catch (error) {
Expand Down
38 changes: 38 additions & 0 deletions src/__tests__/unit/util/aggregation-helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {aggregateInputsIntoOne} from '../../../util/aggregation-helper';
import {ERRORS} from '../../../util/errors';

import {PluginParams} from '../../../types/interface';

const {InvalidAggregationParams} = ERRORS;

describe('util/aggregation-helper: ', () => {
describe('aggregateInputsIntoOne(): ', () => {
it('throws error if aggregation method is none.', () => {
const inputs: PluginParams[] = [];
const metrics: string[] = ['cpu/number-cores'];
const isTemporal = false;

expect.assertions(1);

try {
aggregateInputsIntoOne(inputs, metrics, isTemporal);
} catch (error) {
expect(error).toBeInstanceOf(InvalidAggregationParams);
}
});

it('throws error if aggregation criteria is not found in input.', () => {
const inputs: PluginParams[] = [{timestamp: '', duration: 10}];
const metrics: string[] = ['cpu/utilization'];
const isTemporal = false;

expect.assertions(1);

try {
aggregateInputsIntoOne(inputs, metrics, isTemporal);
} catch (error) {
expect(error).toBeInstanceOf(InvalidAggregationParams);
}
});
});
});
13 changes: 13 additions & 0 deletions src/__tests__/unit/util/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {ERRORS} from '../../../util/errors';

describe('util/errors: ', () => {
describe('ERRORS: ', () => {
it('checks for properties.', () => {
const errors = Object.values(ERRORS);

errors.forEach(error => {
expect(error).toBeInstanceOf(Function);
});
});
});
});
27 changes: 27 additions & 0 deletions src/__tests__/unit/util/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
jest.mock('fs/promises', () => require('../../../__mocks__/fs'));

import {readAndParseJson} from '../../../util/json';

describe('util/json: ', () => {
describe('readAndParseJson(): ', () => {
it('returns file content from path.', async () => {
const path = 'mock/path/json';
expect.assertions(1);

const response = await readAndParseJson<typeof path>(path);
expect(response).toEqual(path);
});

it('throws error if path does not exist.', async () => {
const path = 'mock/path/json-reject';
expect.assertions(1);

try {
const response = await readAndParseJson<typeof path>(path);
console.log(response);
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});
});
31 changes: 31 additions & 0 deletions src/__tests__/unit/util/log-memoize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {LeveledLogMethod} from 'winston';
import {memoizedLog} from '../../../util/log-memoize';

describe('util/log-memoize: ', () => {
describe('momoizedLog(): ', () => {
it('should call logger.', () => {
const message = 'mock-message';
const logger = jest.fn(
(message: string) => message
) as unknown as LeveledLogMethod;

memoizedLog(logger, message);

expect(logger).toHaveBeenCalledTimes(1);

(logger as jest.Mock).mockReset();
});

it('wont call logger since its cached.', () => {
const message = 'mock-message';
const logger = jest.fn(
(message: string) => message
) as unknown as LeveledLogMethod;

memoizedLog(logger, message);
memoizedLog(logger, message);

expect(logger).toHaveBeenCalledTimes(0);
});
});
});
11 changes: 11 additions & 0 deletions src/__tests__/unit/util/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Logger} from 'winston';

import {logger} from '../../../util/logger';

describe('util/logger: ', () => {
describe('logger(): ', () => {
it('is instance of winston logger.', () => {
expect(logger).toBeInstanceOf(Logger);
});
});
});
Loading
Loading