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

Expose field formatters in kibana server #12625

Merged
merged 15 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import expect from 'expect.js';
import { BoolFormat } from '../boolean';

describe('Boolean Format', function () {

let boolean;
beforeEach(() => {
boolean = new BoolFormat();
});

[
{
input: 0,
expected: 'false'
},
{
input: 'no',
expected: 'false'
},
{
input: false,
expected: 'false'
},
{
input: 'false',
expected: 'false'
},
{
input: 1,
expected: 'true'
},
{
input: 'yes',
expected: 'true'
},
{
input: true,
expected: 'true'
},
{
input: 'true',
expected: 'true'
},
{
input: ' True ',//should handle trailing and mixed case
expected: 'true'
}
].forEach((test)=> {
it(`convert ${test.input} to boolean`, ()=> {
expect(boolean.convert(test.input)).to.be(test.expected);
});
});

it('does not convert non-boolean values, instead returning original value', ()=> {
const s = 'non-boolean value!!';
expect(boolean.convert(s)).to.be(s);
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import expect from 'expect.js';
import { BytesFormat } from '../bytes';

describe('BytesFormat', function () {

const config = {};
config['format:bytes:defaultPattern'] = '0,0.[000]b';
const getConfig = (key) => config[key];

it('default pattern', ()=> {
const formatter = new BytesFormat({}, getConfig);
expect(formatter.convert(5150000)).to.be('4.911MB');
});

it('custom pattern', ()=> {
const formatter = new BytesFormat({ pattern: '0,0b' }, getConfig);
expect(formatter.convert('5150000')).to.be('5MB');
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import expect from 'expect.js';
import { ColorFormat } from '../color';

describe('Color Format', function () {

describe('field is a number', () => {
it('should add colors if the value is in range', function () {
const colorer = new ColorFormat({
fieldType: 'number',
colors: [{
range: '100:150',
text: 'blue',
background: 'yellow'
}]
});
expect(colorer.convert(99, 'html')).to.eql('<span ng-non-bindable>99</span>');
expect(colorer.convert(100, 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">100</span></span>'
);
expect(colorer.convert(150, 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">150</span></span>'
);
expect(colorer.convert(151, 'html')).to.eql('<span ng-non-bindable>151</span>');
});

it('should not convert invalid ranges', function () {
const colorer = new ColorFormat({
fieldType: 'number',
colors: [{
range: '100150',
text: 'blue',
background: 'yellow'
}]
});
expect(colorer.convert(99, 'html')).to.eql('<span ng-non-bindable>99</span>');
});
});

describe('field is a string', () => {
it('should add colors if the regex matches', function () {
const colorer = new ColorFormat({
fieldType: 'string',
colors: [{
regex: 'A.*',
text: 'blue',
background: 'yellow'
}]
});

const converter = colorer.getConverterFor('html');
expect(converter('B', 'html')).to.eql('<span ng-non-bindable>B</span>');
expect(converter('AAA', 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">AAA</span></span>'
);
expect(converter('AB', 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">AB</span></span>'
);
expect(converter('a', 'html')).to.eql('<span ng-non-bindable>a</span>');

expect(converter('B', 'html')).to.eql('<span ng-non-bindable>B</span>');
expect(converter('AAA', 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">AAA</span></span>'
);
expect(converter('AB', 'html')).to.eql(
'<span ng-non-bindable><span style="color: blue;background-color: yellow;">AB</span></span>'
);
expect(converter('a', 'html')).to.eql('<span ng-non-bindable>a</span>');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import expect from 'expect.js';
import moment from 'moment-timezone';
import { DateFormat } from '../date';

describe('Date Format', function () {
let convert;
let mockConfig;

beforeEach(function () {
mockConfig = {};
mockConfig.dateFormat = 'MMMM Do YYYY, HH:mm:ss.SSS';
mockConfig['dateFormat:tz'] = 'Browser';
const getConfig = (key) => mockConfig[key];

const date = new DateFormat({}, getConfig);

convert = date.convert.bind(date);
});

it('decoding an undefined or null date should return an empty string', function () {
expect(convert(null)).to.be('-');
expect(convert(undefined)).to.be('-');
});

it('should clear the memoization cache after changing the date', function () {
function setDefaultTimezone() {
moment.tz.setDefault(mockConfig['dateFormat:tz']);
}
const time = 1445027693942;

mockConfig['dateFormat:tz'] = 'America/Chicago';
setDefaultTimezone();
const chicagoTime = convert(time);

mockConfig['dateFormat:tz'] = 'America/Phoenix';
setDefaultTimezone();
const phoenixTime = convert(time);

expect(chicagoTime).not.to.equal(phoenixTime);
});

it('should parse date math values', function () {
expect(convert('2015-01-01||+1M/d')).to.be('January 1st 2015, 00:00:00.000');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import expect from 'expect.js';
import { DurationFormat } from '../duration';

describe('Duration Format', function () {

test({
inputFormat: 'seconds',
outputFormat: 'humanize',
fixtures: [
{
input: -60,
output: 'minus a minute'
},
{
input: 60,
output: 'a minute'
},
{
input: 125,
output: '2 minutes'
}
]
});

test({
inputFormat: 'minutes',
outputFormat: 'humanize',
fixtures: [
{
input: -60,
output: 'minus an hour'
},
{
input: 60,
output: 'an hour'
},
{
input: 125,
output: '2 hours'
}
]
});

test({
inputFormat: 'minutes',
outputFormat: 'asHours',
fixtures: [
{
input: -60,
output: '-1.00'
},
{
input: 60,
output: '1.00'
},
{
input: 125,
output: '2.08'
}
]
});

test({
inputFormat: 'seconds',
outputFormat: 'asSeconds',
outputPrecision: 0,
fixtures: [
{
input: -60,
output: '-60'
},
{
input: 60,
output: '60'
},
{
input: 125,
output: '125'
}
]
});

test({
inputFormat: 'seconds',
outputFormat: 'asSeconds',
outputPrecision: 2,
fixtures: [
{
input: -60,
output: '-60.00'
},
{
input: -32.333,
output: '-32.33'
},
{
input: 60,
output: '60.00'
},
{
input: 125,
output: '125.00'
}
]
});

function test({ inputFormat, outputFormat, outputPrecision, fixtures }) {
fixtures.forEach((fixture) => {
const input = fixture.input;
const output = fixture.output;
it(`should format ${input} ${inputFormat} through ${outputFormat}${outputPrecision ? `, ${outputPrecision} decimals` : ''}`, () => {
const duration = new DurationFormat({ inputFormat, outputFormat, outputPrecision });
expect(duration.convert(input)).to.eql(output);
});
});
}
});
19 changes: 19 additions & 0 deletions src/core_plugins/kibana/common/field_formats/types/__tests__/ip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import expect from 'expect.js';
import { IpFormat } from '../ip';

describe('IP Address Format', function () {
let ip;
beforeEach(function () {
ip = new IpFormat();
});

it('converts a value from a decimal to a string', function () {
expect(ip.convert(1186489492)).to.be('70.184.100.148');
});

it('converts null and undefined to -', function () {
expect(ip.convert(null)).to.be('-');
expect(ip.convert(undefined)).to.be('-');
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import expect from 'expect.js';
import { NumberFormat } from '../number';

describe('NumberFormat', function () {

const config = {};
config['format:number:defaultPattern'] = '0,0.[000]';
const getConfig = (key) => config[key];

it('default pattern', ()=> {
const formatter = new NumberFormat({}, getConfig);
expect(formatter.convert(12.345678)).to.be('12.346');
});

it('custom pattern', ()=> {
const formatter = new NumberFormat({ pattern: '0,0' }, getConfig);
expect(formatter.convert('12.345678')).to.be('12');
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import expect from 'expect.js';
import { PercentFormat } from '../percent';

describe('PercentFormat', function () {

const config = {};
config['format:percent:defaultPattern'] = '0,0.[000]%';
const getConfig = (key) => config[key];

it('default pattern', ()=> {
const formatter = new PercentFormat({}, getConfig);
expect(formatter.convert(0.99999)).to.be('99.999%');
});

it('custom pattern', ()=> {
const formatter = new PercentFormat({ pattern: '0,0%' }, getConfig);
expect(formatter.convert('0.99999')).to.be('100%');
});

});
Loading