Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🚨 Add new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Jan 26, 2024
1 parent c2d00ed commit f28f824
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const { Signals, Logger } = require('lisk-service-framework');

const config = require('../../config');

const { requestIndexer, getToday, getBlocks } = require('../helpers');
const { requestIndexer, getToday, getBlocks } = require('.');

const moment = MomentRange.extendMoment(Moment);
const DATE_FORMAT = config.excel.dateFormat;
Expand Down Expand Up @@ -49,8 +49,9 @@ const checkIfIndexReadyForInterval = async interval => {

// Requested history for only until yesterday and blockchain index can already serve the information
const [, toDate] = interval.split(':');
const to = moment(toDate, DATE_FORMAT);
const to = moment(toDate, DATE_FORMAT).endOf('day');
const today = moment(getToday(), DATE_FORMAT);

if (to < today) {
const response = await getBlocks({ height: indexStatus.lastIndexedBlockHeight });
const [lastIndexedBlock] = response.data;
Expand All @@ -60,7 +61,7 @@ const checkIfIndexReadyForInterval = async interval => {
}

// Allow job scheduling if the last few blocks have not been indexed yet
if (indexStatus.chainLength - indexStatus.numBlocksIndexed <= 5) {
if (indexStatus.chainLength - indexStatus.numBlocksIndexed <= 10) {
return true;
}
} catch (err) {
Expand All @@ -72,5 +73,6 @@ const checkIfIndexReadyForInterval = async interval => {
};

module.exports = {
getIndexStatus,
checkIfIndexReadyForInterval,
};
2 changes: 1 addition & 1 deletion services/export/shared/transactionsExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const {
getTransactions,
} = require('./helpers');

const { checkIfIndexReadyForInterval } = require('./utils/ready');
const { checkIfIndexReadyForInterval } = require('./helpers/ready');

const partials = FilesystemCache(config.cache.partials);
const staticFiles = FilesystemCache(config.cache.exports);
Expand Down
316 changes: 316 additions & 0 deletions services/export/tests/unit/shared/helpers/ready.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
/*
* LiskHQ/lisk-service
* Copyright © 2024 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const { resolve } = require('path');

const mockedRequestFilePath = resolve(`${__dirname}/../../../../shared/helpers/request`);
const mockedHelpersPath = resolve(`${__dirname}/../../../../shared/helpers`);

describe('Test getIndexStatus method', () => {
it('should return the index status', async () => {
const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23827824,
lastIndexedBlockHeight: 23395523,
chainLength: 436833,
numBlocksIndexed: 4532,
percentageIndexed: 1.03,
isIndexingInProgress: true,
},
meta: {
lastUpdate: 1706251273,
},
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});

const { getIndexStatus } = require('../../../../shared/helpers/ready');
const response = await getIndexStatus();

expect(response).toEqual(mockIndexStatus);
});
});

describe('Test checkIfIndexReadyForInterval method', () => {
describe('when indexing is at 100 percent', () => {
beforeEach(() => {
jest.resetModules();

const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23827887,
lastIndexedBlockHeight: 23827887,
chainLength: 436896,
numBlocksIndexed: 436896,
percentageIndexed: 100,
isIndexingInProgress: false,
},
meta: {
lastUpdate: 1706251915,
},
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});
});

it('should return true interval is the day of re-genesis', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-05:2023-12-05');
expect(got).toEqual(want);
});

it('should return true interval starts on the day of re-genesis and is in the future', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-05:2023-12-31');
expect(got).toEqual(want);
});

it('should return true interval starts before the day of re-genesis and is in the future', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-04:2023-12-31');
expect(got).toEqual(want);
});
});

describe('when indexing is below 100 percent', () => {
beforeEach(() => {
jest.resetModules();

const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23827963,
lastIndexedBlockHeight: 23631326,
chainLength: 436972,
numBlocksIndexed: 15523,
percentageIndexed: 55,
isIndexingInProgress: true,
},
meta: { lastUpdate: 1706252683 },
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});

jest.mock(mockedHelpersPath, () => {
const actual = jest.requireActual(mockedHelpersPath);
return {
...actual,
getToday() {
return '2024-01-26';
},
getBlocks() {
// timestamp for block at height 23631326: Wednesday, January 3, 2024 7:59:20 AM
return { data: [{ timestamp: 1704268760 }] };
},
};
});
});

it('should return true when interval ends before the lastIndexedBlockHeight timestamp', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-05:2023-12-31');
expect(got).toEqual(want);
});

it('should return false when interval ends the same day but after the lastIndexedBlockHeight timestamp', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = false;

const got = await checkIfIndexReadyForInterval('2023-12-05:2024-01-03');
expect(got).toEqual(want);
});

it('should return false when interval ends after the lastIndexedBlockHeight timestamp', async () => {
const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = false;

const got = await checkIfIndexReadyForInterval('2023-12-04:2024-01-26');
expect(got).toEqual(want);
});
});

describe('when indexing is around 99.99 percent', () => {
beforeEach(() => jest.resetModules());

it('should return true when interval ends today and the indexing lags by 2 blocks', async () => {
const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23828082,
lastIndexedBlockHeight: 23828080,
chainLength: 437091,
numBlocksIndexed: 437089,
percentageIndexed: 99.99,
isIndexingInProgress: false,
},
meta: { lastUpdate: 1706253875 },
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});

jest.mock(mockedHelpersPath, () => {
const actual = jest.requireActual(mockedHelpersPath);
return {
...actual,
getToday() {
return '2024-01-26';
},
getBlocks() {
// timestamp for block at height 23828080: Friday, January 26, 2024 7:24:10 AM
return { data: [{ timestamp: 1706253850 }] };
},
};
});

const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-05:2024-01-26');
expect(got).toEqual(want);
});

it('should return true when interval ends today and the indexing lags by 10 blocks', async () => {
const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23828082,
lastIndexedBlockHeight: 23828072,
chainLength: 437091,
numBlocksIndexed: 437081,
percentageIndexed: 99.99,
isIndexingInProgress: false,
},
meta: { lastUpdate: 1706253875 },
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});

jest.mock(mockedHelpersPath, () => {
const actual = jest.requireActual(mockedHelpersPath);
return {
...actual,
getToday() {
return '2024-01-26';
},
getBlocks() {
// timestamp for block at height 23828072: Friday, January 26, 2024 7:22:50 AM
return { data: [{ timestamp: 1706253770 }] };
},
};
});

const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = true;

const got = await checkIfIndexReadyForInterval('2023-12-05:2024-01-26');
expect(got).toEqual(want);
});

it('should return false when interval ends today and the indexing lags by 100 blocks', async () => {
const mockIndexStatus = {
data: {
genesisHeight: 23390992,
lastBlockHeight: 23828082,
lastIndexedBlockHeight: 23827982,
chainLength: 437091,
numBlocksIndexed: 436991,
percentageIndexed: 99.97,
isIndexingInProgress: false,
},
meta: { lastUpdate: 1706253875 },
};

jest.mock(mockedRequestFilePath, () => {
const actual = jest.requireActual(mockedRequestFilePath);
return {
...actual,
requestIndexer() {
return mockIndexStatus;
},
};
});

jest.mock(mockedHelpersPath, () => {
const actual = jest.requireActual(mockedHelpersPath);
return {
...actual,
getToday() {
return '2024-01-26';
},
getBlocks() {
// timestamp for block at height 23827982: Friday, January 26, 2024 7:07:50 AM
return { data: [{ timestamp: 1706252870 }] };
},
};
});

const { checkIfIndexReadyForInterval } = require('../../../../shared/helpers/ready');
const want = false;

const got = await checkIfIndexReadyForInterval('2023-12-05:2024-01-26');
expect(got).toEqual(want);
});
});
});

0 comments on commit f28f824

Please sign in to comment.