Skip to content

Commit

Permalink
Adding tests for server libs
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Jan 2, 2017
1 parent 63a736a commit 6ede79b
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 51 deletions.
56 changes: 56 additions & 0 deletions src/core_plugins/metrics/server/lib/__test__/calculate_indices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect } from 'chai';
import { getParams, handleResponse } from '../calculate_indices';

describe('calculateIndices', () => {

describe('getParams', () => {
const req = {
payload: {
timerange: {
min: '2017-01-01T00:00:00.000Z',
max: '2017-01-03T23:59:59.000Z'
},
}
};

it('should return a valid param object', () => {
expect(getParams(req, 'metricbeat-*', '@timestamp')).to.eql({
index: 'metricbeat-*',
level: 'indices',
ignoreUnavailable: true,
body: {
fields: ['@timestamp'],
index_constraints: {
'@timestamp': {
max_value: { gte: '2017-01-03T23:59:59.000Z' },
min_value: { lte: '2017-01-01T00:00:00.000Z' }
}
}
}
});
});

});

describe('handleResponse', () => {
it('returns an array of indices', () => {
const resp = {
indices: {
'metricbeat-2017.01.01': {},
'metricbeat-2017.01.02': {},
'metricbeat-2017.01.03': {}
}
};
expect(handleResponse(resp)).to.eql([
'metricbeat-2017.01.01',
'metricbeat-2017.01.02',
'metricbeat-2017.01.03',
]);
});

it('returns an empty array if none found', () => {
const resp = { indices: { } };
expect(handleResponse(resp)).to.be.empty;
});
});
});
56 changes: 56 additions & 0 deletions src/core_plugins/metrics/server/lib/__test__/get_buckets_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect } from 'chai';
import getBucketsPath from '../get_buckets_path';

describe('getBucketsPath', () => {

const metrics = [
{ id: 1, type: 'derivative' },
{ id: 2, type: 'percentile', percent: '50' },
{ id: 3, type: 'percentile', percent: '20.0' },
{ id: 4, type: 'std_deviation', mode: 'raw' },
{ id: 5, type: 'std_deviation', mode: 'upper' },
{ id: 6, type: 'std_deviation', mode: 'lower' },
{ id: 7, type: 'sum_of_squares' },
{ id: 8, type: 'variance' },
{ id: 9, type: 'max' }
];

it('return path for derivative', () => {
expect(getBucketsPath(1, metrics)).to.equal('1[normalized_value]');
});

it('return path for percentile(50)', () => {
expect(getBucketsPath(2, metrics)).to.equal('2[50.0]');
});

it('return path for percentile(20.0)', () => {
expect(getBucketsPath(3, metrics)).to.equal('3[20.0]');
});

it('return path for std_deviation(raw)', () => {
expect(getBucketsPath(4, metrics)).to.equal('4[std_deviation]');
});

it('return path for std_deviation(upper)', () => {
expect(getBucketsPath(5, metrics)).to.equal('5[std_upper]');
});

it('return path for std_deviation(lower)', () => {
expect(getBucketsPath(6, metrics)).to.equal('6[std_lower]');
});

it('return path for sum_of_squares', () => {
expect(getBucketsPath(7, metrics)).to.equal('7[sum_of_squares]');
});

it('return path for variance', () => {
expect(getBucketsPath(8, metrics)).to.equal('8[variance]');
});

it('return path for basic metric', () => {
expect(getBucketsPath(9, metrics)).to.equal('9');
});


});

84 changes: 84 additions & 0 deletions src/core_plugins/metrics/server/lib/__test__/get_fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'chai';
import { getParams, handleResponse } from '../get_fields';

