Skip to content

Commit

Permalink
test(src): update tests accroding to changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 7, 2024
1 parent be28ccd commit 1612a62
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 62 deletions.
23 changes: 15 additions & 8 deletions src/__tests__/if-run/builtins/time-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {storeAggregationMetrics} from '../../../if-run/lib/aggregate';
import {TimeSync} from '../../../if-run/builtins/time-sync';

import {STRINGS} from '../../../if-run/config';
import {AGGREGATION_METHODS} from '../../../if-run/types/aggregation';

Settings.defaultZone = 'utc';
const {
Expand Down Expand Up @@ -56,16 +57,18 @@ jest.mock('luxon', () => {
describe('builtins/time-sync:', () => {
beforeAll(() => {
const metricStorage: AggregationParams = {
metrics: {
carbon: {method: 'sum'},
'cpu/utilization': {method: 'sum'},
'time-reserved': {method: 'avg'},
'resources-total': {method: 'none'},
},
metrics: [
'carbon',
'cpu/utilization',
'time-reserved',
'resources-total',
],
type: 'horizontal',
};

storeAggregationMetrics(metricStorage);
const convertedMetrics = metricStorage.metrics.map((metric: string) => ({
[metric]: AGGREGATION_METHODS[2],
}));
storeAggregationMetrics(...convertedMetrics);
});

describe('time-sync: ', () => {
Expand Down Expand Up @@ -472,6 +475,7 @@ describe('execute(): ', () => {
interval: 1,
'allow-padding': true,
};
storeAggregationMetrics({carbon: 'sum'});

const timeModel = TimeSync(basicConfig, parametersMetadata);

Expand Down Expand Up @@ -586,6 +590,8 @@ describe('execute(): ', () => {
interval: 5,
'allow-padding': true,
};
storeAggregationMetrics({'time-reserved': 'avg'});
storeAggregationMetrics({'resources-total': 'sum'});

const timeModel = TimeSync(basicConfig, parametersMetadata);

Expand Down Expand Up @@ -659,6 +665,7 @@ describe('execute(): ', () => {
interval: 5,
'allow-padding': true,
};
storeAggregationMetrics({'resources-total': 'none'});

const timeModel = TimeSync(basicConfig, parametersMetadata);

Expand Down
16 changes: 9 additions & 7 deletions src/__tests__/if-run/lib/aggregate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import {
aggregate,
storeAggregationMetrics,
} from '../../../if-run/lib/aggregate';
import {AGGREGATION_METHODS} from '../../../if-run/types/aggregation';

describe('lib/aggregate: ', () => {
beforeAll(() => {
const metricStorage: AggregationParams = {
metrics: {
carbon: {method: 'sum'},
},
metrics: ['carbon'],
type: 'horizontal',
};
const convertedMetrics = metricStorage.metrics.map((metric: string) => ({
[metric]: AGGREGATION_METHODS[2],
}));

storeAggregationMetrics(metricStorage);
storeAggregationMetrics(...convertedMetrics);
});

describe('aggregate(): ', () => {
Expand Down Expand Up @@ -60,7 +62,7 @@ describe('lib/aggregate: ', () => {
};

const aggregatedTree = aggregate(tree, {
metrics: {carbon: {method: 'sum'}},
metrics: ['carbon'],
type: 'horizontal',
});
const expectedAggregated = {
Expand Down Expand Up @@ -108,7 +110,7 @@ describe('lib/aggregate: ', () => {
};

const aggregatedTree = aggregate(tree, {
metrics: {carbon: {method: 'sum'}},
metrics: ['carbon'],
type: 'vertical',
});
const expectedOutputs = [
Expand Down Expand Up @@ -169,7 +171,7 @@ describe('lib/aggregate: ', () => {
};

const aggregatedTree = aggregate(tree, {
metrics: {carbon: {method: 'sum'}},
metrics: ['carbon'],
type: 'both',
});

Expand Down
59 changes: 21 additions & 38 deletions src/__tests__/if-run/util/aggregation-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,33 @@ import {PluginParams} from '@grnsft/if-core/types';
import {AggregationParams} from '../../../common/types/manifest';

import {aggregateInputsIntoOne} from '../../../if-run/util/aggregation-helper';
import {AggregationMetric} from '../../../if-run/types/aggregation';
import {
AGGREGATION_METHODS,
AggregationMetric,
} from '../../../if-run/types/aggregation';
import {storeAggregationMetrics} from '../../../if-run/lib/aggregate';

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

const {InvalidAggregationMethodError, MissingAggregationParamError} = ERRORS;
const {INVALID_AGGREGATION_METHOD, METRIC_MISSING} = STRINGS;
const {MissingAggregationParamError} = ERRORS;
const {METRIC_MISSING} = STRINGS;

describe('util/aggregation-helper: ', () => {
beforeAll(() => {
const metricStorage: AggregationParams = {
metrics: {
carbon: {method: 'sum'},
'cpu/number-cores': {method: 'none'},
'cpu/utilization': {method: 'sum'},
},
metrics: ['carbon', 'cpu/number-cores', 'cpu/utilization'],
type: 'horizontal',
};

storeAggregationMetrics(metricStorage);
const convertedMetrics = metricStorage.metrics.map((metric: string) => ({
[metric]: AGGREGATION_METHODS[2],
}));
storeAggregationMetrics(...convertedMetrics);
});

describe('aggregateInputsIntoOne(): ', () => {
it('throws error if aggregation method is none.', () => {
const inputs: PluginParams[] = [];
const metrics: AggregationMetric = {'cpu/number-cores': {method: 'none'}};
const isTemporal = false;

expect.assertions(2);

try {
aggregateInputsIntoOne(inputs, metrics, isTemporal);
} catch (error) {
expect(error).toBeInstanceOf(InvalidAggregationMethodError);

if (error instanceof InvalidAggregationMethodError) {
expect(error.message).toEqual(
INVALID_AGGREGATION_METHOD('cpu/number-cores')
);
}
}
});

it('throws error if aggregation criteria is not found in input.', () => {
const inputs: PluginParams[] = [{timestamp: '', duration: 10}];
const metrics: AggregationMetric = {'cpu/utilization': {method: 'sum'}};
const metrics: AggregationMetric[] = [{'cpu/utilization': 'sum'}];
const isTemporal = false;

expect.assertions(2);
Expand All @@ -70,7 +51,7 @@ describe('util/aggregation-helper: ', () => {
{timestamp: '', duration: 10, carbon: 10},
{timestamp: '', duration: 10, carbon: 20},
];
const metrics: AggregationMetric = {carbon: {method: 'sum'}};
const metrics: AggregationMetric[] = [{carbon: 'sum'}];
const isTemporal = true;

const expectedValue = {
Expand All @@ -87,7 +68,7 @@ describe('util/aggregation-helper: ', () => {
{timestamp: '', duration: 10, carbon: 10},
{timestamp: '', duration: 10, carbon: 20},
];
const metrics: AggregationMetric = {carbon: {method: 'sum'}};
const metrics: AggregationMetric[] = [{carbon: 'sum'}];
const isTemporal = false;

const expectedValue = {
Expand All @@ -99,18 +80,20 @@ describe('util/aggregation-helper: ', () => {

it('calculates average of metrics.', () => {
const metricStorage: AggregationParams = {
metrics: {
'cpu/utilization': {method: 'avg'},
},
metrics: ['cpu/utilization'],
type: 'horizontal',
};
const convertedMetrics = metricStorage.metrics.map((metric: string) => ({
[metric]: AGGREGATION_METHODS[2],
}));
storeAggregationMetrics(...convertedMetrics);
storeAggregationMetrics({'cpu/utilization': 'avg'});

storeAggregationMetrics(metricStorage);
const inputs: PluginParams[] = [
{timestamp: '', duration: 10, 'cpu/utilization': 10},
{timestamp: '', duration: 10, 'cpu/utilization': 90},
];
const metrics: AggregationMetric = {'cpu/utilization': {method: 'avg'}};
const metrics: AggregationMetric[] = [{'cpu/utilization': 'avg'}];
const isTemporal = false;

const expectedValue = {
Expand Down
12 changes: 3 additions & 9 deletions src/__tests__/if-run/util/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,13 @@ describe('if-run/util/helpers: ', () => {

expect(storeAggregationMetrics).toHaveBeenCalledTimes(3);
expect(storeAggregationMetrics).toHaveBeenNthCalledWith(1, {
metrics: {
carbon: {method: 'sum'},
},
carbon: 'sum',
});
expect(storeAggregationMetrics).toHaveBeenNthCalledWith(2, {
metrics: {
cpu: {method: 'avg'},
},
cpu: 'avg',
});
expect(storeAggregationMetrics).toHaveBeenNthCalledWith(3, {
metrics: {
carbon: {method: 'none'},
},
carbon: 'none',
});
});

Expand Down

0 comments on commit 1612a62

Please sign in to comment.