From 7290a06828bd01ef51b0aa95c60b81ae5b9477e9 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Wed, 13 Nov 2024 15:06:45 +1300 Subject: [PATCH] Fix some tests --- .../src/indexer/dynamic-ds.service.spec.ts | 43 +++++-- .../src/indexer/fetch.service.spec.ts | 117 +++++++++--------- .../src/indexer/project.service.spec.ts | 12 +- 3 files changed, 94 insertions(+), 78 deletions(-) diff --git a/packages/node-core/src/indexer/dynamic-ds.service.spec.ts b/packages/node-core/src/indexer/dynamic-ds.service.spec.ts index 0f4d0bc3d7..fb3dab6f0e 100644 --- a/packages/node-core/src/indexer/dynamic-ds.service.spec.ts +++ b/packages/node-core/src/indexer/dynamic-ds.service.spec.ts @@ -14,6 +14,7 @@ class TestDynamicDsService extends DynamicDsService[number], 'name'> & {startBlock?: number}>( templateName: string, startBlock?: number | undefined @@ -42,7 +43,7 @@ const mockMetadata = (initData: DatasourceParams[] = []) => { describe('DynamicDsService', () => { let service: TestDynamicDsService; const project = { - templates: [{name: 'TestTemplate'}], + templates: [{name: 'Test'}], } as any as ISubqueryProject; beforeEach(() => { @@ -52,7 +53,9 @@ describe('DynamicDsService', () => { it('loads all datasources and params when init', async () => { await service.init(mockMetadata([testParam1])); - await expect(service.getDynamicDatasources()).resolves.toEqual([testParam1]); + await expect(service.getDynamicDatasources()).resolves.toEqual([ + {/*name: 'Test',*/ startBlock: testParam1.startBlock}, + ]); expect((service as any)._datasourceParams).toEqual([testParam1]); }); @@ -66,7 +69,10 @@ describe('DynamicDsService', () => { expect((service as any)._datasourceParams).toEqual([testParam1, testParam2]); await expect(meta.find('dynamicDatasources')).resolves.toEqual([testParam1, testParam2]); - await expect(service.getDynamicDatasources()).resolves.toEqual([testParam1, testParam2]); + await expect(service.getDynamicDatasources()).resolves.toEqual([ + {startBlock: testParam1.startBlock}, + {startBlock: testParam2.startBlock}, + ]); }); it('resets dynamic datasources', async () => { @@ -76,7 +82,10 @@ describe('DynamicDsService', () => { await service.resetDynamicDatasource(2); await expect(meta.find('dynamicDatasources')).resolves.toEqual([testParam1, testParam2]); - await expect(service.getDynamicDatasources()).resolves.toEqual([testParam1, testParam2]); + await expect(service.getDynamicDatasources()).resolves.toEqual([ + {startBlock: testParam1.startBlock}, + {startBlock: testParam2.startBlock}, + ]); }); it('getDynamicDatasources with force reloads from metadata', async () => { @@ -85,19 +94,27 @@ describe('DynamicDsService', () => { meta.set('dynamicDatasources', [testParam1, testParam2, testParam3, testParam4]); - await expect(service.getDynamicDatasources()).resolves.toEqual([testParam1, testParam2]); + await expect(service.getDynamicDatasources()).resolves.toEqual([ + {startBlock: testParam1.startBlock}, + {startBlock: testParam2.startBlock}, + ]); await expect(service.getDynamicDatasources(true)).resolves.toEqual([ - testParam1, - testParam2, - testParam3, - testParam4, + {startBlock: testParam1.startBlock}, + {startBlock: testParam2.startBlock}, + {startBlock: testParam3.startBlock}, + {startBlock: testParam4.startBlock}, + ]); + await expect(service.getDynamicDatasources()).resolves.toEqual([ + {startBlock: testParam1.startBlock}, + {startBlock: testParam2.startBlock}, + {startBlock: testParam3.startBlock}, + {startBlock: testParam4.startBlock}, ]); - await expect(service.getDynamicDatasources()).resolves.toEqual([testParam1, testParam2, testParam3, testParam4]); }); it('can find a template and cannot mutate the template', () => { - const template1 = service.getTemplate('TestTemplate', 1); - const template2 = service.getTemplate('TestTemplate', 2); + const template1 = service.getTemplate('Test', 1); + const template2 = service.getTemplate('Test', 2); expect(template1.startBlock).toEqual(1); expect((template1 as any).name).toBeUndefined(); @@ -106,6 +123,6 @@ describe('DynamicDsService', () => { expect((template2 as any).name).toBeUndefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - expect(project.templates![0]).toEqual({name: 'TestTemplate'}); + expect(project.templates![0]).toEqual({name: 'Test'}); }); }); diff --git a/packages/node-core/src/indexer/fetch.service.spec.ts b/packages/node-core/src/indexer/fetch.service.spec.ts index 5ff7a68998..690092c86c 100644 --- a/packages/node-core/src/indexer/fetch.service.spec.ts +++ b/packages/node-core/src/indexer/fetch.service.spec.ts @@ -1,15 +1,9 @@ // Copyright 2020-2024 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 -import { EventEmitter2 } from '@nestjs/event-emitter'; -import { SchedulerRegistry } from '@nestjs/schedule'; -import { - BaseCustomDataSource, - BaseDataSource, - BaseHandler, - BaseMapping, - DictionaryQueryEntry, -} from '@subql/types-core'; +import {EventEmitter2} from '@nestjs/event-emitter'; +import {SchedulerRegistry} from '@nestjs/schedule'; +import {BaseCustomDataSource, BaseDataSource, BaseHandler, BaseMapping, DictionaryQueryEntry} from '@subql/types-core'; import { UnfinalizedBlocksService, BlockDispatcher, @@ -23,14 +17,19 @@ import { ISubqueryProject, DatasourceParams, IBaseIndexerWorker, + BypassBlocks, } from '../'; -import { BlockHeightMap } from '../utils/blockHeightMap'; -import { DictionaryService } from './dictionary/dictionary.service'; -import { FetchService } from './fetch.service'; +import {BlockHeightMap} from '../utils/blockHeightMap'; +import {DictionaryService} from './dictionary/dictionary.service'; +import {FetchService} from './fetch.service'; const CHAIN_INTERVAL = 100; // 100ms class TestFetchService extends FetchService, any> { + setBypassBlocks(blocks: BypassBlocks) { + this.projectService.bypassBlocks = blocks; + } + protected buildDictionaryQueryEntries( dataSources: BaseDataSource, BaseMapping>>[] ): DictionaryQueryEntry[] { @@ -74,12 +73,12 @@ class TestBlockchainService implements IBlockchainService { fetchBlockWorker( worker: IBaseIndexerWorker, blockNum: number, - context: { workers: IBaseIndexerWorker[] } + context: {workers: IBaseIndexerWorker[]} ): Promise { throw new Error('Method not implemented.'); } async getFinalizedHeader(): Promise
{ - return Promise.resolve({ blockHeight: this.finalizedHeight, blockHash: '0xxx', parentHash: '0xxx' }); + return Promise.resolve({blockHeight: this.finalizedHeight, blockHash: '0xxx', parentHash: '0xxx'}); } async getBestHeight(): Promise { return Promise.resolve(this.bestHeight); @@ -154,7 +153,7 @@ function mockModuloDs(startBlock: number, endBlock: number, modulo: number): Bas { kind: 'mock/Handler', handler: 'mockFunction', - filter: { modulo: modulo }, + filter: {modulo: modulo}, }, ], }, @@ -162,26 +161,26 @@ function mockModuloDs(startBlock: number, endBlock: number, modulo: number): Bas } const getDictionaryService = () => -({ - useDictionary: () => { - return false; - }, - findDictionary: () => { - /* TODO*/ - }, - buildDictionaryEntryMap: () => { - /* TODO*/ - }, - initValidation: () => { - /* TODO */ - }, - scopedDictionaryEntries: () => { - /* TODO */ - }, - initDictionaries: () => { - /* TODO */ - }, -} as any as DictionaryService); + ({ + useDictionary: () => { + return false; + }, + findDictionary: () => { + /* TODO*/ + }, + buildDictionaryEntryMap: () => { + /* TODO*/ + }, + initValidation: () => { + /* TODO */ + }, + scopedDictionaryEntries: () => { + /* TODO */ + }, + initDictionaries: () => { + /* TODO */ + }, + }) as any as DictionaryService; const getBlockDispatcher = () => { const inst = { @@ -296,20 +295,20 @@ describe('Fetch Service', () => { const moduloBlockHeightMap = new BlockHeightMap( new Map([ - [1, [{ ...mockModuloDs(1, 100, 20), startBlock: 1, endBlock: 100 }]], + [1, [{...mockModuloDs(1, 100, 20), startBlock: 1, endBlock: 100}]], [ 101, // empty gap for discontinuous block [], ], - [201, [{ ...mockModuloDs(201, 500, 30), startBlock: 201, endBlock: 500 }]], + [201, [{...mockModuloDs(201, 500, 30), startBlock: 201, endBlock: 500}]], // to infinite - [500, [{ ...mockModuloDs(500, Number.MAX_SAFE_INTEGER, 99), startBlock: 500 }]], + [500, [{...mockModuloDs(500, Number.MAX_SAFE_INTEGER, 99), startBlock: 500}]], // multiple ds [ 600, [ - { ...mockModuloDs(500, 800, 99), startBlock: 600, endBlock: 800 }, - { ...mockModuloDs(700, Number.MAX_SAFE_INTEGER, 101), startBlock: 700 }, + {...mockModuloDs(500, 800, 99), startBlock: 600, endBlock: 800}, + {...mockModuloDs(700, Number.MAX_SAFE_INTEGER, 101), startBlock: 700}, ], ], ]) @@ -327,43 +326,43 @@ describe('Fetch Service', () => { [ 1, [ - { ...mockDs, startBlock: 1, endBlock: 300 }, - { ...mockDs, startBlock: 1, endBlock: 100 }, + {...mockDs, startBlock: 1, endBlock: 300}, + {...mockDs, startBlock: 1, endBlock: 100}, ], ], [ 10, [ - { ...mockDs, startBlock: 1, endBlock: 300 }, - { ...mockDs, startBlock: 1, endBlock: 100 }, - { ...mockDs, startBlock: 10, endBlock: 20 }, + {...mockDs, startBlock: 1, endBlock: 300}, + {...mockDs, startBlock: 1, endBlock: 100}, + {...mockDs, startBlock: 10, endBlock: 20}, ], ], [ 21, [ - { ...mockDs, startBlock: 1, endBlock: 300 }, - { ...mockDs, startBlock: 1, endBlock: 100 }, + {...mockDs, startBlock: 1, endBlock: 300}, + {...mockDs, startBlock: 1, endBlock: 100}, ], ], [ 50, [ - { ...mockDs, startBlock: 1, endBlock: 300 }, - { ...mockDs, startBlock: 1, endBlock: 100 }, - { ...mockDs, startBlock: 50, endBlock: 200 }, + {...mockDs, startBlock: 1, endBlock: 300}, + {...mockDs, startBlock: 1, endBlock: 100}, + {...mockDs, startBlock: 50, endBlock: 200}, ], ], [ 101, [ - { ...mockDs, startBlock: 1, endBlock: 300 }, - { ...mockDs, startBlock: 50, endBlock: 200 }, + {...mockDs, startBlock: 1, endBlock: 300}, + {...mockDs, startBlock: 50, endBlock: 200}, ], ], - [201, [{ ...mockDs, startBlock: 1, endBlock: 300 }]], + [201, [{...mockDs, startBlock: 1, endBlock: 300}]], [301, []], - [500, [{ ...mockDs, startBlock: 500 }]], + [500, [{...mockDs, startBlock: 500}]], ]) ) ); @@ -499,7 +498,7 @@ describe('Fetch Service', () => { { kind: 'mock/BlockHandler', handler: 'mockFunction', - filter: { modulo: 3 }, + filter: {modulo: 3}, }, { kind: 'mock/CallHandler', @@ -632,7 +631,7 @@ describe('Fetch Service', () => { it('enqueues modulo blocks with furture dataSources', async () => { fetchService.mockGetModulos([3]); - dataSources.push({ ...mockDs, startBlock: 20 }); + dataSources.push({...mockDs, startBlock: 20}); await fetchService.init(1); @@ -645,7 +644,7 @@ describe('Fetch Service', () => { it('at the end of modulo block filter, enqueue END should be min of data source range end height and api last height', async () => { // So this will skip next data source fetchService.mockGetModulos([10]); - dataSources.push({ ...mockDs, startBlock: 200 }); + dataSources.push({...mockDs, startBlock: 200}); await fetchService.init(191); expect((fetchService as any).useDictionary).toBeFalsy(); @@ -653,7 +652,7 @@ describe('Fetch Service', () => { }); it('skips bypassBlocks', async () => { - (fetchService as any).networkConfig.bypassBlocks = [3]; + fetchService.setBypassBlocks([3]); await fetchService.init(1); @@ -664,7 +663,7 @@ describe('Fetch Service', () => { it('transforms bypassBlocks', async () => { // Set a range so on init its transformed - (fetchService as any).networkConfig.bypassBlocks = ['2-5']; + fetchService.setBypassBlocks(['2-5']); await fetchService.init(1); diff --git a/packages/node-core/src/indexer/project.service.spec.ts b/packages/node-core/src/indexer/project.service.spec.ts index 402626fc1e..81f083706a 100644 --- a/packages/node-core/src/indexer/project.service.spec.ts +++ b/packages/node-core/src/indexer/project.service.spec.ts @@ -38,27 +38,27 @@ class TestBlockchainService implements IBlockchainService { // eslint-disable-next-line @typescript-eslint/promise-function-async fetchBlocks(blockNums: number[]): Promise[]> { - throw new Error('Method not implemented.'); + throw new Error('Method fetchBlocks not implemented.'); } onProjectChange(project: ISubqueryProject): Promise | void { - throw new Error('Method not implemented.'); + // throw new Error('Method onProjectChange not implemented.'); } // eslint-disable-next-line @typescript-eslint/promise-function-async getBlockTimestamp(height: number): Promise { - throw new Error('Method not implemented.'); + throw new Error('Method getBlockTimestamp not implemented.'); } // eslint-disable-next-line @typescript-eslint/promise-function-async getBestHeight(): Promise { - throw new Error('Method not implemented.'); + throw new Error('Method getBestHeight not implemented.'); } // eslint-disable-next-line @typescript-eslint/promise-function-async getChainInterval(): Promise { - throw new Error('Method not implemented.'); + throw new Error('Method getChainInterval not implemented.'); } // eslint-disable-next-line @typescript-eslint/promise-function-async updateDynamicDs(params: DatasourceParams, template: any): Promise { - throw new Error('Method not implemented.'); + throw new Error('Method updateDynamicDs not implemented.'); } isCustomDs(x: any): x is any { return false;