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 #558

Merged
merged 2 commits into from
Mar 27, 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
21 changes: 19 additions & 2 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ export const readFile = async (filePath: string) => {
return Promise.reject(new Error('rejected'));
}
if (filePath.includes('json')) {
if (filePath.includes('param')) {
return JSON.stringify({
'mock-carbon': {
description: 'an amount of carbon emitted into the atmosphere',
unit: 'gCO2e',
aggregation: 'sum',
},
'mock-cpu': {
description: 'number of cores available',
unit: 'cores',
aggregation: 'none',
},
});
}

return JSON.stringify(filePath);
}

Expand All @@ -19,8 +34,10 @@ export const readFile = async (filePath: string) => {
complexity: moderate
category: cloud
initialize:
models:
boavizta-cpu:
plugins:
mockavizta:
path: mockavizta
method: Mockavizta
tree:
children:
front-end:
Expand Down
146 changes: 146 additions & 0 deletions src/__tests__/unit/lib/load.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
jest.mock('fs/promises', () => require('../../../__mocks__/fs'));
jest.mock(
'mockavizta',
() => ({
__esmodule: true,
Mockavizta: () => ({
execute: (input: PluginParams) => input,
metadata: {
kind: 'execute',
},
}),
}),
{virtual: true}
);

import {load} from '../../../lib/load';

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

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

describe('lib/load: ', () => {
describe('load(): ', () => {
it('loads yaml with default parameters.', async () => {
const inputPath = 'mock.yaml';
const paramPath = undefined;

const result = await load(inputPath, paramPath);

const expectedValue = {
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
context: {
name: 'gsf-demo',
description: 'Hello',
tags: {
kind: 'web',
complexity: 'moderate',
category: 'cloud',
},
initialize: {
plugins: {
mockavizta: {
path: 'mockavizta',
method: 'Mockavizta',
},
},
},
},
parameters: PARAMETERS,
};

expect(result).toEqual(expectedValue);
});

it('loads yaml with custom parameters.', async () => {
const inputPath = 'param-mock.yaml';
const paramPath = 'param-mock.json';

const result = await load(inputPath, paramPath);

const expectedParameters = {
'mock-carbon': {
description: 'an amount of carbon emitted into the atmosphere',
unit: 'gCO2e',
aggregation: 'sum',
},
'mock-cpu': {
description: 'number of cores available',
unit: 'cores',
aggregation: 'none',
},
};
const expectedValue = {
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
context: {
name: 'gsf-demo',
description: 'Hello',
tags: {
kind: 'web',
complexity: 'moderate',
category: 'cloud',
},
initialize: {
plugins: {
mockavizta: {
path: 'mockavizta',
method: 'Mockavizta',
},
},
},
},
parameters: expectedParameters,
};

expect(result).toEqual(expectedValue);
});
});
});
Loading