Skip to content

Commit 7bfa27e

Browse files
ensure-index unit testing
supports one index (jobId_1_accountId_1_time_1)
1 parent 471c0ea commit 7bfa27e

File tree

5 files changed

+81
-27
lines changed

5 files changed

+81
-27
lines changed

2021-08-17-2021-08-17-logs.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

2021-08-17-2021-08-17-metadata.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/interface/cli/commands/offline-logs/ensure-index.cmd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const command = new Command({
77
command: "ensure-index",
88
parent: cmd,
99
description:
10-
"Checks whether a collection has an index and gives the user an option to add one. Adding an index may improve performance in some of the app operations.",
10+
"Checks whether a collection has indexes and gives the user an option to add them. Adding an index may improve performance of some of the read operations.",
1111
webDocs: {
1212
category: "Logs",
13-
title: "Ensures one or more offline-logs collections has an index",
13+
title: "Ensures one or more offline-logs collections has indexes",
1414
},
1515
builder: (yargs) =>
1616
yargs.example(
17-
'codefresh offline-logs ensure-index --uri "mongodb://192.168.99.100:27017" --db logs -c logs'
17+
'codefresh offline-logs ensure-index --uri "mongodb://192.168.99.100:27017" --db logs'
1818
),
1919
handler: async (argv) => {
2020
const { uri, db } = argv;
Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,77 @@
1-
const {Db, Collection} = require('mongodb');
1+
// const {Db, Collection} = require('mongodb');
22
const { Collection } = require('yaml/types');
3-
const { ensureIndex } = require('./utils')
3+
const utils = require('./utils')
44

55
describe("ensure-index", () => {
66
describe("ensureIndex", () => {
7-
it("gets user input from prompt and returns true/false", () => {
8-
Db.collection() = jest.fn();
9-
Collection.createIndexes() = jest.fn()
7+
utils.getUserInput = jest.fn()
8+
const mockCollection = {
9+
indexExists: jest.fn(),
10+
estimatedDocumentCount: jest.fn().mockReturnValue(700),
11+
createIndex: jest.fn(),
12+
};
13+
const mockDatabase = {
14+
collection() {
15+
return mockCollection;
16+
},
17+
};
1018

11-
const database = new Db('db');
19+
beforeEach(() => {
20+
mockCollection.indexExists.mockClear();
21+
mockCollection.estimatedDocumentCount.mockClear();
22+
mockCollection.createIndex.mockClear();
23+
utils.getUserInput.mockClear();
24+
});
25+
26+
it("index does not exists and user says yes on prompt", async () => {
27+
//setup
28+
mockCollection.indexExists.mockReturnValue(false);
29+
utils.getUserInput.mockReturnValue(true);
30+
const collection = 'whatever';
31+
const expectedIndexObj = { jobId: 1, accountId: 1, time: 1 }
32+
33+
//execute
34+
await utils.ensureIndex(mockDatabase, collection);
35+
36+
//check
37+
expect(mockCollection.indexExists).toBeCalledTimes(1);
38+
expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1");
39+
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1);
40+
expect(utils.getUserInput).toBeCalledTimes(1);
41+
expect(mockCollection.createIndex).toBeCalledTimes(1);
42+
expect(mockCollection.createIndex).toBeCalledWith(expectedIndexObj)
43+
});
44+
it("index does not exists and user says no on prompt", async () => {
45+
//setup
46+
mockCollection.indexExists.mockReturnValue(false);
47+
utils.getUserInput.mockReturnValue(false);
1248
const collection = 'whatever';
13-
ensureIndex(database, collection);
49+
50+
//execute
51+
await utils.ensureIndex(mockDatabase, collection);
52+
53+
//check
54+
expect(mockCollection.indexExists).toBeCalledTimes(1);
55+
expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1");
56+
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1);
57+
expect(utils.getUserInput).toBeCalledTimes(1);
58+
expect(mockCollection.createIndex).toBeCalledTimes(0);
59+
});
60+
it("index exists", async () => {
61+
//setup
62+
mockCollection.indexExists.mockReturnValue(true);
63+
const collection = 'whatever';
64+
65+
//execute
66+
await utils.ensureIndex(mockDatabase, collection);
67+
68+
//check
69+
expect(mockCollection.indexExists).toBeCalledTimes(1);
70+
expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1");
71+
expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(0);
72+
expect(utils.getUserInput).toBeCalledTimes(0);
73+
expect(mockCollection.createIndex).toBeCalledTimes(0);
1474
});
75+
1576
});
1677
});

lib/interface/cli/commands/offline-logs/utils.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ const writeLogsToFile = function (
3232
const checkRemnant = function (lowerBound, cutoffDateObj) {
3333
return Math.ceil(cutoffDateObj.diff(lowerBound, "days"));
3434
};
35-
35+
///////////////////////////////////////////////////////////////
3636
const ensureIndex = async function (database, collection) {
3737
const collectionObj = database.collection(collection);
38-
const indexName1 = "jobId_1_accountId_1";
39-
const indexName2 = "jobId_1_accountId_1_time_1";
38+
const indexName = "jobId_1_accountId_1_time_1";
4039

41-
const indexesExists = await Promise.all([
42-
collectionObj.indexExists(indexName1),
43-
collectionObj.indexExists(indexName2),
44-
]);
40+
const indexExist = await collectionObj.indexExists(indexName)
4541

46-
if (indexesExists[0] === true && indexesExists[1] === true) {
42+
if (indexExist) {
4743
console.info(
48-
`indexes '${indexName1}', '${indexName2}' already exists in collection ${collection}`
44+
`index '${indexName}' already exists in collection '${collection}'`
4945
);
5046
return;
5147
}
@@ -56,19 +52,18 @@ const ensureIndex = async function (database, collection) {
5652
do you want to create an index for it? (creating an index for a large collection may take a while,
5753
but can improve performance on future read operations) [yes/no] `;
5854

59-
const userInput = await getUserInput(queryStr);
55+
const userInput = await this.getUserInput(queryStr);
6056

6157
if (!userInput) {
6258
console.info(`skipping collection '${collection}'`);
6359
return;
6460
}
6561

66-
await collectionObj.createIndexes([
67-
{ key: { jobId: 1, accountId: 1 } },
68-
{ key: { jobId: 1, accountId: 1, time: 1 } },
69-
]);
62+
await collectionObj.createIndex(
63+
{ jobId: 1, accountId: 1, time: 1 },
64+
);
7065
console.info(
71-
`indexes were created for collection '${collection}'`
66+
`index was created for collection '${collection}'`
7267
);
7368
};
7469

0 commit comments

Comments
 (0)