Skip to content

Commit

Permalink
test: added test coverage for SCAN_CONTRACT_LIMIT
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Dec 24, 2024
1 parent d4a91f2 commit 76a0e9f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ describe('ByteCodeAnalyzer', () => {
const mockContractCallResponse = testConstants.MOCK_CONTRACT_CALL_RESPONSE;
const mockValidMirrorNodeUrl = 'mock-mirror-node.com';
const mockValidMirrorNodeUrlWeb3 = 'mock-mirror-node-web3.com';
const mockScanningLimit = 39;

beforeEach(() => {
byteCodeAnalyzer = new ByteCodeAnalyzer();
contractScannerService = new ContractScannerService(
mockValidMirrorNodeUrl,
mockValidMirrorNodeUrlWeb3
mockValidMirrorNodeUrlWeb3,
mockScanningLimit
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,40 @@ describe('ConfigService', () => {
expect(startingPoint).toBe(mockStartingPoint);
expect(mockRetrieveNextPointer).toHaveBeenCalled();
});

it('should return default value, 100, if SCAN_CONTRACT_LIMIT is undefined', async () => {
process.env.HEDERA_NETWORK = mockValidHederaNetwork;
process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl;
delete process.env.SCAN_CONTRACT_LIMIT;

const configService = new ConfigService();

expect(configService.getScanContractLimit()).toEqual(100);
});

it('should return dynamic SCAN_CONTRACT_LIMIT value', async () => {
process.env.HEDERA_NETWORK = mockValidHederaNetwork;
process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl;

const expectedLimit = 36;
process.env.SCAN_CONTRACT_LIMIT = expectedLimit.toString();

const configService = new ConfigService();

expect(configService.getScanContractLimit()).toEqual(expectedLimit);
});

it('should throw an error if SCAN_CONTRACT_LIMIT is set to invalid values', async () => {
process.env.HEDERA_NETWORK = mockValidHederaNetwork;
process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl;

const invalidLimits = ['-3', '369', 'not a number'];
invalidLimits.forEach((limit) => {
process.env.SCAN_CONTRACT_LIMIT = limit;

expect(() => {
configService = new ConfigService();
}).toThrow(/SCAN_CONTRACT_LIMIT Is Not Properly Configured/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const mockedHelper = Helper as jest.Mocked<typeof Helper>;
describe('ContractScannerService', () => {
const mockValidMirrorNodeUrl = 'mock-mirror-node.com';
const mockValidMirrorNodeUrlWeb3 = 'mock-mirror-node-web3.com';
const mockScanningLimit = 39;

let contractScannerService: ContractScannerService;

Expand All @@ -53,7 +54,8 @@ describe('ContractScannerService', () => {

contractScannerService = new ContractScannerService(
mockValidMirrorNodeUrl,
mockValidMirrorNodeUrlWeb3
mockValidMirrorNodeUrlWeb3,
mockScanningLimit
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import { Helper } from '../../../src/utils/helper';

describe('Helper', () => {
describe('buildUrl', () => {
const mockNext =
'/api/v1/contracts?limit=100&order=asc&contract.id=gt:0.0.5294198';
const mockScanningLimit = 39;

it('Should build a default next url', () => {
const expectedDefaultNext = '/api/v1/contracts?limit=100&order=asc';
const defaultNext = Helper.buildUrl(null);
expect(defaultNext).toEqual(expectedDefaultNext);
});

it('Should return next link if provided', () => {
const nextLink = Helper.buildUrl(mockNext);
expect(nextLink).toEqual(mockNext);
});

it('Should return next link modified with scanningLimit if provided', () => {
const expectedNextLink = mockNext.replace(
'100',
mockScanningLimit.toString()
);

const nextLink = Helper.buildUrl(mockNext, mockScanningLimit);
expect(nextLink).toEqual(expectedNextLink);
});
});
});

0 comments on commit 76a0e9f

Please sign in to comment.