Skip to content

Commit

Permalink
Merge pull request #552 from Green-Software-Foundation/parameterize-u…
Browse files Browse the repository at this point in the history
…nit-tests

Parameterize unit tests
  • Loading branch information
narekhovhannisyan committed Mar 21, 2024
2 parents a1c8019 + e7d7d36 commit bc2ee54
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
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

0 comments on commit bc2ee54

Please sign in to comment.