Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Nov 13, 2024
1 parent ad60722 commit 7290a06
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 78 deletions.
43 changes: 30 additions & 13 deletions packages/node-core/src/indexer/dynamic-ds.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TestDynamicDsService extends DynamicDsService<BaseDataSource, ISubqueryPro
} as unknown as IBlockchainService);
}

// Make it public
getTemplate<T extends Omit<NonNullable<ISubqueryProject['templates']>[number], 'name'> & {startBlock?: number}>(
templateName: string,
startBlock?: number | undefined
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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]);
});
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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();
Expand All @@ -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'});
});
});
117 changes: 58 additions & 59 deletions packages/node-core/src/indexer/fetch.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<BaseDataSource, IBlockDispatcher<any>, any> {
setBypassBlocks(blocks: BypassBlocks) {
this.projectService.bypassBlocks = blocks;
}

protected buildDictionaryQueryEntries(
dataSources: BaseDataSource<BaseHandler<any>, BaseMapping<BaseHandler<any>>>[]
): DictionaryQueryEntry[] {
Expand Down Expand Up @@ -74,12 +73,12 @@ class TestBlockchainService implements IBlockchainService {
fetchBlockWorker(
worker: IBaseIndexerWorker,
blockNum: number,
context: { workers: IBaseIndexerWorker[] }
context: {workers: IBaseIndexerWorker[]}
): Promise<void> {
throw new Error('Method not implemented.');
}
async getFinalizedHeader(): Promise<Header> {
return Promise.resolve({ blockHeight: this.finalizedHeight, blockHash: '0xxx', parentHash: '0xxx' });
return Promise.resolve({blockHeight: this.finalizedHeight, blockHash: '0xxx', parentHash: '0xxx'});
}
async getBestHeight(): Promise<number> {
return Promise.resolve(this.bestHeight);
Expand Down Expand Up @@ -154,34 +153,34 @@ function mockModuloDs(startBlock: number, endBlock: number, modulo: number): Bas
{
kind: 'mock/Handler',
handler: 'mockFunction',
filter: { modulo: modulo },
filter: {modulo: modulo},
},
],
},
};
}

const getDictionaryService = () =>
({
useDictionary: () => {
return false;
},
findDictionary: () => {
/* TODO*/
},
buildDictionaryEntryMap: () => {
/* TODO*/
},
initValidation: () => {
/* TODO */
},
scopedDictionaryEntries: () => {
/* TODO */
},
initDictionaries: () => {
/* TODO */
},
} as any as DictionaryService<any, any>);
({
useDictionary: () => {
return false;
},
findDictionary: () => {
/* TODO*/
},
buildDictionaryEntryMap: () => {
/* TODO*/
},
initValidation: () => {
/* TODO */
},
scopedDictionaryEntries: () => {
/* TODO */
},
initDictionaries: () => {
/* TODO */
},
}) as any as DictionaryService<any, any>;

const getBlockDispatcher = () => {
const inst = {
Expand Down Expand Up @@ -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},
],
],
])
Expand All @@ -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}]],
])
)
);
Expand Down Expand Up @@ -499,7 +498,7 @@ describe('Fetch Service', () => {
{
kind: 'mock/BlockHandler',
handler: 'mockFunction',
filter: { modulo: 3 },
filter: {modulo: 3},
},
{
kind: 'mock/CallHandler',
Expand Down Expand Up @@ -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);

Expand All @@ -645,15 +644,15 @@ 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();
expect(enqueueBlocksSpy).toHaveBeenCalledWith([], 199);
});

it('skips bypassBlocks', async () => {
(fetchService as any).networkConfig.bypassBlocks = [3];
fetchService.setBypassBlocks([3]);

await fetchService.init(1);

Expand All @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions packages/node-core/src/indexer/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ class TestBlockchainService implements IBlockchainService {

// eslint-disable-next-line @typescript-eslint/promise-function-async
fetchBlocks(blockNums: number[]): Promise<IBlock<any>[]> {
throw new Error('Method not implemented.');
throw new Error('Method fetchBlocks not implemented.');
}

onProjectChange(project: ISubqueryProject): Promise<void> | 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<Date | undefined> {
throw new Error('Method not implemented.');
throw new Error('Method getBlockTimestamp not implemented.');
}
// eslint-disable-next-line @typescript-eslint/promise-function-async
getBestHeight(): Promise<number> {
throw new Error('Method not implemented.');
throw new Error('Method getBestHeight not implemented.');
}
// eslint-disable-next-line @typescript-eslint/promise-function-async
getChainInterval(): Promise<number> {
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<void> {
throw new Error('Method not implemented.');
throw new Error('Method updateDynamicDs not implemented.');
}
isCustomDs(x: any): x is any {
return false;
Expand Down

0 comments on commit 7290a06

Please sign in to comment.