Skip to content

Commit

Permalink
chore(store): add store unit test support file back (#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedragon authored Jan 21, 2025
1 parent 3e54eaf commit 5191ae6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/runtime/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,12 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
]
)
}
const result = await PluginManager.INSTANCE.processBinding(request, preparedData)

const result = await PluginManager.INSTANCE.processBinding(
request,
preparedData,
PluginManager.INSTANCE.dbContextLocalStorage.getStore()
)
recordRuntimeInfo(result, request.handlerType)
return result
}
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/core/base-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export abstract class BaseContext {

get store() {
if (this._store == null) {
console.warn('Store is not set, please initialize the processor with your database schema first.')
this.initStore()
if (this._store == null) {
console.warn('Store is not set, please initialize the processor with your database schema first.')
}
}
return this._store
}
Expand Down
31 changes: 31 additions & 0 deletions packages/sdk/src/store/tests/store-processor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { before, describe, test } from 'node:test'

import { TestProcessorServer } from '../../testing/index.js'
import { mockTransferLog } from '../../eth/builtin/erc20.js'
import { Transaction } from './generated/schema.js'
import { expect } from 'chai'

describe('test entity store for processor', () => {
const ADDRESS = '0x1000000000000000000000000000000000000000'
const service = new TestProcessorServer(() => import('./store-processor.js'))

before(async () => {
await service.start()
})

test('Check entity is stored', async () => {
await service.eth.testAccountLogs(ADDRESS, [
mockTransferLog('0x1E4EDE388cbc9F4b5c79681B7f94d36a11ABEBC9', {
from: ADDRESS,
to: '0xB329e39Ebefd16f40d38f07643652cE17Ca5Bac1',
value: BigInt(100)
})
])

const tx = await service.store.get(
Transaction,
'0x0000000000000000000000000000000000000000000000000000000000000000'
)
expect(tx!.value).to.be.eq(100)
})
})
17 changes: 17 additions & 0 deletions packages/sdk/src/store/tests/store-processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AccountProcessor } from '../../eth/account-processor.js'
import { BigDecimal } from '@sentio/bigdecimal'
import { Transaction } from './generated/schema.js'
const ADDRESS = '0x1000000000000000000000000000000000000000'

AccountProcessor.bind({ address: ADDRESS }).onERC20TransferOut(async (evt, ctx) => {
const tx = new Transaction({
arrayOfArrayValue: [],
arrayValue: [],
gas: 0n,
gasPrice: new BigDecimal(0),
value: Number(evt.args.value),
id: evt.transactionHash
})

await ctx.store.upsert(tx)
})
5 changes: 5 additions & 0 deletions packages/sdk/src/testing/memory-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType } fro
import { PluginManager } from '@sentio/runtime'
import { BigDecimalConverter, BigIntConverter } from '../store/convert.js'
import { BigDecimal } from '@sentio/bigdecimal'
import { Store } from '../store/store.js'

export class MemoryDatabase {
db = new Map<string, Record<string, any>>()
Expand All @@ -29,6 +30,10 @@ export class MemoryDatabase {
}
}

get store() {
return new Store(this.dbContext)
}

start() {
this.dbContext.subject.subscribe(this.processRequest.bind(this))
}
Expand Down
19 changes: 17 additions & 2 deletions packages/sdk/src/testing/test-processor-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ import { StarknetFacet } from './starknet-facet.js'
import { BTCFacet } from './btc-facet.js'
import { Subject } from 'rxjs'
import { MemoryDatabase } from './memory-database.js'
import { DatabaseSchemaState } from '../core/database-schema.js'

export const TEST_CONTEXT: CallContext = <CallContext>{}

export function cleanTest() {
// retain the DatabaseSchemaState
const state = State.INSTANCE.stateMap.get(DatabaseSchemaState.INSTANCE.key())
State.reset()
State.INSTANCE.stateMap.set(DatabaseSchemaState.INSTANCE.key(), state)
}

export class TestProcessorServer implements ProcessorServiceImplementation {
Expand All @@ -51,6 +55,7 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
cosmos: CosmosFacet
starknet: StarknetFacet
btc: BTCFacet
db: MemoryDatabase

constructor(loader: () => Promise<any>, httpEndpoints: Record<string, string> = {}) {
cleanTest()
Expand All @@ -73,8 +78,6 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
// start a memory database for testing
const subject = new Subject<DeepPartial<ProcessStreamResponse>>()
this.storeContext = new StoreContext(subject, 1)
const db = new MemoryDatabase(this.storeContext)
db.start()
}

async start(request: StartRequest = { templateInstances: [] }, context = TEST_CONTEXT): Promise<Empty> {
Expand All @@ -97,12 +100,14 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
request: ProcessBindingsRequest,
context: CallContext = TEST_CONTEXT
): Promise<ProcessBindingResponse> {
this.initDb()
return PluginManager.INSTANCE.dbContextLocalStorage.run(this.storeContext, () => {
return this.service.processBindings(request, context)
})
}

processBinding(request: DataBinding, context: CallContext = TEST_CONTEXT): Promise<ProcessBindingResponse> {
this.initDb()
return PluginManager.INSTANCE.dbContextLocalStorage.run(this.storeContext, () => {
return this.service.processBindings({ bindings: [request] }, context)
})
Expand All @@ -125,4 +130,14 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
// processBindingsStream(request: AsyncIterable<ProcessStreamRequest>, context: CallContext) {
// return this.service.processBindingsStream(request, context)
// }
private initDb() {
if (this.db == null) {
this.db = new MemoryDatabase(this.storeContext)
this.db.start()
}
}

get store() {
return this.db.store
}
}

0 comments on commit 5191ae6

Please sign in to comment.