Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testing not catching block fetch errors #2716

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/controller/generate-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@

it('prepareInputFragments, no method passed, should prompt through inquirer', async () => {
// when using ejs, jest spyOn does not work on inquirer
const inquirer = require('@inquirer/prompts');

Check warning on line 182 in packages/cli/src/controller/generate-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Require statement not part of import statement

const promptSpy = jest.spyOn(inquirer, 'checkbox').mockResolvedValue(['Approval(address,address,uint256)']);

Expand Down Expand Up @@ -213,11 +213,11 @@
await expect(
prepareInputFragments<FunctionFragment>(
'function',
'transFerfrom(address,address,uint256)',
'transferFrom(address,address,uint256)',
functionFragments,
abiName
)
).rejects.toThrow("'transFerfrom(address' is not a valid function on Erc721");
).rejects.toThrow("'transferFrom(address' is not a valid function on Erc721");
await expect(
prepareInputFragments<FunctionFragment>(
'function',
Expand Down
23 changes: 23 additions & 0 deletions packages/node-core/src/indexer/test.runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,27 @@ describe('TestRunner', () => {
`\t\tattribute: "timestamp":\n\t\t\texpected: "1970-01-01T00:00:01.000Z"\n\t\t\tactual: "1970-01-01T00:00:01.001Z"\n`
);
});

it('increments error if a block fails to be fetched', async () => {
const expectedEntity = {
_name: 'Entity1',
id: '1',
attr: 'value',
};
const testMock = {
name: 'test1',
blockHeight: 1,
handler: 'handler1',
expectedEntities: [expectedEntity],
dependentEntities: [],
};

apiServiceMock.fetchBlocks = jest.fn().mockRejectedValue(new Error('Failed to fetch block'));

const indexBlock = jest.fn().mockResolvedValue(undefined);
const res = await testRunner.runTest(testMock, sandboxMock, indexBlock);

expect(res.failedTests).toBe(2);
expect(res.failedTestSummary?.failedAttributes[0]).toContain('Failed to fetch block');
});
});
20 changes: 19 additions & 1 deletion packages/node-core/src/indexer/test.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ export class TestRunner<A, SA, B, DS> {
@Inject('IIndexerManager') protected readonly indexerManager: IIndexerManager<B, DS>
) {}

private async fetchBlock(height: number): Promise<IBlock<B>> {
try {
const [block] = await this.apiService.fetchBlocks([height]);
return block;
} catch (e: any) {
logger.warn(`Test: ${test.name} field due to fetch block error`, e);
this.failedTestSummary = {
testName: test.name,
entityId: undefined,
entityName: undefined,
failedAttributes: [`Fetch Block Error:\n${e.message}`],
};

this.failedTests++;
throw e;
}
}

async runTest(
test: SubqlTest,
sandbox: TestSandbox,
Expand All @@ -55,7 +73,7 @@ export class TestRunner<A, SA, B, DS> {
try {
// Fetch block
logger.debug('Fetching block');
const [block] = await this.apiService.fetchBlocks([test.blockHeight]);
const block = await this.fetchBlock(test.blockHeight);

await this.storeService.setBlockHeader(block.getHeader());
// Ensure a block height is set so that data is flushed correctly
Expand Down
7 changes: 1 addition & 6 deletions packages/node-core/src/indexer/testing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ export abstract class TestingService<A, SA, B, DS extends BaseDataSource> {

abstract getTestRunner(): Promise<[close: () => Promise<void>, runner: TestRunner<A, SA, B, DS>]>; // TestRunner will be create with a new app instance

async indexBlock(
block: IBlock<B>,
handler: string,
indexerManager: IIndexerManager<B, DS>,
apiService?: IApi<A, SA, IBlock<B>[]>
): Promise<void> {
async indexBlock(block: IBlock<B>, handler: string, indexerManager: IIndexerManager<B, DS>): Promise<void> {
await indexerManager.indexBlock(block, this.getDsWithHandler(handler));
}

Expand Down
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Testing not updating test results correctly if fetching block fails (#2716)

## [5.10.0] - 2025-03-05
### Changed
Expand Down
Loading