πŸ”₯ Unit testing framework for Subgraph development on The Graph protocol. βš™οΈ

Related tags

Testing matchstick
Overview

GitHub Banner-1

License: MIT

πŸ‘‹ Welcome to Matchstick - a unit testing framework for The Graph protocol. Try out your mapping logic in a sandboxed environment and ensure your handlers run correctly when deploying your awesome subgraph!

Quick Start πŸš€

The release binary comes in three flavours - for macOS, Linux and Windows. To add Matchstick to your subgraph project just open up a terminal, navigate to the root folder of your project and simply run graph test - it downloads the latest Matchstick binary and runs the specified test or all tests in a test folder (or all existing tests if no datasource flag is specified). Example usage: graph test Gravity.

❗ If you don't have Postgres installed, you will need to install it. Instructions for that below:

macOS ο£Ώ

❗ Postgres installation command:

brew install postgresql

Linux 🐧

❗ Postgres installation command (depends on your distro):

sudo apt install postgresql

Windows

❗ Postgres installation command:

choco install postgresql12

Install dependencies

In order to use the test helper methods and run the tests, you will need to install the following dependencies:

yarn add matchstick-as

Now you can jump straight to the examples in our demo-subgraph and start your journey in Subgraph unit testing!

Building from source

Prerequisites

To build and run Matchstick you need to have the following installed on your system:

Setup

Clone this repository and run cargo build. If that executes successfully congratulations πŸŽ‰ you're all set.

NOTE: You may encounter an error, related to missing libpq dependencies on your system. In that case - install the missing dependencies (listed in the error log) with your package manager.

Next steps 🎯

There is a lot of room for improvements to Matchstick. We're trying to gather as much feedback from subgraph developers as we can, to understand how we can solve the problems they face when building subgraphs, as well as how we can make the overall testing process as smooth and streamlined as possible.

There's a GitHub project board where we keep track of day to day work which you can check out here.

You can check out the full list of tasks here.

Technologies used πŸ’»

diagram-resized

The Matchstick framework is built in Rust and acts as a wrapper for the generated WebAssembly module that contains the mappings and the unit tests. It passes the host function implementations down to the module, to be used in the tests (and in the mappings if needed). The framework also acts as a proxy for structs defined in the graph-node repo, because it needs to pass down all the usual imports, as well as a few bonus/mocked ones glued on top.

Matchstick also relies on a helper library - matchstick-as, written in AssemblyScript and used as an import in the unit tests.