describe('getFields', () => {

describe('getParams', () => {

it('returns a valid params object', () => {
const req = { query: { index: 'metricbeat-*' } };
expect(getParams(req)).to.eql({
index: 'metricbeat-*',
fields: ['*'],
ignoreUnavailable: false,
allowNoIndices: false,
includeDefaults: true
});
});

});

describe('handleResponse', () => {
it('returns a valid response', () => {
const resp = {
'foo': {
'mappings': {
'bar': {
'@timestamp': {
'full_name': '@timestamp',
'mapping': {
'@timestamp': {
'type': 'date'
}
}
}
}
}
},
'twitter': {
'mappings': {
'tweet': {
'message': {
'full_name': 'message',
'mapping': {
'message': {
'type': 'text',
'fields': {
'keyword': {
'type': 'keyword',
'ignore_above': 256
}
}
}
}
},
'@timestamp': {
'full_name': '@timestamp',
'mapping': {
'@timestamp': {
'type': 'date'
}
}
},
'id.keyword': {
'full_name': 'id.keyword',
'mapping': {
'keyword': {
'type': 'keyword',
'ignore_above': 256
}
}
}
}
}
}
};
expect(handleResponse(resp)).to.eql([
{ name: '@timestamp', type: 'date' },
{ name: 'id.keyword', type: 'keyword' },
{ name: 'message', type: 'text' }
]);
});
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import getSiblingAggValue from '../get_sibling_agg_value';

describe('getSiblingAggValue', () => {
const row = {
test: {
max: 3,
std_deviation: 1.5,
std_deviation_bounds: {
upper: 2,
lower: 1
}
}
};

it('returns the value for std_deviation_bounds.upper', () => {
const metric = { id: 'test', type: 'std_deviation_bucket', mode: 'upper' };
expect(getSiblingAggValue(row, metric)).to.equal(2);
});

it('returns the value for std_deviation_bounds.lower', () => {
const metric = { id: 'test', type: 'std_deviation_bucket', mode: 'lower' };
expect(getSiblingAggValue(row, metric)).to.equal(1);
});

it('returns the value for std_deviation', () => {
const metric = { id: 'test', type: 'std_deviation_bucket', mode: 'raw' };
expect(getSiblingAggValue(row, metric)).to.equal(1.5);
});

it('returns the value for basic (max)', () => {
const metric = { id: 'test', type: 'max_bucket' };
expect(getSiblingAggValue(row, metric)).to.equal(3);
});

});
51 changes: 51 additions & 0 deletions src/core_plugins/metrics/server/lib/__test__/parse_settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from 'chai';
import parseSettings from '../parse_settings';

describe('parseSettings', () => {
it('returns the true for "true"', () => {
const settings = 'pad=true';
expect(parseSettings(settings)).to.eql({
pad: true,
});
});

it('returns the false for "false"', () => {
const settings = 'pad=false';
expect(parseSettings(settings)).to.eql({
pad: false,
});
});

it('returns the true for "true"', () => {
const settings = 'pad=true';
expect(parseSettings(settings)).to.eql({
pad: true,
});
});

it('returns the true for 1', () => {
const settings = 'pad=1';
expect(parseSettings(settings)).to.eql({
pad: true,
});
});

it('returns the false for 0', () => {
const settings = 'pad=0';
expect(parseSettings(settings)).to.eql({
pad: false,
});
});

it('returns the settings as an object', () => {
const settings = 'alpha=0.9 beta=0.4 gamma=0.2 period=5 pad=false type=add';
expect(parseSettings(settings)).to.eql({
alpha: 0.9,
beta: 0.4,
gamma: 0.2,
period: 5,
pad: false,
type: 'add'
});
});
});
73 changes: 73 additions & 0 deletions src/core_plugins/metrics/server/lib/__test__/series_agg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { expect } from 'chai';
import seriesAgg from '../series_agg';

describe('seriesAgg', () => {
const series = [
[[0,2],[1,1],[2,3]],
[[0,4],[1,2],[2,3]],
[[0,2],[1,1],[2,3]]
];

describe('basic', () => {
it('returns the series sum', () => {
expect(seriesAgg.sum(series)).to.eql([
[[0,8], [1,4], [2,9]]
]);
});

it('returns the series max', () => {
expect(seriesAgg.max(series)).to.eql([
[[0,4], [1,2], [2,3]]
]);
});

it('returns the series min', () => {
expect(seriesAgg.min(series)).to.eql([
[[0,2], [1,1], [2,3]]
]);
});

it('returns the series mean', () => {
expect(seriesAgg.mean(series)).to.eql([
[[0,(8 / 3)], [1,(4 / 3)], [2,3]]
]);
});
});

describe('overall', () => {
it('returns the series overall sum', () => {
expect(seriesAgg.overall_sum(series)).to.eql([
[[0,21], [1,21], [2,21]]
]);
});

it('returns the series overall max', () => {
expect(seriesAgg.overall_max(series)).to.eql([
[[0,4], [1,4], [2,4]]
]);
});

it('returns the series overall min', () => {
expect(seriesAgg.overall_min(series)).to.eql([
[[0,1], [1,1], [2,1]]
]);
});

it('returns the series overall mean', () => {
const value = ((8) + (4) + 9) / 3;
expect(seriesAgg.overall_avg(series)).to.eql([
[[0,value], [1,value], [2,value]]
]);
});

});

describe('cumlative sum', () => {
it('returns the series cumlative sum', () => {
expect(seriesAgg.cumlative_sum(series)).to.eql([
[[0,8], [1,12], [2,21]]
]);
});
});

});
Loading

0 comments on commit 6ede79b

Please sign in to comment.