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 21, 2023
1 parent c877819 commit 862d2a9
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 37 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ typechain-types
build
generated
subgraph.yaml
.bin
.latest.json
.docker

# Misc
.DS_Store
48 changes: 33 additions & 15 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"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.58.0",
"@graphprotocol/graph-ts": "0.31.0"
"@graphprotocol/graph-cli": "0.60.0",
"@graphprotocol/graph-ts": "0.31.0",
"matchstick-as": "0.6.0",
"assemblyscript": "0.19.10"
}
}
12 changes: 5 additions & 7 deletions subgraph/src/module-registry.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { ModuleRegistered as ModuleRegisteredEvent, ModuleRegistry } from "../generated/ModuleRegistry/ModuleRegistry";
import { ModuleRegistered as ModuleRegisteredEvent } from "../generated/ModuleRegistry/ModuleRegistry";
import { Counter, Module } from "../generated/schema";

export function handleModuleRegistered(event: ModuleRegisteredEvent): void {
const contract = ModuleRegistry.bind(event.address);
const moduleData = contract.modules(event.params.moduleAddress);
const module = new Module(event.params.moduleAddress.toHex());
const module = new Module(event.params.moduleAddress.toHexString());

incrementModulesCount();

module.moduleAddress = moduleData.getModuleAddress();
module.name = moduleData.getName();
module.description = moduleData.getDescription();
module.moduleAddress = event.params.moduleAddress;
module.name = event.params.name;
module.description = event.params.description;

module.save();
}
Expand Down
6 changes: 3 additions & 3 deletions subgraph/src/portal-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Counter, Portal } from "../generated/schema";
export function handlePortalRegistered(event: PortalRegisteredEvent): void {
const contract = PortalRegistry.bind(event.address);
const portalData = contract.getPortalByAddress(event.params.portalAddress);
const portal = new Portal(event.params.portalAddress.toHex());
const portal = new Portal(event.params.portalAddress.toHexString());

incrementPortalsCount();

portal.name = event.params.name;
portal.description = event.params.description;
portal.ownerAddress = portalData.ownerAddress;
portal.modules = changetype<Bytes[]>(portalData.modules);
portal.isRevocable = portalData.isRevocable;
portal.name = portalData.name;
portal.description = portalData.description;
portal.ownerName = portalData.ownerName;

portal.save();
Expand Down
14 changes: 6 additions & 8 deletions subgraph/src/schema-registry.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { SchemaCreated as SchemaCreatedEvent, SchemaRegistry } from "../generated/SchemaRegistry/SchemaRegistry";
import { SchemaCreated as SchemaCreatedEvent } from "../generated/SchemaRegistry/SchemaRegistry";
import { Counter, Schema } from "../generated/schema";

export function handleSchemaCreated(event: SchemaCreatedEvent): void {
const contract = SchemaRegistry.bind(event.address);
const schemaData = contract.getSchema(event.params.id);
const schema = new Schema(event.params.id.toHex());
const schema = new Schema(event.params.id.toHexString());

incrementSchemasCount();

schema.name = schemaData.name;
schema.description = schemaData.description;
schema.context = schemaData.context;
schema.schema = schemaData.schema;
schema.name = event.params.name;
schema.description = event.params.description;
schema.context = event.params.context;
schema.schema = event.params.schemaString;

schema.save();
}
Expand Down
72 changes: 72 additions & 0 deletions subgraph/tests/module-registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { afterEach, assert, clearStore, describe, test } from "matchstick-as";
import {
ModuleRegistered as ModuleRegisteredEvent,
ModuleRegistered,
} from "../generated/ModuleRegistry/ModuleRegistry";
import { Address, ethereum } from "@graphprotocol/graph-ts";
import { newTypedMockEvent } from "matchstick-as/assembly/defaults";
import { handleModuleRegistered } from "../src/module-registry";

describe("handleModuleRegistered()", () => {
const moduleAddress = "f75be6f9418710fd516fa82afb3aad07e11a0f1b";
const moduleName = "module name";
const moduleDescription = "module description";

afterEach(() => {
clearStore();
});

test("Should create a new Module entity", () => {
assert.entityCount("Module", 0);

const moduleRegisteredEvent = createModuleRegisteredEvent(moduleAddress, moduleName, moduleDescription);

handleModuleRegistered(moduleRegisteredEvent);

assert.entityCount("Module", 1);

assert.fieldEquals("Module", "0x" + moduleAddress, "id", "0x" + moduleAddress);
assert.fieldEquals("Module", "0x" + moduleAddress, "name", moduleName);
assert.fieldEquals("Module", "0x" + moduleAddress, "description", moduleDescription);
assert.fieldEquals("Module", "0x" + moduleAddress, "moduleAddress", "0x" + moduleAddress);
});

test("Should increment the modules Counter", () => {
assert.entityCount("Module", 0);

const moduleAddress1 = "f75be6f9418710fd516fa82afb3aad07e11a0f1b";
const moduleAddress2 = "e75be6f9418710fd516fa82afb3aad07e11a0f1b";

const moduleRegisteredEvent1 = createModuleRegisteredEvent(moduleAddress1, moduleName, moduleDescription);

handleModuleRegistered(moduleRegisteredEvent1);

assert.entityCount("Module", 1);
assert.fieldEquals("Counter", "counter", "modules", "1");

const moduleRegisteredEvent2 = createModuleRegisteredEvent(moduleAddress2, moduleName, moduleDescription);

handleModuleRegistered(moduleRegisteredEvent2);

assert.entityCount("Module", 2);
assert.fieldEquals("Counter", "counter", "modules", "2");
});
});

function createModuleRegisteredEvent(
moduleAddress: string,
moduleName: string,
moduleDescription: string,
): ModuleRegistered {
const moduleRegisteredEvent = newTypedMockEvent<ModuleRegisteredEvent>();

moduleRegisteredEvent.parameters.push(new ethereum.EventParam("name", ethereum.Value.fromString(moduleName)));
moduleRegisteredEvent.parameters.push(
new ethereum.EventParam("description", ethereum.Value.fromString(moduleDescription)),
);
moduleRegisteredEvent.parameters.push(
new ethereum.EventParam("moduleAddress", ethereum.Value.fromAddress(Address.fromString(moduleAddress))),
);

return moduleRegisteredEvent;
}
Loading

0 comments on commit 862d2a9

Please sign in to comment.