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 test coverage #58

Merged
merged 23 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
79bfda8
test(lib): cover cloud-metedata test
manushak Mar 14, 2024
53ce07b
fix(lib): remove unnecessary check in cloud-metadata
manushak Mar 14, 2024
1040aca
test(lib): cover csv-export plugin tests
manushak Mar 14, 2024
c07a8c7
test(lib): cover divide plugin test
manushak Mar 14, 2024
8cf6ab8
test(lib): add coverage for regex plugin
manushak Mar 14, 2024
1bbfa03
fix(lib): fix regex validation check
manushak Mar 14, 2024
91317cc
test(lib): add coverage for sci plugin
manushak Mar 14, 2024
5531acf
test(lib): add test coverage for sci-m plugin
manushak Mar 14, 2024
be6b515
fix(lib): remove unnecessary function in sci-m
manushak Mar 14, 2024
631e199
test(lib): add coverage for tdp-finder plugin
manushak Mar 15, 2024
fed861f
test(lib): add test coverage for shell plugin
manushak Mar 15, 2024
29732b7
test(lib): add test coverage for commom-generator of mock-observation…
manushak Mar 15, 2024
1958a52
test(lib): add assertion in common-generartor test
manushak Mar 15, 2024
566909a
test(lib): add test coverage for RandIntGenerator of mock-observation…
manushak Mar 15, 2024
3bb7983
fix(lib): add validation for global config in mock-obervations
manushak Mar 16, 2024
2385594
test(lib): add test coverage for mock-observations plugin
manushak Mar 16, 2024
573f25b
test(lib): add tests for validations.ts
manushak Mar 16, 2024
ae2712c
Merge branch 'main' into test-coverage
manushak Mar 16, 2024
bbe2a3a
fix(lib): update file name in cloud-metadata
manushak Mar 16, 2024
7380697
feat(package): update package version
manushak Mar 16, 2024
e12d9f4
test(lib): fix ' instead of `
narekhovhannisyan Mar 18, 2024
658aa81
test(lib): remove unneccesary expect function
manushak Mar 18, 2024
4c6581d
Merge branch 'test-coverage' of https://github.com/Green-Software-Fou…
manushak Mar 18, 2024
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions src/__tests__/unit/lib/cloud-metadata/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,134 @@ describe('lib/cloud-metadata:', () => {
]);
});

it('returns a result when azure instance type do not have size number.', async () => {
const inputs = [
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_B1ms',
'cloud/vendor': 'azure',
},
];

const result = await cloudMetadata.execute(inputs);

expect.assertions(1);

expect(result).toStrictEqual([
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_B1ms',
'cloud/vendor': 'azure',
'cpu/thermal-design-power': 270,
'physical-processor':
'Intel® Xeon® Platinum 8370C,Intel® Xeon® Platinum 8272CL,Intel® Xeon® 8171M 2.1 GHz,Intel® Xeon® E5-2673 v4 2.3 GHz,Intel® Xeon® E5-2673 v3 2.4 GHz',
'vcpus-allocated': 1,
'vcpus-total': 64,
'memory-available': 2,
},
]);
});

it('returns a result with configured outputs.', async () => {
const inputs = [
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_A1_v2',
'cloud/vendor': 'azure',
'cloud/region': 'francesouth',
},
];
const config = {
fields: [
'cloud/vendor',
'cloud/region-wt-id',
'cloud/instance-type',
'physical-processor',
'cpu/thermal-design-power',
],
};
const result = await cloudMetadata.execute(inputs, config);

expect.assertions(1);

expect(result).toStrictEqual([
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_A1_v2',
'cloud/region': 'francesouth',
'cloud/region-wt-id': 'FR',
'cloud/vendor': 'azure',
'cpu/thermal-design-power': 205,
'physical-processor':
'Intel® Xeon® Platinum 8272CL,Intel® Xeon® 8171M 2.1 GHz,Intel® Xeon® E5-2673 v4 2.3 GHz,Intel® Xeon® E5-2673 v3 2.4 GHz',
},
]);
});

it('returns a result when provided a `cloud/region` in the input.', async () => {
const inputs = [
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_A1_v2',
'cloud/vendor': 'azure',
'cloud/region': 'francesouth',
},
];

const result = await cloudMetadata.execute(inputs);

expect.assertions(1);

expect(result).toStrictEqual([
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_A1_v2',
'cloud/region': 'francesouth',
'cloud/region-cfe': 'France',
'cloud/region-em-zone-id': 'FR',
'cloud/region-geolocation': '48.8567,2.3522',
'cloud/region-location': 'Paris',
'cloud/region-wt-id': 'FR',
'cloud/vendor': 'azure',
'cpu/thermal-design-power': 205,
'memory-available': 2,
'physical-processor':
'Intel® Xeon® Platinum 8272CL,Intel® Xeon® 8171M 2.1 GHz,Intel® Xeon® E5-2673 v4 2.3 GHz,Intel® Xeon® E5-2673 v3 2.4 GHz',
'vcpus-allocated': 1,
'vcpus-total': 52,
},
]);
});

it('throws an error when provided a wrong `cloud/region` for vendor in the input.', async () => {
const errorMessage =
"CloudMetadata: 'uk-west' region is not supported in 'azure' cloud vendor.";
const inputs = [
{
timestamp: '',
duration: 5,
'cloud/instance-type': 'Standard_A1_v2',
'cloud/vendor': 'azure',
'cloud/region': 'uk-west',
},
];

expect.assertions(2);

try {
await cloudMetadata.execute(inputs);
} catch (error) {
expect(error).toStrictEqual(new UnsupportedValueError(errorMessage));
expect(error).toBeInstanceOf(UnsupportedValueError);
}
});

