subsquid / squid-sdk

The main repo of the squid SDK
GNU General Public License v3.0
1.2k stars 140 forks source link

Testing framework: RnD #239

Closed 2ndshadow closed 1 hour ago

2ndshadow commented 4 months ago

Epic: Testing framework for squids #220 Estimate: 2d

vanruch commented 3 months ago

Mock data sinks

Mock TypeORM store

Use in-memory sqlite data source (outdated example here)

Mock Filestore

Do we need it? Devs can just delete files manually after testing

Mock bigquery

I haven't found an easy solution to mock BigQuery. I think best solution here would be to recommend users to use something like https://github.com/goccy/bigquery-emulator and connect to it

Mock Batch Processors

MockEvmBatchProcessor gets EvmProcessor instance and only cares for what was set in .addLog, .addTransaction, .addStateDiff, .addTrace & .setFields calls

import {handler, processor} from "./src"
import {encodeLog} from "@subsquid/evm-utils"
import * as contract from "./abi/contract.ts"

const tokenAddress = '0x12313123...'

test("Correctly calculates minted amounts", () => {
  const mockProcessor = new MockEvmBatchProcessor(processor)
  const db = new MockTypeOrm()

  mockProcessor.run(db, handler)

  // This would wait until handler(ctx) is called
  await mockProcessor.inject([{
    logs: [
      encodeLog(contract.events.Mint, tokenAddress, {
        to: ...
        amount: 10n
       }),
       encodeLog(contract.events.Mint, tokenAddress, {
        to: ...
        amount: 100n
       }),
     ],
    }])

  expect((await db.get(Token, tokenAddress)).totalSupply).toBe(110n)
})

Similar one for substrate

dzhelezov commented 3 months ago

Can we generate events from the ABI?

dzhelezov commented 3 months ago

also not clear how db.get() works

mo4islona commented 3 months ago

also not clear how db.get() works

Why we can't use a real database?