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

Parameterize unit tests #552

Merged
merged 6 commits into from
Mar 21, 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
100 changes: 100 additions & 0 deletions src/__tests__/unit/lib/parameterize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const mockLog = jest.fn();

jest.mock('../../../util/log-memoize', () => ({
memoizedLog: mockLog,
}));
jest.mock('../../../util/logger', () => ({
logger: {
warn: mockLog,
},
}));

import {PARAMETERS} from '../../../config';
import {parameterize} from '../../../lib/parameterize';

import {STRINGS} from '../../../config';

import {ManifestParameter} from '../../../types/manifest';

const {REJECTING_OVERRIDE} = STRINGS;

describe('lib/parameterize: ', () => {
afterEach(() => {
mockLog.mockReset();
});

describe('getAggregationMethod(): ', () => {
it('returns method for average aggregation method metric.', () => {
const metric = 'cpu/utilization';
const method = parameterize.getAggregationMethod(metric);

const expectedMethod = 'avg';

expect(method).toEqual(expectedMethod);
});

it('returns method for unknown aggregation method metric.', () => {
const metric = 'mock/metric';
const method = parameterize.getAggregationMethod(metric);

const expectedMethod = 'sum';

expect(method).toEqual(expectedMethod);
expect(mockLog).toHaveBeenCalledTimes(1);
});
});

describe('combine(): ', () => {
it('checks if return type is undefined.', () => {
const params = {};
const response = parameterize.combine(null, params);

expect(response).toBeUndefined();
});

it('checks if uninitialized custom param is requested, then returns fallback `sum` method.', () => {
const name = 'mock-name';
const method = parameterize.getAggregationMethod(name);

const expectedMethodName = 'sum';
expect(method).toEqual(expectedMethodName);
});

it('checks if custom params are inserted successfully.', () => {
const params = [
{
name: 'mock-name',
description: 'mock-description',
unit: 'mock/sq',
aggregation: 'none',
},
] as ManifestParameter[];
const object = {};

parameterize.combine(params, object);
const method = parameterize.getAggregationMethod(params[0].name);

expect(method).toEqual(params[0].aggregation);
});

it('rejects on default param override.', () => {
const params = [
{
name: 'carbon',
description: 'mock-description',
unit: 'mock/co',
aggregation: 'none',
},
] as ManifestParameter[];

parameterize.combine(params, PARAMETERS);
const method = parameterize.getAggregationMethod(params[0].name);

const expectedMethodName = 'sum';
const expectedMessage = REJECTING_OVERRIDE(params[0]);

expect(method).toEqual(expectedMethodName);
expect(mockLog).toHaveBeenCalledWith(expectedMessage);
});
});
});
7 changes: 5 additions & 2 deletions src/lib/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export const load = async (inputPath: string, paramPath?: string) => {
const rawManifest = await openYamlFileAsObject<any>(inputPath);
const {tree, ...context} = validateManifest(rawManifest);
const parametersFromCli =
paramPath && (await readAndParseJson<Parameters>(paramPath)); // todo: validate json
const parameters = parametersFromCli || PARAMETERS;
paramPath &&
(await readAndParseJson<Parameters>(paramPath)); /** @todo validate json */
const parameters =
parametersFromCli ||
PARAMETERS; /** @todo PARAMETERS should be specified in parameterize only */

return {
tree,
Expand Down
9 changes: 5 additions & 4 deletions src/lib/parameterize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {logger} from '../util/logger';
import {memoizedLog} from '../util/log-memoize';

import {STRINGS, PARAMETERS} from '../config';

import {ManifestParameter} from '../types/manifest';
import {memoizedLog} from '../util/log-memoize';
import {Parameters} from '../types/parameters';

const {REJECTING_OVERRIDE, UNKNOWN_PARAM} = STRINGS;

Expand All @@ -18,7 +19,7 @@ const Parametrize = () => {
*/
const getAggregationMethod = (unitName: string) => {
if (`${unitName}` in parametersStorage) {
return PARAMETERS[unitName as keyof typeof PARAMETERS].aggregation;
return parametersStorage[unitName as keyof typeof PARAMETERS].aggregation;
}

memoizedLog(logger.warn, UNKNOWN_PARAM(unitName));
Expand All @@ -33,12 +34,12 @@ const Parametrize = () => {
*/
const combine = (
contextParameters: ManifestParameter[] | null | undefined,
parameters: any
parameters: Parameters
) => {
if (contextParameters) {
contextParameters.forEach(param => {
if (`${param.name}` in parameters) {
logger.warn(REJECTING_OVERRIDE);
logger.warn(REJECTING_OVERRIDE(param));

return;
}
Expand Down
Loading