Skip to content

Commit

Permalink
added missing tests, fixed client mistmactched api call and types
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnoble committed Jan 27, 2023
1 parent 89fffb5 commit b704e84
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/elasticsearch-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"srcMain": "src/index.ts",
"terascope": {
"enableTypedoc": true,
"testSuite": "search"
"testSuite": "elasticsearch"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ export class Client {
distributionMeta
);

const resp = await client.indices.getSettings(parsedParams);
const resp = await client.indices.getFieldMapping(parsedParams);

return _removeBody(resp);
},
Expand All @@ -580,7 +580,7 @@ export class Client {
* @returns IndicesGetSettingsResponse
*/
async getSettings(
params: ClientParams.IndicesGetSettingsParams
params: ClientParams.IndicesGetSettingsParams = {}
): Promise<ClientResponse.IndicesGetSettingsResponse> {
const parsedParams = methods.convertIndicesGetSettingsParams(
params as ClientParams.IndicesGetSettingsParams,
Expand Down Expand Up @@ -617,7 +617,7 @@ export class Client {
* can be an empty query
*/
async refresh(
params: ClientParams.IndicesRefreshParams
params: ClientParams.IndicesRefreshParams = {}
): Promise<ClientResponse.IndicesRefreshResponse> {
const parsedParams = methods.convertIndicesRefreshParams(
params as ClientParams.IndicesRefreshParams,
Expand All @@ -636,7 +636,7 @@ export class Client {
* can be an empty query
*/
async recovery(
params: ClientParams.IndicesRecoveryParams
params: ClientParams.IndicesRecoveryParams = {}
): Promise<ClientResponse.IndicesRecoveryResponse> {
const parsedParams = methods.convertIndicesRecoveryParams(
params as ClientParams.IndicesRecoveryParams,
Expand Down
29 changes: 16 additions & 13 deletions packages/elasticsearch-store/src/test-helpers/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ export async function upload(
return client.bulk(query);
}

export function createMappingFromDatatype(
client: Client,
dataType: DataType,
type = '_doc',
overrides = {}
) {
const metaData = getClientMetadata(client);
const mapping = dataType.toESMapping({ typeName: type, overrides, ...metaData });

return fixMappingRequest(client, { body: mapping }, false);
}

export async function populateIndex(
client: Client,
index: string,
Expand All @@ -103,21 +115,12 @@ export async function populateIndex(
},
};

const metaData = getClientMetadata(client);
const mapping = dataType.toESMapping({ typeName: type, overrides, ...metaData });

await client.indices.create(
fixMappingRequest(
client,
{
index,
wait_for_active_shards: 'all',
body: mapping,
},
false
)
const mapping = createMappingFromDatatype(
client, dataType, type, overrides
);

await client.indices.create(mapping);

const body = formatUploadData(index, records);

const results = await client.bulk({
Expand Down
1 change: 1 addition & 0 deletions packages/elasticsearch-store/src/utils/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function isElasticsearch8(client: Client): boolean {
return distribution === ElasticsearchDistribution.elasticsearch && parsedVersion === 8;
}

// TODO: move this logic over to datatype
export function fixMappingRequest(
client: Client, _params: Record<string, any>, isTemplate: boolean
): any {
Expand Down
177 changes: 156 additions & 21 deletions packages/elasticsearch-store/test/client-functions-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable jest/no-focused-tests */
import 'jest-extended';
import { DataEntity, debugLogger, cloneDeep } from '@terascope/utils';
import { ClientParams } from '@terascope/types';
import {
DataEntity, debugLogger, cloneDeep, get
} from '@terascope/utils';
import { ClientParams, ClientResponse } from '@terascope/types';
import {
createClient, getBaseClient, Client,
ElasticsearchTestHelpers
ElasticsearchTestHelpers,
isElasticsearch6,
} from '../src';

const {
Expand All @@ -17,6 +20,7 @@ const { data } = EvenDateData;

describe('creates client that exposes elasticsearch and opensearch functions', () => {
const index = 'wrapped_client_test';
const taskIndex = 'task_index_test';
const docType = '_doc';

const testLogger = debugLogger('create-client-test');
Expand All @@ -33,15 +37,18 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
beforeAll(async () => {
({ client } = await createClient(config, testLogger));

await cleanupIndex(client, index);
Promise.all([
cleanupIndex(client, index),
cleanupIndex(client, taskIndex)
]);

if (clientMetadata.version.split('.').length !== 3) {
throw new Error(`Expected version to follow semver format (major.minor.patch) got ${clientMetadata.version}`);
}

await upload(client, { index, type: docType }, data);
await waitForData(client, index, 1000);
});
}, 15000);

describe('info', () => {
it('should return info about the cluster', async () => {
Expand Down Expand Up @@ -804,7 +811,7 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
}
};

const resp = await client.reindex(params);
const resp = await client.reindex(params) as ClientResponse.ReindexCompletedResponse;

expect(resp.total).toBe(10);
expect(resp.created).toBe(10);
Expand All @@ -824,7 +831,7 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
}
};

const resp = await client.reindex(params);
const resp = await client.reindex(params) as ClientResponse.ReindexCompletedResponse;

expect(resp.total).toBe(10);
expect(resp.created).toBe(10);
Expand All @@ -848,7 +855,7 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
}
};

const resp = await client.reindex(params);
const resp = await client.reindex(params) as ClientResponse.ReindexCompletedResponse;

expect(resp.total).toBe(10);
expect(resp.created).toBe(10);
Expand All @@ -869,7 +876,7 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
}
};

const resp = await client.reindex(params);
const resp = await client.reindex(params) as ClientResponse.ReindexCompletedResponse;

expect(resp.total).toBe(1);
expect(resp.created).toBe(1);
Expand All @@ -896,7 +903,7 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
}
};

const resp = await client.reindex(params);
const resp = await client.reindex(params) as ClientResponse.ReindexCompletedResponse;

if (clientMetadata.distribution === 'elasticsearch') {
if (clientMetadata.majorVersion === 6) {
Expand Down Expand Up @@ -1576,18 +1583,114 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
});
});

// TODO: make tests for these
describe('indices.getMapping', () => {});
describe('indices.putMapping', () => {});
describe('indices.getFieldMapping', () => {});
describe('indices.getSettings', () => {});
describe('indices.putSettings', () => {});
describe('indices.refresh', () => {});
describe('indices.recovery', () => {});
describe('indices.validateQuery', () => {});
describe('indices.getMapping', () => {
it('should get a mapping from an index', async () => {
const resp = await client.indices.getMapping({ index });
const expectedKeys = Object.keys(data[0]);

let pathToProperties: string;

if (isElasticsearch6(client)) {
pathToProperties = `${index}.mappings.${docType}.properties`;
} else {
pathToProperties = `${index}.mappings.properties`;
}
const keys = Object.keys(get(resp, pathToProperties, {}));

expectedKeys.forEach((key) => {
expect(keys).toInclude(key);
});
});
});

describe('indices.getFieldMapping', () => {
it('should get the mapping for a field', async () => {
const field = 'uuid';
const resp = await client.indices.getFieldMapping({
index,
fields: [field]
});

expect(resp).toBeDefined();
});
});

describe('indices.putMapping', () => {
it('should update a mapping to an index', async () => {
const query: ClientParams.IndicesPutMappingParams = {
index,
body: {
properties: {
test: { type: 'keyword' }
}
}
};
// NOTE: this mutates the fields
const resp = await client.indices.putMapping(query);

expect(resp.acknowledged).toBeTrue();
});
});

describe('indices.getSettings', () => {
it('should fetch settings', async () => {
const resp = await client.indices.getSettings({ index });
const settings = get(resp, `${index}.settings.index`) as any;

describe('tasks.cancel', () => {});
describe('tasks.get', () => {});
expect(settings).toBeDefined();
expect(settings.uuid).toBeString();
});
});

describe('indices.putSettings', () => {
it('can add settings', async () => {
const resp = await client.indices.putSettings({
index,
body: {
settings: {
'index.max_result_window': 100000
}
}
});

expect(resp.acknowledged).toBeTrue();
});
});

describe('indices.refresh', () => {
it('can refresh an index', async () => {
const resp = await client.indices.refresh({ index });
expect(resp._shards).toBeDefined();
});
});

describe('indices.recovery', () => {
it('can do something', async () => {
const resp = await client.indices.recovery({ index });
expect(resp[index].shards).toBeArray();
});
});

describe('indices.validateQuery', () => {
it('can verify a query', async () => {
const goodQuery = {
index,
q: '_exists_:uuid'
};
const badQuery = {
index,
body: { foo: 'bar' }
};

const [goodResp, badResp] = await Promise.all([
client.indices.validateQuery(goodQuery),
client.indices.validateQuery(badQuery)
]);

expect(badResp.valid).toBeFalse();
expect(goodResp.valid).toBeTrue();
});
});

describe('tasks.list', () => {
it('should return tasks', async () => {
Expand All @@ -1613,4 +1716,36 @@ describe('creates client that exposes elasticsearch and opensearch functions', (
expect(resp.tasks).toBeDefined();
});
});

// we are combining as we need an action that is slow enough to persist
// reliably to make tests pass, and we don't want multiple scenarios of these
describe('tasks.get and tasks.cancel', () => {
it('should get and cancel a task', async () => {
const { task } = await client.reindex({
requests_per_second: 1,
wait_for_completion: false,
body: {
source: {
index,
size: 10
},
dest: {
index: taskIndex
}
}
}) as ClientResponse.ReindexTaskResponse;

const taskResp = await client.tasks.get({ task_id: task });

expect(taskResp.completed).toBeFalse();
expect(taskResp.task).toBeDefined();
expect(taskResp.task.action).toInclude('reindex');

const canceledTaskResp = await client.tasks.cancel({
task_id: task
});

expect(canceledTaskResp.nodes).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export interface IndicesPutMappingResponse extends i.IndicesResponseBase {
_shards: i.ShardStatistics
}

export interface IndicesPutSettingsResponse {
export interface IndicesPutSettingsResponse extends i.IndicesResponseBase {
_shards: i.ShardStatistics
}

Expand All @@ -213,7 +213,7 @@ export interface NodesStatsResponse {
_nodes?: i.NodeStatistics;
}

export interface ReindexResponse {
export interface ReindexCompletedResponse {
took: number;
timed_out: boolean;
total: number;
Expand All @@ -233,4 +233,10 @@ export interface ReindexResponse {
failures: any[]
}

export interface ReindexTaskResponse {
task: string
}

export type ReindexResponse = ReindexCompletedResponse | ReindexTaskResponse

export interface SearchResponse<T = Record<string, unknown>> extends i.SearchRecordResponse<T> {}

0 comments on commit b704e84

Please sign in to comment.