Skip to content

Commit

Permalink
chore: Add unit test to the subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
alainncls committed Oct 12, 2023
1 parent c9283aa commit 4241bd0
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 26 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/subgraph.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Subgraph

on:
pull_request:
branches:
- main
- dev
- release/*
push:
branches:
- main
- dev
- release/*

jobs:
test:
runs-on: ubuntu-latest

defaults:
run:
working-directory: subgraph

steps:
- name: Check out the repo
uses: actions/checkout@v3

- name: Install Pnpm
uses: pnpm/action-setup@v2
with:
version: 8
run_install: false

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: pnpm

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Build the subgraph
run: pnpm run build:goerli

- name: Run the unit tests
run: pnpm run test

- name: Add test summary
run: |
echo "## Unit tests result" >> $GITHUB_STEP_SUMMARY
echo "✅ Passed" >> $GITHUB_STEP_SUMMARY
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ typechain-types
build
generated
subgraph.yaml
.bin
.latest.json

# Misc
.DS_Store
63 changes: 41 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
"deploy": "source .env && cp subgraph.mainnet.yaml subgraph.yaml && pnpm run build && graph deploy --network linea-mainnet --node $DEPLOY_ENDPOINT_MAINNET --headers \"{\\\"Authorization\\\": \\\"Basic $IPFS_IDENTIFIERS\\\"}\" --ipfs $IPFS_ENDPOINT --version-label v0.0.1 Consensys/linea-attestation-registry",
"deploy:goerli": "source .env && cp subgraph.goerli.yaml subgraph.yaml && pnpm run build:goerli && graph deploy --network linea-goerli --node $DEPLOY_ENDPOINT_GOERLI --headers \"{\\\"Authorization\\\": \\\"Basic $IPFS_IDENTIFIERS\\\"}\" --ipfs $IPFS_ENDPOINT --version-label v0.0.5 Consensys/linea-attestation-registry",
"remove": "source .env && graph remove --node $DEPLOY_ENDPOINT_MAINNET Consensys/linea-attestation-registry",
"remove:goerli": "source .env && graph remove --node $DEPLOY_ENDPOINT_GOERLI Consensys/linea-attestation-registry"
"remove:goerli": "source .env && graph remove --node $DEPLOY_ENDPOINT_GOERLI Consensys/linea-attestation-registry",
"test": "graph test -v 0.5.2"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.58.0",
"@graphprotocol/graph-ts": "0.31.0"
"@graphprotocol/graph-cli": "0.59.0",
"@graphprotocol/graph-ts": "0.30.0",
"matchstick-as": "0.5.2",
"assemblyscript": "0.19.10"
}
}
68 changes: 68 additions & 0 deletions subgraph/tests/module-registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { afterEach, assert, beforeEach, clearStore, describe, log, newMockEvent, test } from "matchstick-as";
import { ModuleRegistered } from "../generated/ModuleRegistry/ModuleRegistry";
import { Address, ethereum } from "@graphprotocol/graph-ts";
import { handleModuleRegistered } from "../src/module-registry";

describe("handleModuleRegistered()", () => {
beforeEach(() => {
log.info("beforeEach", []);
});

afterEach(() => {
clearStore();
log.info("afterEach", []);
});

test("Should be true", () => {
assert.assertTrue(true);
});

test("Should be true again", () => {
assert.assertTrue(true);
});

test("Should be failing", () => {
assert.equals(ethereum.Value.fromI32(12), ethereum.Value.fromI32(13));
});

test("Should create a new Module entity", () => {
assert.entityCount("Module", 0);
const moduleRegisteredEvent = createModuleRegisteredEvent(
12345,
"module name",
"this is a test module",
"0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7",
);

handleModuleRegistered(moduleRegisteredEvent);

assert.fieldEquals("Module", "12345", "id", "12345");
assert.fieldEquals("Module", "12345", "name", "module name");
assert.fieldEquals("Module", "12345", "description", "this is a test module");
assert.fieldEquals("Module", "12345", "moduleAddress", "0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7");
});
});

function createModuleRegisteredEvent(
id: i32,
name: string,
description: string,
moduleAddress: string,
): ModuleRegistered {
const moduleRegisteredEvent = changetype<ModuleRegistered>(newMockEvent());
moduleRegisteredEvent.parameters = [];
const idParam = new ethereum.EventParam("id", ethereum.Value.fromI32(id));
const nameParam = new ethereum.EventParam("name", ethereum.Value.fromString(name));
const descriptionParam = new ethereum.EventParam("description", ethereum.Value.fromString(description));
const moduleAddressParam = new ethereum.EventParam(
"moduleAddress",
ethereum.Value.fromAddress(Address.fromString(moduleAddress)),
);

moduleRegisteredEvent.parameters.push(idParam);
moduleRegisteredEvent.parameters.push(nameParam);
moduleRegisteredEvent.parameters.push(descriptionParam);
moduleRegisteredEvent.parameters.push(moduleAddressParam);

return moduleRegisteredEvent;
}
3 changes: 2 additions & 1 deletion subgraph/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "@graphprotocol/graph-ts/types/tsconfig.base.json"
"extends": "@graphprotocol/graph-ts/types/tsconfig.base.json",
"include": ["src", "tests"]
}

0 comments on commit 4241bd0

Please sign in to comment.