Skip to content

Commit

Permalink
Add tests for highlights endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez committed Jan 3, 2020
1 parent 1971e5c commit 1836ba3
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export const logColumnRT = rt.union([
rt.type({
columnId: rt.string,
field: rt.string,
value: valueRT,
value: rt.string,
highlights: rt.array(rt.string),
}),
rt.type({
columnId: rt.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ export class InfraLogEntriesDomain {
return {
columnId: column.fieldColumn.id,
field: column.fieldColumn.field,
value: doc.fields[column.fieldColumn.field],
value: stringify(doc.fields[column.fieldColumn.field]),
highlights: doc.highlights[column.fieldColumn.field] || [],
};
}
}
Expand Down
152 changes: 152 additions & 0 deletions x-pack/test/api_integration/apis/infra/log_entry_highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import expect from '@kbn/expect';
import { ascending, pairs } from 'd3-array';
import gql from 'graphql-tag';

import { pipe } from 'fp-ts/lib/pipeable';
import { identity } from 'fp-ts/lib/function';
import { fold } from 'fp-ts/lib/Either';

import {
createPlainError,
throwErrors,
} from '../../../../legacy/plugins/infra/common/runtime_types';

import {
LOG_ENTRIES_HIGHLIGHTS_PATH,
logEntriesHighlightsRequestRT,
logEntriesHighlightsResponseRT,
} from '../../../../legacy/plugins/infra/common/http_api';

import { FtrProviderContext } from '../../ftr_provider_context';
import { sharedFragments } from '../../../../legacy/plugins/infra/common/graphql/shared';
import { InfraTimeKey } from '../../../../legacy/plugins/infra/public/graphql/types';
Expand All @@ -29,14 +44,151 @@ const KEY_AFTER_END = {
tiebreaker: 0,
};

const COMMON_HEADERS = {
'kbn-xsrf': 'some-xsrf-token',
};

export default function({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const client = getService('infraOpsGraphQLClient');

describe('log highlight apis', () => {
before(() => esArchiver.load('infra/simple_logs'));
after(() => esArchiver.unload('infra/simple_logs'));

describe('/log_entries/highlights', () => {
describe('with the default source', () => {
before(() => esArchiver.load('empty_kibana'));
after(() => esArchiver.unload('empty_kibana'));

it('highlights built-in message column', async () => {
const { body } = await supertest
.post(LOG_ENTRIES_HIGHLIGHTS_PATH)
.set(COMMON_HEADERS)
.send(
logEntriesHighlightsRequestRT.encode({
sourceId: 'default',
startDate: KEY_BEFORE_START.time,
endDate: KEY_AFTER_END.time,
highlightTerms: ['message of document 0'],
})
)
.expect(200);

const logEntriesHighlightsResponse = pipe(
logEntriesHighlightsResponseRT.decode(body),
fold(throwErrors(createPlainError), identity)
);

expect(logEntriesHighlightsResponse.data).to.have.length(1);

const data = logEntriesHighlightsResponse.data[0];
const entries = data.entries;
const firstEntry = entries[0];
const lastEntry = entries[entries.length - 1];

// Finds expected entries
expect(entries).to.have.length(10);

// Cursors are set correctly
expect(firstEntry.cursor).to.eql(data.topCursor);
expect(lastEntry.cursor).to.eql(data.bottomCursor);

// Entries fall within range
// @kbn/expect doesn't have a `lessOrEqualThan` or `moreOrEqualThan` comparators
expect(firstEntry.cursor.time >= KEY_BEFORE_START.time).to.be(true);
expect(lastEntry.cursor.time <= KEY_AFTER_END.time).to.be(true);

// All entries contain the highlights
entries.forEach(entry => {
entry.columns.forEach(column => {
if ('message' in column && 'highlights' in column.message[0]) {
expect(column.message[0].highlights).to.eql(['message', 'of', 'document', '0']);
}
});
});
});

it('highlights field columns', async () => {
const { body } = await supertest
.post(LOG_ENTRIES_HIGHLIGHTS_PATH)
.set(COMMON_HEADERS)
.send(
logEntriesHighlightsRequestRT.encode({
sourceId: 'default',
startDate: KEY_BEFORE_START.time,
endDate: KEY_AFTER_END.time,
highlightTerms: ['generate_test_data/simple_logs'],
})
)
.expect(200);

const logEntriesHighlightsResponse = pipe(
logEntriesHighlightsResponseRT.decode(body),
fold(throwErrors(createPlainError), identity)
);

expect(logEntriesHighlightsResponse.data).to.have.length(1);

const entries = logEntriesHighlightsResponse.data[0].entries;

// Finds expected entries
expect(entries).to.have.length(50);

// All entries contain the highlights
entries.forEach(entry => {
entry.columns.forEach(column => {
if ('field' in column && 'highlights' in column && column.highlights.length > 0) {
// https://github.com/elastic/kibana/issues/49959
// expect(column.highlights).to.eql(['generate_test_data/simple_logs']);
expect(column.highlights).to.eql(['generate_test_data']);
}
});
});
});

it('applies the query as well as the highlight', async () => {
const { body } = await supertest
.post(LOG_ENTRIES_HIGHLIGHTS_PATH)
.set(COMMON_HEADERS)
.send(
logEntriesHighlightsRequestRT.encode({
sourceId: 'default',
startDate: KEY_BEFORE_START.time,
endDate: KEY_AFTER_END.time,
query: JSON.stringify({
multi_match: { query: 'host-a', type: 'phrase', lenient: true },
}),
highlightTerms: ['message'],
})
)
.expect(200);

const logEntriesHighlightsResponse = pipe(
logEntriesHighlightsResponseRT.decode(body),
fold(throwErrors(createPlainError), identity)
);

expect(logEntriesHighlightsResponse.data).to.have.length(1);

const entries = logEntriesHighlightsResponse.data[0].entries;

// Finds expected entries
expect(entries).to.have.length(25);

// All entries contain the highlights
entries.forEach(entry => {
entry.columns.forEach(column => {
if ('message' in column && 'highlights' in column.message[0]) {
expect(column.message[0].highlights).to.eql(['message', 'message']);
}
});
});
});
});
});

describe('logEntryHighlights', () => {
describe('with the default source', () => {
before(() => esArchiver.load('empty_kibana'));
Expand Down

0 comments on commit 1836ba3

Please sign in to comment.