Skip to content

Commit

Permalink
refactor: Move Context object to new class (#929)
Browse files Browse the repository at this point in the history
All logic associated with the creation of the context object resides
inside the Indexer class, which bloats the file and complicates unit
testing. In addition, it prevents access to the context object itself so
that these calls can be made through the Indexer class itself.

This PR refactors this logic into its own class, allowing access to
context methods through Indexer. The object is still recreated each time
it is accessed as the object bakes in some state information (block
height and log entry array) which need to be updated on subsequent
accesses.

I've also reorganized the folders so that we have fewer folders under
src as it is getting cluttered.
  • Loading branch information
darunrs authored Aug 9, 2024
1 parent a238cb5 commit af08c30
Show file tree
Hide file tree
Showing 24 changed files with 1,092 additions and 1,324 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ redis/
node_modules/
.vscode/
runner/yarn.lock
**/.DS_Store

3 changes: 0 additions & 3 deletions core-indexers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
},
"author": "",
"license": "ISC",
"dependencies": {
"queryapi-runner": "file:../runner"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.7.0",
Expand Down
42 changes: 40 additions & 2 deletions core-indexers/receiver-blocks/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import LocalIndexer from 'queryapi-runner/src/local-indexer';
import LocalIndexer from 'queryapi-runner/src/indexer/local-indexer';
import { LocalIndexerConfig } from 'queryapi-runner/src/indexer-config/indexer-config';
import { LogLevel } from 'queryapi-runner/src/indexer-meta/log-entry';
import path from 'path';
Expand All @@ -15,6 +15,44 @@ describe('Receiver Blocks Indexer Tests', () => {

test('Try executing on a block', async () => {
const localIndexer = new LocalIndexer(indexerConfig);
await localIndexer.executeOnBlock(123621232);
const context = localIndexer.getContext();

// Run on one block to populate receivers table and initial bitmap
await localIndexer.executeOnBlock(100000000);
const receivers = await context.db.Receivers.select({
receiver: 'app.nearcrowd.near'
});
const tokenSweatId = receivers[0].id;

const correctBitmapOne = {
first_block_height: 100000000,
block_date: '2023-08-30',
receiver_id: tokenSweatId,
bitmap: 'wA==',
last_elias_gamma_start_bit: 1,
max_index: 0,
};
const correctBitmapTwo = {
first_block_height: 100000000,
block_date: '2023-08-30',
receiver_id: tokenSweatId,
bitmap: 'oA==',
last_elias_gamma_start_bit: 1,
max_index: 1,
};

let bitmap = await context.db.Bitmaps.select({
receiver_id: tokenSweatId
});
expect(bitmap.length).toBe(1);
expect(bitmap[0]).toEqual(correctBitmapOne);

// Run on second block and verify bitmap update
await localIndexer.executeOnBlock(100000001);
bitmap = await context.db.Bitmaps.select({
receiver_id: tokenSweatId
});
expect(bitmap.length).toBe(1);
expect(bitmap[0]).toEqual(correctBitmapTwo);
});
});
5 changes: 4 additions & 1 deletion core-indexers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"target": "es2018", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es2021"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "..",
"rootDir": "..",
"paths": {
"@queryapi-runner/*": ["../runner/*"], /* Allow imports from runner using queryapi-runner alias */
},
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"resolveJsonModule": true, /* Enable importing .json files. */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
Expand Down
Binary file removed runner/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion runner/src/dml-handler/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions runner/src/indexer-config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from './indexer-config';
export { ProvisioningConfig, LocalIndexerConfig } from './indexer-config';
72 changes: 0 additions & 72 deletions runner/src/indexer/__snapshots__/indexer.test.ts.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ContextBuilder unit tests Context object social api can fetch from the near social api 1`] = `
[
[
"https://api.near.social/index",
{
"body": "{"action":"post","key":"main","options":{"limit":1,"order":"desc"}}",
"headers": {
"Content-Type": "application/json",
},
"method": "POST",
},
],
]
`;

exports[`ContextBuilder unit tests ContextBuilder adds CRUD operations for table 1`] = `
{
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
}
`;

exports[`ContextBuilder unit tests ContextBuilder can parse various schemas 1`] = `
{
"Posts": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
}
`;

exports[`ContextBuilder unit tests ContextBuilder can parse various schemas 2`] = `
{
"Comments": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"PostLikes": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"Posts": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
}
`;

exports[`ContextBuilder unit tests ContextBuilder can parse various schemas 3`] = `
{
"CommentsTable": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"Posts": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
}
`;

exports[`ContextBuilder unit tests ContextBuilder can parse various schemas 4`] = `
{
"AnotherTable": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"Comments": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"ComposerQuest": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"ContractorQuest": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"CreatorQuest": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"MyTable1": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"PostLikes": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"Posts": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"ThirdTable": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
"YetAnotherTable": {
"delete": [Function],
"insert": [Function],
"select": [Function],
"update": [Function],
"upsert": [Function],
},
}
`;
Loading

0 comments on commit af08c30

Please sign in to comment.