Skip to content

Commit

Permalink
fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 2, 2020
1 parent 7805cc1 commit 1a32a00
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
30 changes: 5 additions & 25 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,41 +198,21 @@ describe('createIndex', () => {

describe('queryEventsBySavedObject', () => {
test('should call cluster with proper arguments', async () => {
await clusterClientAdapter.queryEventsBySavedObject(
'index-name',
'saved-object-type',
'saved-object-id'
);
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('search', {
index: 'index-name',
body: {
query: {
bool: {
must: [
{ match: { 'kibana.saved_objects.type.keyword': 'saved-object-type' } },
{
match: {
'kibana.saved_objects.id.keyword': 'saved-object-id',
},
},
],
},
},
clusterClient.callAsInternalUser.mockResolvedValue({
hits: {
hits: [],
},
});
});

test('should allow pagination options', async () => {
await clusterClientAdapter.queryEventsBySavedObject(
'index-name',
'saved-object-type',
'saved-object-id',
{ from: 100, size: 10 }
{ page: 10, per_page: 10 }
);
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('search', {
index: 'index-name',
body: {
from: 100,
from: 90,
size: 10,
query: {
bool: {
Expand Down
11 changes: 4 additions & 7 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { Logger, ClusterClient } from '../../../../../src/core/server';
import { IEvent } from '../types';
import { FindOptionsType } from '../event_log_client';

export type EsClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
export type IClusterClientAdapter = PublicMethodsOf<ClusterClientAdapter>;
Expand All @@ -15,11 +16,6 @@ export interface ConstructorOpts {
clusterClient: EsClusterClient;
}

export interface QueryEventsOptions {
from: number;
size: number;
}

export class ClusterClientAdapter {
private readonly logger: Logger;
private readonly clusterClient: EsClusterClient;
Expand Down Expand Up @@ -117,15 +113,16 @@ export class ClusterClientAdapter {
index: string,
type: string,
id: string,
options: Partial<QueryEventsOptions> = {}
{ page, per_page: size }: FindOptionsType
): Promise<any[]> {
try {
const {
hits: { hits },
} = await this.callEs('search', {
index,
body: {
...options,
size,
from: (page - 1) * size,
query: {
bool: {
must: [
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/event_log/server/event_log_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ describe('EventLogStart', () => {
esContext.esNames.alias,
'saved-object-type',
'saved-object-id',
{}
{
page: 1,
per_page: 10,
}
);
});
});
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/event_log/server/event_log_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ export class EventLogClient implements IEventLogClient {
async findEventsBySavedObject(
type: string,
id: string,
options: Partial<FindOptionsType> = {}
options?: Partial<FindOptionsType>
): Promise<IEvent[]> {
const { per_page: size, page } = findOptionsSchema.validate(options);
await this.savedObjectsClient.get(type, id);
return (await this.esContext.esAdapter.queryEventsBySavedObject(
this.esContext.esNames.alias,
type,
id,
{ size, from: (page - 1) * size }
findOptionsSchema.validate(options ?? {})
)) as IEvent[];
}
}

0 comments on commit 1a32a00

Please sign in to comment.