Comments
  • "SyntaxError: Unexpected token =" when running "docker build -t matchstick ."

    Hi,

    when I run docker build -t matchstick . I get following error

     => ERROR [14/15] RUN npm run codegen                                                                                                6.9s
    ------
     > [14/15] RUN npm run codegen:
    #18 1.619
    #18 1.619 > nft-share-platform-ropsten@ codegen /matchstick
    #18 1.619 > graph codegen
    #18 1.619
    #18 6.794 /matchstick/node_modules/@graphprotocol/graph-cli/src/codegen/schema.js:9
    #18 6.794   static BYTES = Symbol("Bytes")
    #18 6.794                ^
    #18 6.794
    #18 6.794 SyntaxError: Unexpected token =
    #18 6.794     at Module._compile (internal/modules/cjs/loader.js:723:23)
    #18 6.794     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    #18 6.794     at Module.load (internal/modules/cjs/loader.js:653:32)
    #18 6.794     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    #18 6.794     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    #18 6.794     at Module.require (internal/modules/cjs/loader.js:692:17)
    #18 6.794     at require (internal/modules/cjs/helpers.js:25:18)
    #18 6.794     at Object.<anonymous> (/matchstick/node_modules/@graphprotocol/graph-cli/src/schema.js:5:27)
    #18 6.794     at Module._compile (internal/modules/cjs/loader.js:778:30)
    #18 6.794     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    #18 6.801 npm ERR! code ELIFECYCLE
    #18 6.801 npm ERR! errno 1
    #18 6.802 npm ERR! nft-share-platform-ropsten@ codegen: `graph codegen`
    #18 6.803 npm ERR! Exit status 1
    #18 6.803 npm ERR!
    #18 6.803 npm ERR! Failed at the nft-share-platform-ropsten@ codegen script.
    #18 6.804 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    #18 6.822
    #18 6.822 npm ERR! A complete log of this run can be found in:
    #18 6.822 npm ERR!     /root/.npm/_logs/2022-03-16T14_58_53_712Z-debug.log
    ------
    executor failed running [/bin/sh -c npm run codegen]: exit code: 1
    

    This is my Dockerfile

    
    ARG BUILDPLATFORM=linux/x86_64
    
    ENV ARGS=""
    
    RUN apt update
    RUN apt install -y nodejs
    RUN apt install -y npm
    RUN apt install -y git
    RUN apt install -y postgresql
    RUN apt install -y curl
    RUN apt install -y cmake
    
    RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/0.3.0/binary-linux-20
    RUN chmod a+x binary-linux-20
    
    RUN mkdir matchstick
    WORKDIR /matchstick
    
    COPY ../ .
    
    RUN npm run codegen
    RUN npm run build
    
    CMD ../binary-linux-20 ${ARGS}
    

    When I run npm run codegen in my subgraph project it finishes without error.

    Host PC is Windows 11. Any ideas what went wrong?

    opened by mmartinmo 11
  • m1 v5 binary incorrect

    m1 v5 binary incorrect

    Hi, I was trying to use version 5 on an m1 Mac, but could not get it to install the correct binary.

    I downloaded the m1 binary from the release and manually replaced the one inside binary-install-raw/bin/0.5.0/binary-macos-11 - all seems to work now.

    This is the error I faced:

    Downloading release from https://github.com/LimeChain/matchstick/releases/download/0.5.0/binary-macos-11
    binary-macos-11 has been installed!
    dyld[81528]: Library not loaded: /usr/local/opt/postgresql/lib/libpq.5.dylib
      Referenced from: /Users/.../node_modules/binary-install-raw/bin/0.5.0/binary-macos-11
      Reason: tried: '/usr/local/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/libpq.5.dylib' (no such file)
    
    opened by dub6ix 7
  • matchstick fails to find asc binary on M1 Macbook

    matchstick fails to find asc binary on M1 Macbook

    When running graph test from /Users/<user>/code/rift/rift-subgraph/rinkeby, the following occurs:

    πŸ’¬ Compiling vault/updated_vault...
    thread 'main' panicked at 'πŸ†˜ Internal error during compilation: No such file or directory (os error 2).
    Command path: "/Users/<user>/code/rift/rift-subgraph/rinkeby/node_modules/assemblyscript/bin/asc"
    Globals path: "/Users/<user>/code/rift/rift-subgraph/rinkeby/node_modules/@graphprotocol/graph-ts/global/global.ts"
    Libs folder: "/Users/<user>/code/rift/rift-subgraph/rinkeby/node_modules"', src/compiler.rs:131:17
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    error Command failed with exit code 101.
    

    In /Users/<user>/code/rift/rift-subgraph/rinkeby/node_modules/assemblyscript/bin/, there exist 2 files:

    • asc.js
    • asinit.js
    opened by mrod502 6
  • Coverage flag not working

    Coverage flag not working

    The coverage flag update in 0.2.3 does not seem to be working. I get the following error when using -c

    image

    This is the same if I try to run naively on Linux or in a dockerfile similar to the subgraph-demo setup

    config:

    "@graphprotocol/graph-cli": "0.25.1",
    "@graphprotocol/graph-ts": "0.24.1",
    "matchstick-as": "0.2.3",
    
    opened by ghardin1314 6
  • Cannot make it work on Mac

    Cannot make it work on Mac

    Hey hi, not sure where to ask for help.

    I'm basically trying to run Matchstick, which by the way amazing project. But I'm getting the following

    Screenshot 2022-04-08 at 20 51 36 Screenshot 2022-04-08 at 20 52 05

    Not sure what to do.

    I have a Mac M1 but I was even trying to make it work in Ubuntu and the same problem.

    Can you help please? Thanks

    opened by HetmanJones 5
  • what should I do when encounter ERROR TS2554?

    what should I do when encounter ERROR TS2554?

    what should I do when encounter this error?

    ERROR TS2554: Expected 15 arguments, but got 14.
    
     let defaultBlock = new ethereum.Block(defaultAddressBytes, defaultAddressBytes, defaultAddressBytes, defaultAddress,
        defaultAddressBytes, defaultAddressBytes, defaultAddressBytes, defaultBigInt, defaultBigInt,
        defaultBigInt, defaultBigInt, defaultBigInt, defaultBigInt, defaultBigInt);
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     in ~lib/matchstick-as/assembly/defaults.ts(6,20)
    
    ERROR TS2554: Expected 9 arguments, but got 8.
    
     let defaultTransaction = new ethereum.Transaction(defaultAddressBytes, defaultBigInt, defaultAddress,
        defaultAddress, defaultBigInt, defaultBigInt, defaultBigInt, defaultAddressBytes);
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     in ~lib/matchstick-as/assembly/defaults.ts(9,26)
    
    FAILURE 2 compile error(s)
    thread 'main' panicked at 'πŸ†˜ Critical: Please attend to the compilation errors above!', src/main.rs:158:9
    
    opened by waheya 5
  • Display full path of each failed test at the bottom of the output log

    Display full path of each failed test at the bottom of the output log

    Try to find a better solution to accumulate and display the full path for each failed test, something like in the screenshot below: Screenshot 2021-12-01 at 10 27 06

    The solution I came up with initially here is not reliable enough and does not handle some cases, which is a problem.

    Research Estimate: 2-3 days We can provide After research is finished

    opened by dimitrovmaksim 5
  • Can't find node_modules if it is not in the same level as matchstick.yaml file

    Can't find node_modules if it is not in the same level as matchstick.yaml file

    This error will get thrown when he can't find the node_modules folder. thread 'main' panicked at 'πŸ†˜ Path to lib "./node_modules" does not exist!', src/compiler/mod.rs:37:13 I'm in a monorepo where the node modules folder is in the root. But since my tests are in a package sub dir, I can't run these tests. Can the matchstick.yaml file be extended to provide a custom path to the node modules folder?

    opened by fritzschoff 4
  • Matchstick attempts to download

    Matchstick attempts to download "undefined" version

    Matchstick version: "matchstick-as": "^0.5.4"

    After running yarn test, matchstick attempts to download a binary with undefined in the path

    https://github.com/LimeChain/matchstick/releases/download/undefined/binary-macos-11 returns a 404

    it should be https://github.com/LimeChain/matchstick/releases/download/0.5.4/binary-macos-11

    (base) ~/OpenQ-Fullstack/OpenQ-Graph > yarn test
    yarn run v1.22.17
    warning ../../package.json: No license field
    $ graph test
    Fetching latest version tag
    Downloading release from https://github.com/LimeChain/matchstick/releases/download/undefined/binary-macos-11
    Error fetching release: Request failed with status code 404
    error Command failed with exit code 1.
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    
    opened by FlacoJones 3
  • Unmocked function call does not provide information on the test case that caused it

    Unmocked function call does not provide information on the test case that caused it

    Running matchstick-as 0.5.0, if there is a call that has not been mocked, the CLI output does not provide any information/context on which test case (ideally, which line in the source code!) caused it. All that is output is:

    thread 'main' panicked at 'πŸ†˜ Could not find a mocked function for function with address: 0xbd0d…5660, name: lp_token, signature lp_token():(address), params: [].', src/context.rs:849:13

    It would be far more helpful if that context was given. Thanks!

    opened by 0xJem 3
  • Unable to use library on ubunt 22.04 due to libssl1.1 dependancy.

    Unable to use library on ubunt 22.04 due to libssl1.1 dependancy.

    Ubuntu 22.04 only includes libssl3 (not libssl.1.1)

    matchstick: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or director
    

    Same goes for libcrypto.so.1.1.

    If I link the libraries(sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so.3 /usr/lib/x86_64-linux-gnu/libssl.so.1.1, sudo ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.3 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1) I get the following error:

    ../matchstick: /lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_0' not found (required by ../matchstick)
    ../matchstick: /lib/x86_64-linux-gnu/libcrypto.so.1.1: version `OPENSSL_1_1_0' not found (required by ../matchstick)
    

    Only solution is to install an officially unsuported version:

    wget archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
    sudo dpkg -i ./libssl1.1_1.1.1f-1ubuntu2_amd64.deb
    
    opened by JasoonS 3
  • @graphprotocol/graph-ts types are not assignable to matchstick-as types

    @graphprotocol/graph-ts types are not assignable to matchstick-as types

    I'm trying to write some tests with assertions like addressEquals and bigIntEquals because the fieldEquals assertion seems unnecessarily verbose and repetitive. However, when using these assertions compilation fails with type errors like Address is not assignable to Address and BigInt is not assignable to BigInt:

    ERROR TS2322: Type '~lib/@graphprotocol/graph-ts/common/numbers/Address' is not assignable to type '~lib/matchstick-as/node_modules/@graphprotocol/graph-ts/common/numbers/Address'.
    
                     Address.fromBytes(nftListedEvent.nftAddress),
    
    ERROR TS2322: Type '~lib/@graphprotocol/graph-ts/common/numbers/BigInt' is not assignable to type '~lib/matchstick-as/node_modules/@graphprotocol/graph-ts/common/numbers/BigInt'.
    
                 assert.bigIntEquals(nftListedEvent.nftId, listing.nft.id)
    

    I would expect the types used in the latest version of the matchstick-as package to match the types used in the latest version of the @graphprotocol/graph-ts where I import Address and BigInt from, but it seems this is not the case currently. I'm using the latest versions of @graphprotocol/[email protected], @graphprotocol/[email protected], and [email protected]. It does look like there's a 0.5.4 version of matchstick tagged on github, but that hasn't been published to npm.

    Code for reference:

    import { describe, test, assert, afterAll, clearStore } from 'matchstick-as'
    import { Address, BigInt } from '@graphprotocol/graph-ts'
    import { NftListedEvent } from '../generated/schema'
    import {
        handleNftListed,
        getEventId
    } from '../src/nifty'
    import { createNftListedEvent } from './nifty-utils'
    
    // No object literals or destructuring in AssemblyScript is rough >.<
    
    class Nft {
        address: Address
        id: BigInt
    
        constructor(address: Address, id: BigInt) {
            this.address = address
            this.id = id
        }
    }
    
    class Listing {
        nft: Nft
        price: BigInt
        seller: Address
    
        constructor(nft: Nft, price: BigInt, seller: Address) {
            this.nft = nft
            this.price = price
            this.seller = seller
        }
    }
    
    let listing = new Listing(
        new Nft(
            Address.fromString('0x0000000000000000000000000000000000000001'),
            BigInt.fromI32(0)
        ),
        BigInt.fromI32(100),
        Address.fromString('0x0000000000000000000000000000000000000002')
    )
    
    describe('Events', () => {
        afterAll(() => {
            clearStore()
        })
    
        describe('NftListed', () => {
            test('stores an NftListedEvent entity', () => {
                let event = createNftListedEvent(
                    listing.nft.address,
                    listing.nft.id,
                    listing.price,
                    listing.seller
                )
                handleNftListed(event)
    
                let nftListedEvent = NftListedEvent.load(getEventId(event))
    
                if (nftListedEvent == null) {
                    throw new Error('NftListedEvent not found')
                }
    
                assert.entityCount('NftListedEvent', 1)
                assert.addressEquals(
                    Address.fromBytes(nftListedEvent.nftAddress),
                    listing.nft.address
                )
                assert.bigIntEquals(nftListedEvent.nftId, listing.nft.id)
                assert.bigIntEquals(nftListedEvent.price, listing.price)
                assert.addressEquals(
                    Address.fromBytes(nftListedEvent.seller),
                    listing.seller
                )
            })
        })
    })
    
    opened by scherroman 9
  • @derivedFrom unexpected null

    @derivedFrom unexpected null

    Given the following schema

    holders: [OfferHolder!]! @derivedFrom(field: "offer")
    

    If there are no holders shouldn't it return an empty array? I have an older subgraph that works in this way and couldn't find any documentation on if this has changed?

    With the above calling

    offers.holders.length
    

    results in an unexpected null error, alternatively if I make the field look this way

    holders: [OfferHolder!] @derivedFrom(field: "offer")
    

    It always returns null even if there are holders present

    opened by jbrumwell 2
  • Add a command to the hardhat plugin to run a local graph-node using firehose

    Add a command to the hardhat plugin to run a local graph-node using firehose

    Also important to check what dependencies completing this task has - if it requires more than just the sfeth dependency to run firehose (like for example the geth node) revisit this assignment with someone from the graph. Firehose integration should be as decoupled as possible.

    invalid 
    opened by georg-getz 0
  • Implement the trace handler

    Implement the trace handler

    This relies on #364 being done

    May require you to create a 1 transaction blocks if the traces given are per transaction and not per block since firehose only works with bundles of blocks.

    invalid 
    opened by georg-getz 0
Releases(0.5.4)
Owner
null
Testing Framework for Rust

Polish Polish is Test-Driven Development done right Getting Started Installing the Package The crates.io package is kept up-to-date with all the major

Fadi Hanna Al-Kass 49 Dec 18, 2022
Declarative Testing Framework

Demonstrate allows tests to be written without as a much repetitive code within the demonstrate! macro, which will generate the corresponding full tests.

Austin Baugh 41 Aug 17, 2022
Cucumber testing framework for Rust. Fully native, no external test runners or dependencies.

An implementation of the Cucumber testing framework for Rust. Fully native, no external test runners or dependencies.

Brendan Molloy 394 Jan 1, 2023
Testing Framework for Rust

Polish Polish is Test-Driven Development done right Getting Started Installing the Package The crates.io package is kept up-to-date with all the major

Fadi Hanna Al-Kass 49 Dec 18, 2022
ArchTest is a rule based architecture testing tool for rust

ArchTest is a rule based architecture testing tool. It applies static analyses on the specified rust project to extract use relationships.

Tom Dymel 7 Sep 26, 2021
Rnp - A simple cloud-friendly tool for testing network reachability.

Rnp - A simple cloud-friendly tool for testing network reachability. Release Status Crates.io Github release Nuget packages NOTE: This project is in e

Riff 50 Dec 13, 2022
Automated property based testing for Rust (with shrinking).

quickcheck QuickCheck is a way to do property based testing using randomly generated input. This crate comes with the ability to randomly generate and

Andrew Gallant 2k Jan 2, 2023
Hypothesis-like property testing for Rust

Proptest Introduction Proptest is a property testing framework (i.e., the QuickCheck family) inspired by the Hypothesis framework for Python. It allow

Jason Lingle 1.1k Jan 1, 2023
Viceroy provides local testing for developers working with Compute@Edge.

Viceroy provides local testing for developers working with Compute@Edge. It allows you to run services written against the Compute@Edge APIs on your local development machine, and allows you to configure testing backends for your service to communicate with.

Fastly 99 Jan 7, 2023
Simple goldenfile testing in Rust.

?? Rust Goldenfile Simple goldenfile testing in Rust. Goldenfile tests generate one or more output files as they run. At the end of the test, the gene

Calder Coalson 24 Nov 26, 2022
🧡 Generate self-describing strings of a given length to help aid software testing

rust-counter-strings Counter strings generator written in rust to help aid software testing What is a counterstring? "A counterstring is a graduated s

Thomas Chaplin 23 Jun 24, 2022
Loom is a concurrency permutation testing tool for Rust.

Loom is a testing tool for concurrent Rust code

Tokio 1.4k Jan 9, 2023
Drill is an HTTP load testing application written in Rust inspired by Ansible syntax

Drill is an HTTP load testing application written in Rust inspired by Ansible syntax

Ferran Basora 1.5k Jan 1, 2023
assay - A super powered testing macro for Rust

assay - A super powered testing macro for Rust as·say /ˈaˌsā,aˈsā/ noun - the testing of a metal or ore to determine its ingredients and quality. Rust

Michael Gattozzi 105 Dec 4, 2022
This is a tiny (but delightful!) utility library for exhaustive testing.

Exhaustigen This is a tiny (but delightful!) utility library for exhaustive testing. It is based (directly) on the idea and code in the following blog

Graydon Hoare 34 Dec 14, 2022
Rust testing library

K9 - Rust Testing Library Snapshot testing + better assertions Available test macros snapshot assert_equal assert_greater_than assert_greater_than_or_

Aaron Abramov 269 Dec 10, 2022
Rustress - stress testing library in Rust. For fun

rustress Simple network stress testing library. To get familiar with Rust Planned features (Subject to change) Multithreaded client/server Throughput

Hakan SΓΆnmez 7 Sep 22, 2022
A series of utility macros for outputting testing results.

test-results A series of utility macros for outputting testing results. Getting Started Simply add the test-results crate to your project's Cargo.toml

EYHN 1 Jan 27, 2022
hb is an endpoint focused HTTP load testing / benchmark tool.

hb hb is an endpoint focused HTTP load testing / benchmark tool. Description The goal of hb is to provide a simple, robust tool to apply load against

Mark Pritchard 2 Aug 23, 2022