Skip to content

Commit

Permalink
test(builtins): update test accorgind to changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 12, 2024
1 parent e3bf7c6 commit 8dc3c76
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 83 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/if-run/builtins/coefficient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('builtins/coefficient: ', () => {
const mapping = {
carbon: 'carbon-for-production',
};

const coefficient = Coefficient(
globalConfig,
parametersMetadata,
Expand All @@ -76,7 +77,7 @@ describe('builtins/coefficient: ', () => {
const result = coefficient.execute([
{
duration: 3600,
carbon: 3,
'carbon-for-production': 3,
timestamp: '2021-01-01T00:00:00Z',
},
]);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/if-run/builtins/copy-param.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('builtins/copy: ', () => {
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
original: 'hello',
from: 'hello',
},
]);

Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/if-run/builtins/csv-lookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ describe('builtins/CSVLookup: ', () => {
};
const parameterMetadata = {inputs: {}, outputs: {}};
const mapping = {
tdp: 'cpu/tdp',
'cpu/utilized': 'cpu/util',
};
const csvLookup = CSVLookup(globalConfig, parameterMetadata, mapping);
Expand All @@ -142,7 +141,7 @@ describe('builtins/CSVLookup: ', () => {
{
timestamp: '2024-03-01',
'cpu/available': 16,
'cpu/utilized': 16,
'cpu/util': 16,
'cpu/manufacturer': 'AWS',
},
]);
Expand All @@ -152,7 +151,7 @@ describe('builtins/CSVLookup: ', () => {
'cpu/available': 16,
'cpu/util': 16,
'cpu/manufacturer': 'AWS',
'cpu/tdp': 150,
tdp: 150,
},
];

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/if-run/builtins/divide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('builtins/divide: ', () => {
const result = await divide.execute([
{
duration: 3600,
'vcpus-allocated': 24,
'vcpus-distributed': 24,
timestamp: '2021-01-01T00:00:00Z',
},
]);
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/if-run/builtins/exponent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ describe('builtins/exponent: ', () => {
const mapping = {
'energy/base': 'energy/main',
};
const globalConfig = {
'input-parameter': 'energy/base',
exponent: 3,
'output-parameter': 'energy',
};
const exponent = Exponent(globalConfig, parametersMetadata, mapping);
const expectedResult = [
{
Expand All @@ -66,7 +71,7 @@ describe('builtins/exponent: ', () => {
const result = await exponent.execute([
{
duration: 3600,
'energy/base': 2,
'energy/main': 2,
timestamp: '2021-01-01T00:00:00Z',
},
]);
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/if-run/builtins/interpolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ describe('builtins/interpolation: ', () => {
'cpu/utilization': 'cpu/util',
'interpolation-result': 'result',
};
const globalConfig = {
method: Method.LINEAR,
x: [0, 10, 50, 100],
y: [0.12, 0.32, 0.75, 1.02],
'input-parameter': 'cpu/utilization',
'output-parameter': 'interpolation-result',
};
const inputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/util': 45,
},
];
const plugin = Interpolation(globalConfig, parametersMetadata, mapping);
const outputs = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/if-run/builtins/mock-observations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('builtins/mock-observations: ', () => {
'common-key': 'common-val',
},
randint: {
'cpu/utilization': {min: 10, max: 11},
'cpu/util': {min: 10, max: 11},
},
},
};
Expand Down
10 changes: 7 additions & 3 deletions src/__tests__/if-run/builtins/multiply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('builtins/multiply: ', () => {
'network/energy': 'energy-from-network',
'memory/energy': 'energy-from-memory',
};
const globalConfig = {
'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'],
'output-parameter': 'energy',
};
const multiply = Multiply(globalConfig, parametersMetadata, mapping);

const expectedResult = [
Expand All @@ -75,9 +79,9 @@ describe('builtins/multiply: ', () => {
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 2,
'network/energy': 2,
'memory/energy': 2,
'energy-from-cpu': 2,
'energy-from-network': 2,
'energy-from-memory': 2,
},
]);

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/if-run/builtins/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('builtins/regex: ', () => {
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cloud/instance-type': 'Standard_DS1_v2',
'instance-type': 'Standard_DS1_v2',
},
]);

Expand Down
13 changes: 6 additions & 7 deletions src/__tests__/if-run/builtins/sci-embodied.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('builtins/sci-embodied:', () => {
inputs: {},
outputs: {},
};
const sciEmbodied = SciEmbodied(parametersMetadata, {});
const sciEmbodied = SciEmbodied(undefined, parametersMetadata, {});

describe('init: ', () => {
it('successfully initalized.', () => {
Expand Down Expand Up @@ -72,22 +72,21 @@ describe('builtins/sci-embodied:', () => {
it('executes when `mapping` has valid data.', async () => {
const mapping = {
'device/emissions-embodied': 'device/carbon-footprint',
'carbon-embodied': 'carbon-footprint',
};
const sciEmbodied = SciEmbodied(parametersMetadata, mapping);
const sciEmbodied = SciEmbodied(undefined, parametersMetadata, mapping);
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/emissions-embodied': 200,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
},
{
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30 * 2,
'device/emissions-embodied': 200,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
Expand All @@ -106,7 +105,7 @@ describe('builtins/sci-embodied:', () => {
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
'carbon-footprint': 4.10958904109589,
'carbon-embodied': 4.10958904109589,
},
{
timestamp: '2021-01-01T00:00:00Z',
Expand All @@ -115,7 +114,7 @@ describe('builtins/sci-embodied:', () => {
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
'carbon-footprint': 4.10958904109589 * 2,
'carbon-embodied': 4.10958904109589 * 2,
},
]);
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/if-run/builtins/sci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('builtins/sci:', () => {

it('successfully executes when `mapping` has valid data.', async () => {
const mapping = {
'carbon-embodied': 'carbon-footprint',
'carbon-footprint': 'carbon-embodied',
};
const sci = Sci(config, parametersMetadata, mapping);
const inputs = [
Expand All @@ -69,7 +69,7 @@ describe('builtins/sci:', () => {
{
timestamp: '2021-01-01T00:00:00Z',
'carbon-operational': 0.02,
'carbon-footprint': 5,
'carbon-embodied': 5,
carbon: 5.02,
users: 100,
duration: 1,
Expand Down
59 changes: 3 additions & 56 deletions src/__tests__/if-run/builtins/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('builtins/shell', () => {
inputs: {},
outputs: {},
};
const shell = Shell(globalConfig, parametersMetadata, {});
const shell = Shell(globalConfig, parametersMetadata);

describe('init: ', () => {
it('successfully initalized.', () => {
Expand Down Expand Up @@ -56,61 +56,8 @@ describe('builtins/shell', () => {
expect(mockLoadAll).toHaveBeenCalledWith('mocked stdout');
});

it('executes when `mapping` is provided.', async () => {
const mapping = {
'cpu/energy': 'energy-for-cpu',
};
const shell = Shell(globalConfig, parametersMetadata, mapping);
const mockSpawnSync = spawnSync as jest.MockedFunction<
typeof spawnSync
>;
mockSpawnSync.mockReturnValueOnce({stdout: 'mocked stdout'} as any);

const mockLoadAll = loadAll as jest.MockedFunction<typeof loadAll>;
mockLoadAll.mockReturnValueOnce([
{
timestamp: '2023-11-02T10:35:31.820Z',
duration: 3600,
'energy-for-cpu': 0.002,
'memory/energy': 0.000005,
energy: 1,
},
] as any);

const inputs = [
{
timestamp: '2023-11-02T10:35:31.820Z',
duration: 3600,
'cpu/energy': 0.002,
'memory/energy': 0.000005,
},
];

expect.assertions(3);

const result = await shell.execute(inputs);
expect(result).toEqual([
{
timestamp: '2023-11-02T10:35:31.820Z',
duration: 3600,
'energy-for-cpu': 0.002,
'memory/energy': 0.000005,
energy: 1,
},
]);

expect(mockSpawnSync).toHaveBeenCalledWith(
'python3',
['/path/to/script.py'],
{
encoding: 'utf8',
}
);
expect(mockLoadAll).toHaveBeenCalledWith('mocked stdout');
});

it('throws an error if validation fails.', async () => {
const shell = Shell({}, parametersMetadata, {});
const shell = Shell({}, parametersMetadata);
const invalidInputs = [
{duration: 3600, timestamp: '2022-01-01T00:00:00Z', command: 123},
];
Expand All @@ -129,7 +76,7 @@ describe('builtins/shell', () => {
});

it('throws an error when shell could not run command.', async () => {
const shell = Shell(globalConfig, parametersMetadata, {});
const shell = Shell(globalConfig, parametersMetadata);
(spawnSync as jest.Mock).mockImplementation(() => {
throw new InputValidationError('Could not run the command');
});
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/if-run/builtins/subtract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ describe('builtins/subtract: ', () => {
const mapping = {
'cpu/energy': 'energy-for-cpu',
};
const globalConfig = {
'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'],
'output-parameter': 'energy/diff',
};
const subtract = Subtract(globalConfig, parametersMetadata, mapping);
expect.assertions(1);

Expand All @@ -72,7 +76,7 @@ describe('builtins/subtract: ', () => {
const result = await subtract.execute([
{
duration: 3600,
'cpu/energy': 4,
'energy-for-cpu': 4,
'network/energy': 2,
'memory/energy': 1,
timestamp: '2021-01-01T00:00:00Z',
Expand Down
9 changes: 7 additions & 2 deletions src/__tests__/if-run/builtins/sum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ describe('builtins/sum: ', () => {

it('successfully executes when `mapping` has valid data.', () => {
expect.assertions(1);

const mapping = {
'cpu/energy': 'energy-from-cpu',
'network/energy': 'energy-from-network',
};
const globalConfig = {
'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'],
'output-parameter': 'energy',
};

const sum = Sum(globalConfig, parametersMetadata, mapping);

Expand All @@ -78,8 +83,8 @@ describe('builtins/sum: ', () => {
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 1,
'network/energy': 1,
'energy-from-cpu': 1,
'energy-from-network': 1,
'memory/energy': 1,
},
]);
Expand Down
10 changes: 7 additions & 3 deletions src/__tests__/if-run/builtins/time-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ describe('builtins/time-sync:', () => {
expect(result).toStrictEqual(expectedResult);
});

it.skip('returns a result when `mapping` has valid data.', async () => {
it('returns a result when `mapping` has valid data.', async () => {
const basicConfig = {
'start-time': '2023-12-12T00:00:00.000Z',
'end-time': '2023-12-12T00:00:09.000Z',
Expand All @@ -642,19 +642,23 @@ describe('builtins/time-sync:', () => {
const mapping = {
'time-reserved': 'time-allocated',
};

storeAggregationMetrics({'time-allocated': 'avg'});
storeAggregationMetrics({'resources-total': 'sum'});

const timeModel = TimeSync(basicConfig, parametersMetadata, mapping);

const result = await timeModel.execute([
{
timestamp: '2023-12-12T00:00:00.000Z',
duration: 3,
'time-reserved': 5,
'time-allocated': 5,
'resources-total': 10,
},
{
timestamp: '2023-12-12T00:00:05.000Z',
duration: 3,
'time-reserved': 5,
'time-allocated': 5,
'resources-total': 10,
},
]);
Expand Down

0 comments on commit 8dc3c76

Please sign in to comment.