Skip to content

Commit

Permalink
test(util): remove mapping functions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 21, 2024
1 parent f5d7043 commit a781b0f
Showing 1 changed file with 1 addition and 154 deletions.
155 changes: 1 addition & 154 deletions src/__tests__/common/util/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ jest.mock('node:readline/promises', () =>
require('../../../__mocks__/readline')
);

import {
parseManifestFromStdin,
mapInputIfNeeded,
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';
import {parseManifestFromStdin} from '../../../common/util/helpers';

describe('common/util/helpers: ', () => {
describe('parseManifestFromStdin(): ', () => {
Expand Down Expand Up @@ -46,152 +41,4 @@ describe('common/util/helpers: ', () => {
expect(response).toEqual(expectedMessage);
});
});

describe('mapInputIfNeeded(): ', () => {
it('returns a new object with no changes when mapping is empty.', () => {
const input = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};
const mapping = {};

const result = mapInputIfNeeded(input, mapping);

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

it('returns a new object with keys remapped according to the mapping.', () => {
const input = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};
const mapping = {'device/emissions-embodied': 'device/carbon-footprint'};

const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/emissions-embodied': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};

const result = mapInputIfNeeded(input, mapping);

expect(result).toEqual(expectedOutput);
expect(result).not.toHaveProperty('device/carbon-footprint');
});
});

describe('mapConfigIfNeeded', () => {
it('returns the config as is if no mapping is provided.', () => {
const config = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/utilized',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};

const nullMapping = null;
expect(mapConfigIfNeeded(config, nullMapping!)).toEqual(config);

const undefinedMapping = undefined;
expect(mapConfigIfNeeded(config, undefinedMapping!)).toEqual(config);
});

it('recursively maps config keys and values according to the mapping.', () => {
const config = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/utilized',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};
const mapping = {
'cpu/utilized': 'cpu/util',
};

const expected = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/util',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};
expect(mapConfigIfNeeded(config, mapping)).toEqual(expected);
});

it('returns an empty object or array when config is an empty object or array.', () => {
expect(mapConfigIfNeeded({}, {})).toEqual({});
expect(mapConfigIfNeeded([], {})).toEqual([]);
});
});

describe('mapOutputIfNeeded(): ', () => {
const output = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 1,
'network/energy': 1,
'memory/energy': 1,
};
it('returns provided `output` if `mapping` is not valid.', () => {
const mapping = undefined;
const mappedOutput = mapOutputIfNeeded(output, mapping!);

expect.assertions(1);
expect(mappedOutput).toEqual(output);
});

it('returns mapped output if `mapping` has data.', () => {
const mapping = {
'cpu/energy': 'energy-from-cpu',
'network/energy': 'energy-from-network',
};
const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'energy-from-cpu': 1,
'energy-from-network': 1,
'memory/energy': 1,
};
const mappedOutput = mapOutputIfNeeded(output, mapping);

expect.assertions(1);
expect(mappedOutput).toEqual(expectedOutput);
});

it('returns the correct mapped output if some properties are mismatched.', () => {
const mapping = {
'mock-cpu/energy': 'energy-from-cpu',
'network/energy': 'energy-from-network',
};
const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 1,
'energy-from-network': 1,
'memory/energy': 1,
};
const mappedOutput = mapOutputIfNeeded(output, mapping);

expect.assertions(1);
expect(mappedOutput).toEqual(expectedOutput);
});
});
});

0 comments on commit a781b0f

Please sign in to comment.