Skip to content

Commit

Permalink
test(builtins): add tests related to inline arithmetic feature
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Sep 6, 2024
1 parent dd1cfb0 commit 575ecee
Show file tree
Hide file tree
Showing 13 changed files with 834 additions and 9 deletions.
74 changes: 73 additions & 1 deletion src/__tests__/if-run/builtins/coefficient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ERRORS} from '@grnsft/if-core/utils';
import {Coefficient} from '../../../if-run/builtins/coefficient';

import {STRINGS} from '../../../if-run/config';
import {CoefficientConfig} from '@grnsft/if-core/types';

const {InputValidationError, ConfigError} = ERRORS;
const {MISSING_CONFIG} = STRINGS;
Expand Down Expand Up @@ -113,7 +114,78 @@ describe('builtins/coefficient: ', () => {
expect(result).toStrictEqual(expectedResult);
});

it('throws an error when global config is not provided.', () => {
it('successfully executes when a parameter has an arithmetic expression.', () => {
expect.assertions(1);
const config = {
'input-parameter': '=3*carbon',
coefficient: 3,
'output-parameter': 'carbon-product',
};
const parametersMetadata = {
inputs: {},
outputs: {},
};
const coefficient = Coefficient(config, parametersMetadata, {});

const expectedResult = [
{
duration: 3600,
carbon: 3,
'carbon-product': 27,
timestamp: '2021-01-01T00:00:00Z',
},
];

const result = coefficient.execute([
{
duration: 3600,
carbon: 3,
timestamp: '2021-01-01T00:00:00Z',
},
]);

expect.assertions(1);

expect(result).toStrictEqual(expectedResult);
});

it('throws an error when the `coefficient` has wrong arithmetic expression.', () => {
const config = {
'input-parameter': 'carbon',
coefficient: 'mock-param',
'output-parameter': 'carbon-product',
};
const parametersMetadata = {
inputs: {},
outputs: {},
};
const coefficient = Coefficient(
config as any as CoefficientConfig,
parametersMetadata,
{}
);

expect.assertions(2);

try {
coefficient.execute([
{
duration: 3600,
carbon: 'some-param',
timestamp: '2021-01-01T00:00:00Z',
},
]);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toEqual(
new InputValidationError(
'"coefficient" parameter is expected number, received string. Error code: invalid_type.'
)
);
}
});

it('throws an error when config is not provided.', () => {
const config = undefined;
const coefficient = Coefficient(config!, parametersMetadata, {});

Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/if-run/builtins/copy-param.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,36 @@ describe('builtins/copy: ', () => {

expect(result).toStrictEqual(expectedResult);
});

it('successfully executes when the `from` contains arithmetic expression', () => {
const config = {
'keep-existing': false,
from: '=3*size',
to: 'if-size',
};
const copy = Copy(config, parametersMetadata, {});

const inputs = [
{
timestamp: '2024-07-05T13:45:48.398Z',
duration: 3600,
size: 0.05,
},
];

const expectedResult = [
{
timestamp: '2024-07-05T13:45:48.398Z',
duration: 3600,
'if-size': 0.15000000000000002,
},
];

expect.assertions(1);
const result = copy.execute(inputs);

expect(result).toEqual(expectedResult);
});
});
});
});
61 changes: 60 additions & 1 deletion src/__tests__/if-run/builtins/divide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('builtins/divide: ', () => {
expect(result).toStrictEqual(expectedResult);
});

it('returns a result when `denominator` is provded in input.', async () => {
it('returns a result when `denominator` is provided in input.', async () => {
expect.assertions(1);
const config = {
numerator: 'vcpus-allocated',
Expand Down Expand Up @@ -137,6 +137,65 @@ describe('builtins/divide: ', () => {
expect(response).toEqual(expectedResult);
});

it('successfully executes when a parameter contains arithmetic expression.', () => {
expect.assertions(1);

const config = {
numerator: '=3*"vcpus-allocated"',
denominator: 'duration',
output: 'vcpus-allocated-per-second',
};

const divide = Divide(config, parametersMetadata, {});
const input = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'vcpus-allocated': 24,
},
];
const response = divide.execute(input);

const expectedResult = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'vcpus-allocated': 24,
'vcpus-allocated-per-second': 72 / 3600,
},
];

expect(response).toEqual(expectedResult);
});

it('throws an error the `numerator` parameter has wrong arithmetic expression.', () => {
const config = {
numerator: '3*"vcpus-allocated"',
denominator: 'duration',
output: 'vcpus-allocated-per-second',
};

const divide = Divide(config, parametersMetadata, {});
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'vcpus-allocated': 24,
},
];
expect.assertions(2);
try {
divide.execute(inputs);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toEqual(
new InputValidationError(
'The `numerator` contains an invalid arithmetic expression. It should start with `=` and include the symbols `*`, `+`, `-` and `/`.'
)
);
}
});

