From a45c450610c6c15dd12abcb90286a6edeaccae23 Mon Sep 17 00:00:00 2001 From: catalinfaurpaul Date: Mon, 23 Oct 2023 23:29:13 +0300 Subject: [PATCH] add scresults.spec.ts --- .../services/scresults.e2e-spec.ts | 100 ---------- .../services/api.config.spec.ts} | 0 .../services/caching.spec.ts} | 0 src/test/unit/services/scresults.spec.ts | 185 ++++++++++++++++++ 4 files changed, 185 insertions(+), 100 deletions(-) delete mode 100644 src/test/integration/services/scresults.e2e-spec.ts rename src/test/{integration/services/api.config.e2e-spec.ts => unit/services/api.config.spec.ts} (100%) rename src/test/{integration/services/caching.e2e-spec.ts => unit/services/caching.spec.ts} (100%) create mode 100644 src/test/unit/services/scresults.spec.ts diff --git a/src/test/integration/services/scresults.e2e-spec.ts b/src/test/integration/services/scresults.e2e-spec.ts deleted file mode 100644 index 5baedd3ae..000000000 --- a/src/test/integration/services/scresults.e2e-spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { Test } from "@nestjs/testing"; -import '@multiversx/sdk-nestjs-common/lib/utils/extensions/jest.extensions'; -import { PublicAppModule } from "src/public.app.module"; -import { SmartContractResultFilter } from "src/endpoints/sc-results/entities/smart.contract.result.filter"; -import { SmartContractResultService } from "src/endpoints/sc-results/scresult.service"; -import { SmartContractResult } from "src/endpoints/sc-results/entities/smart.contract.result"; - -describe('Scresults Service', () => { - let scResultsService: SmartContractResultService; - let accountAddress: string; - let scHash: string; - - beforeAll(async () => { - const moduleRef = await Test.createTestingModule({ - imports: [PublicAppModule], - }).compile(); - - scResultsService = moduleRef.get(SmartContractResultService); - - const scResults = await scResultsService.getScResults({ from: 0, size: 1 }, new SmartContractResultFilter()); - expect(scResults).toHaveLength(1); - - const scResult = scResults[0]; - accountAddress = scResult.sender; - scHash = scResults[0].hash; - }); - - describe('Scresults list', () => { - it('scresults should have hash, nonce and timestamp', async () => { - const scResultFilter = new SmartContractResultFilter(); - scResultFilter.originalTxHashes = ['f01660479c8481a1c07e78508898130e45dc8657bf2fc5c2f377623eb18f734d']; - - const results = await scResultsService.getScResults({ from: 0, size: 1 }, new SmartContractResultFilter()); - - for (const result of results) { - expect(result).toHaveStructure(Object.keys(new SmartContractResult())); - } - }); - - it(`should return a list with 25 scresults`, async () => { - const results = await scResultsService.getScResults({ from: 0, size: 25 }, new SmartContractResultFilter()); - - expect(results).toHaveLength(25); - - for (const result of results) { - expect(result).toHaveStructure(Object.keys(new SmartContractResult())); - } - }); - - it(`should return a list with 50 scresults`, async () => { - const results = await scResultsService.getScResults({ from: 0, size: 50 }, new SmartContractResultFilter()); - - expect(results).toHaveLength(50); - - for (const result of results) { - expect(result).toHaveStructure(Object.keys(new SmartContractResult())); - } - }); - }); - - describe('Scresults filters', () => { - it(`should return a list of scresults for an account`, async () => { - const results = await scResultsService.getAccountScResults(accountAddress, { from: 0, size: 25 }); - expect(results).toBeInstanceOf(Array); - - for (const result of results) { - expect(result).toHaveStructure(Object.keys(new SmartContractResult())); - expect([result.sender, result.receiver]).toContain(accountAddress); - } - }); - }); - - describe('Scresults count', () => { - it(`should return smart contract count`, async () => { - const count = await scResultsService.getScResultsCount(); - expect(typeof count).toBe('number'); - }); - }); - - describe('Scresult Details', () => { - it(`should return a detailed scresult with hash`, async () => { - const result = await scResultsService.getScResult(scHash); - expect(result).toBeDefined(); - expect(result).toBeInstanceOf(Object); - expect(result?.hash).toStrictEqual(scHash); - }); - - it(`should throw 'Smart contract result not found' error`, async () => { - const scResult = await scResultsService.getScResult(scHash + 'a'); - expect(scResult).toBeUndefined(); - }); - - describe('Get Results of a smart contract count', () => { - it('should return sc results count', async () => { - const count = await scResultsService.getAccountScResultsCount(accountAddress); - expect(typeof count).toBe('number'); - }); - }); - }); -}); diff --git a/src/test/integration/services/api.config.e2e-spec.ts b/src/test/unit/services/api.config.spec.ts similarity index 100% rename from src/test/integration/services/api.config.e2e-spec.ts rename to src/test/unit/services/api.config.spec.ts diff --git a/src/test/integration/services/caching.e2e-spec.ts b/src/test/unit/services/caching.spec.ts similarity index 100% rename from src/test/integration/services/caching.e2e-spec.ts rename to src/test/unit/services/caching.spec.ts diff --git a/src/test/unit/services/scresults.spec.ts b/src/test/unit/services/scresults.spec.ts new file mode 100644 index 000000000..eaf14af74 --- /dev/null +++ b/src/test/unit/services/scresults.spec.ts @@ -0,0 +1,185 @@ +import { Test } from "@nestjs/testing"; +import { AssetsService } from "src/common/assets/assets.service"; +import { QueryPagination } from "src/common/entities/query.pagination"; +import { IndexerService } from "src/common/indexer/indexer.service"; +import { SmartContractResult } from "src/endpoints/sc-results/entities/smart.contract.result"; +import { SmartContractResultService } from "src/endpoints/sc-results/scresult.service"; +import { TransactionActionService } from "src/endpoints/transactions/transaction-action/transaction.action.service"; + +describe('TagService', () => { + let service: SmartContractResultService; + let indexerService: IndexerService; + let transactionActionService: TransactionActionService; + + beforeEach(async () => { + const moduleRef = await Test.createTestingModule({ + providers: [ + SmartContractResultService, + { + provide: IndexerService, useValue: { + getScResults: jest.fn(), + getScResult: jest.fn(), + getScResultsCount: jest.fn(), + getAccountScResults: jest.fn(), + getAccountScResultsCount: jest.fn(), + }, + }, + { + provide: TransactionActionService, useValue: { + getTransactionAction: jest.fn(), + }, + }, + { + provide: AssetsService, useValue: { + getAllAccountAssets: jest.fn(), + }, + }, + ], + }).compile(); + + service = moduleRef.get(SmartContractResultService); + indexerService = moduleRef.get(IndexerService); + transactionActionService = moduleRef.get(TransactionActionService); + }); + + it('service should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('getAccountScResultsCount', () => { + it('should return account smart contract results count', async () => { + const address = 'erd1qga7ze0l03chfgru0a32wxqf2226nzrxnyhzer9lmudqhjgy7ycqjjyknz'; + jest.spyOn(indexerService, 'getAccountScResultsCount').mockResolvedValue(256); + + const result = await service.getAccountScResultsCount(address); + + expect(result).toStrictEqual(256); + expect(indexerService.getAccountScResultsCount).toHaveBeenCalledWith(address); + expect(indexerService.getAccountScResultsCount).toHaveBeenCalledTimes(1); + }); + }); + + describe('getScResultsCount', () => { + it('should return total smart contracts count', async () => { + jest.spyOn(indexerService, 'getScResultsCount').mockResolvedValue(10000); + + const result = await service.getScResultsCount(); + + expect(result).toStrictEqual(10000); + expect(indexerService.getScResultsCount).toHaveBeenCalledTimes(1); + }); + }); + + describe('getScResult', () => { + it('should return smart contract details for a given smart contract hash', async () => { + const scHash = '3eda9575e66505051f8603b40489781b221d709d9ac23ab8e13d0234eddea781'; + const mockIndexerScResult = { + scHash: '3eda9575e66505051f8603b40489781b221d709d9ac23ab8e13d0234eddea781', + nonce: 0, + gasLimit: '0', + gasPrice: '1000000000', + value: '0', + sender: 'erd1qqqqqqqqqqqqqpgql6dxenaameqn2uyyru3nmmpf7e95zmlxu7zskzpdcw', + receiver: 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax', + senderShard: 1, + receiverShard: 1, + data: 'RVNEVFRyYW5zZmVyQDQ4NTk1MDQ1MmQzNjMxMzkzNjM2MzFAOGIzYzVhNDFhMDRhNjliMjdm', + prevTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + originalTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + callType: '0', + timestamp: 1678983282, + tokens: ['HYPE-619661'], + esdtValues: ['2568446286792506323583'], + operation: 'ESDTTransfer', + }; + + jest.spyOn(indexerService, 'getScResult').mockResolvedValue(mockIndexerScResult); + jest.spyOn(transactionActionService, 'getTransactionAction').mockResolvedValue({ + category: 'esdtNft', + name: 'transfer', + description: 'Transfer', + arguments: { + transfers: [[Object]], + receiver: 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax', + functionName: undefined, + functionArgs: [], + }, + }); + + const result = await service.getScResult(scHash); + + expect(result).toBeInstanceOf(SmartContractResult); + expect(indexerService.getScResult).toHaveBeenCalledWith(scHash); + expect(indexerService.getScResult).toHaveBeenCalledTimes(1); + expect(transactionActionService.getTransactionAction).toHaveBeenCalledTimes(1); + }); + }); + + describe('getAccountScResults', () => { + const mockIndexerScResults = [{ + scHash: 'f9a553996e7d3cde70d99212eb2462e209df721db324fdc72139626746369883', + nonce: 0, + gasLimit: '0', + gasPrice: '1000000000', + value: '0', + sender: 'erd1qqqqqqqqqqqqqpgql6dxenaameqn2uyyru3nmmpf7e95zmlxu7zskzpdcw', + receiver: 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax', + senderShard: 1, + receiverShard: 1, + data: 'RVNEVFRyYW5zZmVyQDQ4NTk1MDQ1MmQzNjMxMzkzNjM2MzFAOGIzYzVhNDFhMDRhNjliMjdm', + prevTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + originalTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + callType: '0', + timestamp: 1678983282, + tokens: ['HYPE-619661'], + esdtValues: ['1000000000000000000'], + operation: 'ESDTTransfer', + }, + { + scHash: '6fc7c94e6fd113fd073e513d7e4f04d25bcbc4fefed8eea65748d18ed15b270d', + nonce: 0, + gasLimit: '0', + gasPrice: '1000000000', + value: '0', + sender: 'erd1qqqqqqqqqqqqqpgql6dxenaameqn2uyyru3nmmpf7e95zmlxu7zskzpdcw', + receiver: 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax', + senderShard: 1, + receiverShard: 1, + data: 'RVNEVFRyYW5zZmVyQDQ4NTk1MDQ1MmQzNjMxMzkzNjM2MzFAOGIzYzVhNDFhMDRhNjliMjdm', + prevTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + originalTxHash: 'f708864d802799353743f8703bffc87fa8167e46a522eb973fdfadedaa2bc9e0', + callType: '0', + timestamp: 1678983282, + tokens: ['HYPE-619661'], + esdtValues: ['123416452926529462'], + operation: 'ESDTTransfer', + }]; + + const mockTransactionAction = { + category: 'esdtNft', + name: 'transfer', + description: 'Transfer', + arguments: { + transfers: [[Object]], + receiver: 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax', + functionName: undefined, + functionArgs: [], + }, + }; + + it('should return account smart contract results', async () => { + const address = 'erd1ahauxqcaxvcksrcyyxzk5lrr3p8p8rjtayfue068nktrpjpvzers3dgvax'; + + jest.spyOn(indexerService, 'getAccountScResults').mockResolvedValue(mockIndexerScResults); + jest.spyOn(transactionActionService, 'getTransactionAction').mockResolvedValue(mockTransactionAction); + + const results = await service.getAccountScResults(address, new QueryPagination()); + + for (const result of results) { + expect(result).toBeInstanceOf(SmartContractResult); + expect(indexerService.getAccountScResults).toHaveBeenCalledTimes(1); + expect(transactionActionService.getTransactionAction).toHaveBeenCalledTimes(2); + } + }); + }); +});