it('throws on `cloud/instance-type` when `cloud/vendor` is aws.', async () => {
const errorMessage =
"CloudMetadata(cloud/instance-type): 't2.micro2' instance type is not supported in 'aws' cloud vendor.";
Expand Down
32 changes: 31 additions & 1 deletion src/__tests__/unit/lib/csv-export/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {CsvExport} from '../../../../lib/csv-export';

import {ERRORS} from '../../../../util/errors';

const {MakeDirectoryError, WriteFileError} = ERRORS;
const {MakeDirectoryError, WriteFileError, InputValidationError} = ERRORS;

jest.mock('fs/promises', () => ({
mkdir: jest.fn<() => Promise<void>>().mockResolvedValue(),
Expand Down Expand Up @@ -126,6 +126,36 @@ describe('lib/csv-export: ', () => {
expect(result).toStrictEqual(input);
});

it('throws an error when node config is not provided.', async () => {
const csvExport = CsvExport();

const input = [
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 10,
energy: 10,
carbon: 2,
},
{
timestamp: '2023-12-12T00:00:10.000Z',
duration: 30,
energy: 20,
carbon: 5,
},
];

expect.assertions(2);

try {
await csvExport.execute(input);
} catch (error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error).toEqual(
new InputValidationError('CsvExport: Configuration data is missing.')
);
}
});

it('throws an error when a file writing fails.', async () => {
(fs.writeFile as jest.Mock).mockImplementation(() => {
throw new Error('Permission denied');
Expand Down
46 changes: 45 additions & 1 deletion src/__tests__/unit/lib/divide/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Divide} from '../../../../lib';

import {ERRORS} from '../../../../util/errors';

const {InputValidationError} = ERRORS;
const {InputValidationError, ConfigValidationError} = ERRORS;

describe('lib/divide: ', () => {
describe('Divide: ', () => {
Expand Down Expand Up @@ -102,6 +102,25 @@ describe('lib/divide: ', () => {
});
});

it('throws an error on missing global config.', async () => {
const expectedMessage = 'Divide: Configuration data is missing.';
const config = undefined;
const divide = Divide(config!);

expect.assertions(1);

try {
await divide.execute([
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
},
]);
} catch (error) {
expect(error).toStrictEqual(new ConfigValidationError(expectedMessage));
}
});

it('throws an error when `denominator` is 0.', async () => {
const expectedMessage =
'"denominator" parameter is number must be greater than 0. Error code: too_small.';
Expand All @@ -127,5 +146,30 @@ describe('lib/divide: ', () => {
expect(error).toStrictEqual(new InputValidationError(expectedMessage));
}
});

it('throws an error when `denominator` is string.', async () => {
const expectedMessage = 'Divide: `10` is missing from the input.';

const globalConfig = {
numerator: 'vcpus-allocated',
denominator: '10',
output: 'vcpus-allocated-per-second',
};
const divide = Divide(globalConfig);

expect.assertions(1);

try {
await divide.execute([
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'vcpus-allocated': 24,
},
]);
} catch (error) {
expect(error).toStrictEqual(new InputValidationError(expectedMessage));
}
});
});
});
10 changes: 8 additions & 2 deletions src/__tests__/unit/lib/mock-observations/CommonGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ const {InputValidationError} = ERRORS;

describe('lib/mock-observations/CommonGenerator: ', () => {
describe('initialize: ', () => {
it('initialize with an empty config.', async () => {
it('throws an error when config is not empty object.', async () => {
const commonGenerator = CommonGenerator({});

expect.assertions(1);

try {
CommonGenerator({});
expect(commonGenerator.next([]));
manushak marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
expect(error).toEqual(
new InputValidationError(
Expand All @@ -29,6 +33,8 @@ describe('lib/mock-observations/CommonGenerator: ', () => {
};
const commonGenerator = CommonGenerator(config);

expect.assertions(1);

expect(commonGenerator.next([])).toStrictEqual({
key1: 'value1',
key2: 'value2',
Expand Down
24 changes: 22 additions & 2 deletions src/__tests__/unit/lib/mock-observations/RandIntGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {InputValidationError} = ERRORS;

describe('lib/mock-observations/RandIntGenerator: ', () => {
describe('initialize', () => {
it('initialize with an empty name', async () => {
it('throws an error when the generator name is empty string.', async () => {
expect.assertions(1);
try {
RandIntGenerator('', {});
} catch (error) {
Expand All @@ -20,7 +21,8 @@ describe('lib/mock-observations/RandIntGenerator: ', () => {
}
});

it('initialize with an empty config', async () => {
it('throws an error when config is empty object.', async () => {
expect.assertions(1);
try {
RandIntGenerator('generator-name', {});
} catch (error) {
Expand All @@ -31,6 +33,22 @@ describe('lib/mock-observations/RandIntGenerator: ', () => {
);
}
});

it('throws an error `min` is missing from the config.', async () => {
const config = {max: 90};

expect.assertions(1);

try {
RandIntGenerator('random', config);
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: Config is missing min or max.'
)
);
}
});
});

describe('next(): ', () => {
Expand All @@ -42,6 +60,8 @@ describe('lib/mock-observations/RandIntGenerator: ', () => {
const randIntGenerator = RandIntGenerator('random', config);
const result = randIntGenerator.next([]) as {random: number};

expect.assertions(4);

expect(result).toBeInstanceOf(Object);
expect(result).toHaveProperty('random');
expect(result.random).toBeGreaterThanOrEqual(10);
Expand Down
Loading
Loading