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

Update Satellite ES to 8.4.0, ES-mock to 2.0.0 #3676

Merged
merged 1 commit into from
Oct 6, 2022
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
87 changes: 73 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/satellite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"homepage": "https://github.com/Seneca-CDOT/telescope/tree/master/src/satellite",
"dependencies": {
"@elastic/elasticsearch": "7.17.0",
"@elastic/elasticsearch-mock": "0.3.1",
"@elastic/elasticsearch": "8.4.0",
"@elastic/elasticsearch-mock": "2.0.0",
"@godaddy/terminus": "4.10.2",
"cors": "2.8.5",
"express": "4.17.3",
Expand Down
152 changes: 99 additions & 53 deletions src/satellite/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,72 +993,118 @@ describe('Redis()', () => {
});

describe('Elastic()', () => {
describe('Tests for regular Elastic()', () => {
test('Testing the name property which should be a string', async () => {
const client = Elastic();
let client;
let mock;

const clientInfo = await client.info();
beforeEach(() => {
client = Elastic();
mock = client.mock;
mock.clearAll();
});

expect(clientInfo.statusCode).toBe(200);
});
afterAll(() => {
mock.clearAll();
});

describe('Tests for mock Elastic()', () => {
let client;
let mock;
test('Should mock an API', async () => {
mock.add(
{
method: 'GET',
path: '/_cat/indices',
},
() => {
return { status: 'ok' };
}
);

beforeEach(() => {
client = Elastic();
mock = client.mock;
mock.clearAll();
});
const response = await client.cat.indices({}, { meta: true });
expect(response.body).toStrictEqual({ status: 'ok' });
expect(response.statusCode).toBe(200);
});

afterAll(() => {
mock.clearAll();
});
test('If an API is not mocked correctly, it should return a 404', async () => {
let response;

test('Should mock an API', async () => {
mock.add(
{
method: 'GET',
path: '/_cat/indices',
},
() => {
return { status: 'ok' };
}
);

const response = await client.cat.indices();
expect(response.body).toStrictEqual({ status: 'ok' });
expect(response.statusCode).toBe(200);
mock.add(
{
method: 'GET',
path: '/_cat/health',
querystring: { pretty: 'true' },
},
() => {
return { status: 'ok' };
}
);

try {
response = await client.cat.health();
} catch (err) {
expect(err instanceof errors.ResponseError).toBe(true);
expect(err.body).toStrictEqual({ error: 'Mock not found' });
expect(err.statusCode).toBe(404);
}

response = await client.cat.health({ pretty: true }, { meta: true });
expect(response.body).toStrictEqual({ status: 'ok' });
expect(response.statusCode).toBe(200);
});

test('mock.clearAll() will work for mocks that share the same client', async () => {
const mockResults1 = { text: 'first result' };
const mockResults2 = { text: 'second result' };

mock.add(
{
method: ['POST', 'GET'],
path: '/posts/_search',
},
() => {
return mockResults1;
}
);

let response = await client.search({
index: 'posts',
query: { match_all: {} },
});
expect(response).toStrictEqual(mockResults1);

test('If an API is not mocked correctly, it should return a 404', async () => {
let response;
// Adding a second mock without clearing
mock.add(
{
method: ['POST', 'GET'],
path: '/posts/_search',
},
() => {
return mockResults2;
}
);

mock.add(
{
method: 'GET',
path: '/_cat/health',
querystring: { pretty: 'true' },
},
() => {
return { status: 'ok' };
}
);

try {
response = await client.cat.health();
} catch (err) {
expect(err instanceof errors.ResponseError).toBe(true);
expect(err.body).toStrictEqual({ error: 'Mock not found' });
expect(err.statusCode).toBe(404);
response = await client.search({
index: 'posts',
query: { match_all: {} },
});
// Will not get the results of the new mock
expect(response).toStrictEqual(mockResults1);

// Clearing mocks before adding a new mock
mock.clearAll();
mock.add(
{
method: ['POST', 'GET'],
path: '/posts/_search',
},
() => {
return mockResults2;
}
);

response = await client.cat.health({ pretty: true });
expect(response.body).toStrictEqual({ status: 'ok' });
expect(response.statusCode).toBe(200);
response = await client.search({
index: 'posts',
query: { match_all: {} },
});
// Will get the results of the new mock as expected
expect(response).toStrictEqual(mockResults2);
});
});

Expand Down