apibara/dna

Allow sink developers to easily test their transform functions

Closed this issue · 4 comments

Is your feature request related to a problem? Please describe.
It's not a problem per say, but sink developers don't have any way to test if their transform function is doing exactly what they want it to do

Describe the solution you'd like
First, we should generate the test data using the filter and transform configuration in their script.js file, they have to run a command like:
apibara test new script.js --num-blocks 100

The generated test data will include:

  • the filter and transform configuration, to be used to check we're testing against the same script
  • the raw data got from DNA
  • the data returned from their transform function

The data will be stored in a JSON file, maybe in the target/ directory

The idea is that, the dev will look at the file and check if that's exactly what he expects to see, if not he should edit the transform function and run again until he gets what he want. Then, that file will be used to test that the transform function is always doing what it's supposed to do, for that the dev will run a command every time he want to make sure his transform function is working as expected:

apibara test check script.js or apibara test check target/test/script-test-case-xxx.json

This will feed the input data to the transform function and assert that it returns the expected output

Describe alternatives you've considered A clear and concise description of
any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature
request here.

I think this is a great idea! A couple of comments:

  • we should add a way to override the starting block so that devs can generate the data for specific events
  • maybe new should have a --name option to override the test script name?
  • tests should go in the tests folder by default, target is often associated with build outputs because of rust
  • apibara test check maybe should look for all *.json files in tests/ by default?

Ideally, long term we would have a nice cli like insta does.

So the development flow I envision is:

$ tree .
.
└── src
    └── indexer.ts
$ apibara test new src/indexer.ts --name approval
$ tree .
.
├── mkdir
├── src
│   └── indexer.ts
└── tests
    └── approval.json
$ apibara test new src/indexer.ts --name mint
$ tree .
.
├── mkdir
├── src
│   └── indexer.ts
└── tests
    ├── approval.json
    └── mint.json
$ apibara test check
Collecting tests from `tests/`. Found 2.
test approval... ok
test mint... ok
  • insta's CLI looks great, I think we can make the CLI simpler and have a single test command instead of two commands: new and check, the command will first check if there is a test with the same name as the script (if passed) or --name if provided, if so, it runs the input through the transform function in the script and assert the output, if the test does not exist, it creates it and then run it. if no script or --name is provided, it runs all the tests

  • OK for the --name argument

  • tests folder has special treatment in many languages/frameworks, I'd advice to get away from it or at least make a subdirectory

  • Yeah, I think apibara test (if we go with one command) or apibara test check should run all the tests

I agree with using a single command that either generates a new test case or runs existing tests.

Maybe we can call the folder with tests snapshots/?

snapshots/ is already used by insta.rs, so that'd cause conflicts if we use it alongside and it's not self explanatory for our case, I'd go with something like apibara-tests ?