From 061fffe9174f0a295d34358b2a99bf798510c1b6 Mon Sep 17 00:00:00 2001 From: nagdahimanshu Date: Fri, 10 Nov 2023 12:28:39 +0100 Subject: [PATCH] Add unit tests --- .../shared/dataService/business/generators.js | 3 + .../dataservice/business/generators.test.js | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 services/blockchain-indexer/tests/unit/shared/dataservice/business/generators.test.js diff --git a/services/blockchain-indexer/shared/dataService/business/generators.js b/services/blockchain-indexer/shared/dataService/business/generators.js index 5d2df562e1..a3e3b46d7f 100644 --- a/services/blockchain-indexer/shared/dataService/business/generators.js +++ b/services/blockchain-indexer/shared/dataService/business/generators.js @@ -67,4 +67,7 @@ module.exports = { reloadGeneratorsCache, getGenerators, getNumberOfGenerators, + + // For unit tests + getGeneratorsInfo, }; diff --git a/services/blockchain-indexer/tests/unit/shared/dataservice/business/generators.test.js b/services/blockchain-indexer/tests/unit/shared/dataservice/business/generators.test.js new file mode 100644 index 0000000000..bdb8cd8c5c --- /dev/null +++ b/services/blockchain-indexer/tests/unit/shared/dataservice/business/generators.test.js @@ -0,0 +1,101 @@ +/* + * LiskHQ/lisk-service + * Copyright © 2023 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. + * + */ +/* eslint-disable import/no-dynamic-require */ +const { resolve } = require('path'); + +const mockValidatorUtilsPath = resolve(`${__dirname}/../../../../../shared/utils/validator`); +const mockRequestPath = resolve(`${__dirname}/../../../../../shared/utils/request`); +const mockedGeneratorsPath = resolve( + `${__dirname}/../../../../../shared/dataService/business/generators`, +); +const mockedPosPath = resolve(`${__dirname}/../../../../../shared/dataService/business/pos`); + +beforeEach(() => { + jest.resetModules(); + jest.clearAllMocks(); +}); + +jest.mock('lisk-service-framework', () => { + const actual = jest.requireActual('lisk-service-framework'); + return { + ...actual, + DB: { + MySQL: { + getTableInstance: jest.fn(() => ({ + find: jest.fn(() => [ + { publicKey: 'fe50ee28b084414499465ff823e7d651c9d19d75d33a82c44b2a15e3dc62bac9' }, + ]), + })), + KVStore: { + ...actual.DB.MySQL.KVStore, + configureKeyValueTable: jest.fn(), + getKeyValueTable: jest.fn(), + }, + }, + }, + CacheRedis: jest.fn(), + CacheLRU: jest.fn(), + }; +}); + +describe('getGeneratorsInfo', () => { + it('should return generators list', async () => { + jest.mock(mockRequestPath, () => ({ + requestConnector: jest.fn(() => ({ + list: [ + { + address: 'lsky2j2fnmhxushe5ywvdw4ouvxg8s4aeo4a7bpxb', + nextAllocatedTime: 1699615180, + }, + ], + })), + })); + + jest.mock(mockValidatorUtilsPath, () => ({ + getNameByAddress: jest.fn(() => 'testGenerator_1'), + })); + + const { getGeneratorsInfo } = require(mockedGeneratorsPath); + const generators = await getGeneratorsInfo(); + const expectedResponse = [ + { + address: 'lsky2j2fnmhxushe5ywvdw4ouvxg8s4aeo4a7bpxb', + name: 'testGenerator_1', + nextAllocatedTime: 1699615180, + publicKey: 'fe50ee28b084414499465ff823e7d651c9d19d75d33a82c44b2a15e3dc62bac9', + }, + ]; + expect(generators.length).toBeGreaterThanOrEqual(1); + expect(generators).toEqual(expectedResponse); + }); +}); + +describe('getNumberOfGenerators', () => { + it('should return number of generators in one round', async () => { + jest.mock(mockedPosPath, () => ({ + getPosConstants: jest.fn(() => ({ + data: { + numberActiveValidators: 101, + numberStandbyValidators: 2, + }, + })), + })); + + const { getNumberOfGenerators } = require(mockedGeneratorsPath); + const numOfGenerators = await getNumberOfGenerators(); + expect(numOfGenerators).toBe(103); + }); +});