it('throws an error on missing params in input.', async () => {
const expectedMessage =
'"vcpus-allocated" parameter is required. Error code: invalid_type.';
Expand Down
100 changes: 97 additions & 3 deletions src/__tests__/if-run/builtins/exponent.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {ExponentConfig} from '@grnsft/if-core/types';
import {ERRORS} from '@grnsft/if-core/utils';

import {STRINGS} from '../../../if-run/config';
import {Exponent} from '../../../if-run/builtins/exponent';

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

const {MISSING_CONFIG} = STRINGS;

describe('builtins/exponent: ', () => {
describe('Exponent: ', () => {
Expand Down Expand Up @@ -124,7 +128,7 @@ describe('builtins/exponent: ', () => {
} catch (error) {
expect(error).toStrictEqual(
new InputValidationError(
'"input-parameter" parameter is required. Error code: invalid_type.'
'"energy/base" parameter is required. Error code: invalid_type.'
)
);
}
Expand All @@ -145,7 +149,7 @@ describe('builtins/exponent: ', () => {
} catch (error) {
expect(error).toStrictEqual(
new InputValidationError(
'"input-parameter" parameter is expected number, received string. Error code: invalid_type.'
'"energy/base" parameter is expected number, received string. Error code: invalid_type.'
)
);
}
Expand Down Expand Up @@ -180,6 +184,96 @@ describe('builtins/exponent: ', () => {

expect(response).toEqual(expectedResult);
});

it('successfully executes when a parameter contains arithmetic expression.', () => {
const config = {
'input-parameter': "=2*'energy/base'",
exponent: 3,
'output-parameter': 'energy',
};
const parametersMetadata = {
inputs: {},
outputs: {},
};

const exponent = Exponent(config, parametersMetadata, {});

expect.assertions(1);

const expectedResult = [
{
duration: 3600,
'energy/base': 4,
energy: 512,
timestamp: '2021-01-01T00:00:00Z',
},
];

const result = exponent.execute([
{
duration: 3600,
'energy/base': 4,
timestamp: '2021-01-01T00:00:00Z',
},
]);

expect(result).toStrictEqual(expectedResult);
});

it('throws an error when the `exponent` has wrong arithmetic expression.', () => {
const config = {
'input-parameter': "=2*'energy/base'",
exponent: "3*'mock-param'",
'output-parameter': 'energy',
};
const parametersMetadata = {
inputs: {},
outputs: {},
};

const exponent = Exponent(
config as any as ExponentConfig,
parametersMetadata,
{}
);

expect.assertions(2);

try {
exponent.execute([
{
duration: 3600,
'energy/base': 4,
timestamp: '2021-01-01T00:00:00Z',
},
]);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toEqual(
new InputValidationError(
'The `exponent` contains an invalid arithmetic expression. It should start with `=` and include the symbols `*`, `+`, `-` and `/`.'
)
);
}
});
});

it('throws an error on missing config.', async () => {
const config = undefined;
const divide = Exponent(config!, parametersMetadata, {});

expect.assertions(1);

try {
await divide.execute([
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
},
]);
} catch (error) {
expect(error).toStrictEqual(new ConfigError(MISSING_CONFIG));
}
});
});
});
61 changes: 61 additions & 0 deletions src/__tests__/if-run/builtins/interpolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,67 @@ describe('builtins/interpolation: ', () => {
expect(plugin.execute(inputs)).toEqual(outputs);
});

it('successfully executes when the config parameter contains an arithmetic expression.', () => {
const config = {
method: Method.LINEAR,
x: [0, 10, 50, 100],
y: [0.12, 0.32, 0.75, 1.02],
'input-parameter': "=2*'cpu/utilization'",
'output-parameter': 'interpolation-result',
};
const inputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 90,
},
];

const plugin = Interpolation(config, parametersMetadata, {});
const outputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 90,
'interpolation-result': 0,
},
];

expect.assertions(1);
expect(plugin.execute(inputs)).toEqual(outputs);
});

it('throws an error the config parameter contains wrong arithmetic expression.', () => {
const config = {
method: Method.LINEAR,
x: [0, 10, 50, 100],
y: [0.12, 0.32, 0.75, 1.02],
'input-parameter': "2*'cpu/utilization'",
'output-parameter': 'interpolation-result',
};
const inputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 90,
},
];

const plugin = Interpolation(config, parametersMetadata, {});

expect.assertions(2);
try {
plugin.execute(inputs);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toEqual(
new InputValidationError(
'The `input-parameter` contains an invalid arithmetic expression. It should start with `=` and include the symbols `*`, `+`, `-` and `/`.'
)
);
}
});

it('throws an when the config is not provided.', () => {
const config = undefined;
const plugin = Interpolation(config!, parametersMetadata, {});
Expand Down
Loading

0 comments on commit 575ecee

Please sign in to comment.