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

Aggregate tests #563

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Changes from all commits
Commits
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
201 changes: 201 additions & 0 deletions src/__tests__/unit/lib/aggregate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {aggregate} from '../../../lib/aggregate';

describe('lib/aggregate: ', () => {
describe('aggregate(): ', () => {
it('returns tree if aggregation is missing.', () => {
const tree = {};
const aggregation = undefined;

const aggregatedTree = aggregate(tree, aggregation);
expect(aggregatedTree).toEqual(tree);
});

it('returns tree if aggregation.type is missing.', () => {
const tree = {};
const aggregation = {
metrics: [],
};

// @ts-ignore
const aggregatedTree = aggregate(tree, aggregation);
expect(aggregatedTree).toEqual(tree);
});

it('does horizontal aggregation.', () => {
const tree = {
children: {
mocks: {
outputs: [
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
],
},
},
};

const aggregatedTree = aggregate(tree, {
metrics: ['carbon'],
type: 'horizontal',
});
const expectedAggregated = {
carbon:
tree.children.mocks.outputs[0].carbon +
tree.children.mocks.outputs[1].carbon,
};
expect(aggregatedTree.children.mocks.aggregated).toEqual(
expectedAggregated
);
});

it('does vertical aggregation.', () => {
const tree = {
children: {
'mocks-1': {
outputs: [
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
],
},
'mocks-2': {
outputs: [
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 20,
},
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 20,
},
],
},
},
};

const aggregatedTree = aggregate(tree, {
metrics: ['carbon'],
type: 'vertical',
});
const expectedOutputs = [
{
carbon: 30,
timestamp: 'mock-timestamp',
duration: 'mock-duration',
},
{
carbon: 30,
timestamp: 'mock-timestamp',
duration: 'mock-duration',
},
];
const expectedAggregated = {
carbon:
tree.children['mocks-1'].outputs[0].carbon +
tree.children['mocks-2'].outputs[0].carbon +
tree.children['mocks-1'].outputs[1].carbon +
tree.children['mocks-2'].outputs[1].carbon,
};
expect(aggregatedTree.outputs).toEqual(expectedOutputs);
expect(aggregatedTree.aggregated).toEqual(expectedAggregated);
});

it('does both aggregations.', () => {
const tree = {
children: {
'mocks-1': {
outputs: [
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 10,
},
],
},
'mocks-2': {
outputs: [
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 20,
},
{
timestamp: 'mock-timestamp',
duration: 'mock-duration',
carbon: 20,
},
],
},
},
};

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

// horizontal aggregation
const expectedNode1Horizontal = {
carbon:
tree.children['mocks-1'].outputs[0].carbon +
tree.children['mocks-1'].outputs[1].carbon,
};
expect(aggregatedTree.children['mocks-1'].aggregated).toEqual(
expectedNode1Horizontal
);
const expectedNode2Horizontal = {
carbon:
tree.children['mocks-2'].outputs[0].carbon +
tree.children['mocks-2'].outputs[1].carbon,
};
expect(aggregatedTree.children['mocks-2'].aggregated).toEqual(
expectedNode2Horizontal
);

// vertical aggregation
const expectedOutputs = [
{
carbon: 30,
timestamp: 'mock-timestamp',
duration: 'mock-duration',
},
{
carbon: 30,
timestamp: 'mock-timestamp',
duration: 'mock-duration',
},
];
expect(aggregatedTree.outputs).toEqual(expectedOutputs);
const expectedAggregated = {
carbon:
tree.children['mocks-1'].outputs[0].carbon +
tree.children['mocks-2'].outputs[0].carbon +
tree.children['mocks-1'].outputs[1].carbon +
tree.children['mocks-2'].outputs[1].carbon,
};
expect(aggregatedTree.aggregated).toEqual(expectedAggregated);
});
});
});
Loading