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 20, 2023
1 parent f6cd610 commit 4b0ca41
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ generated
subgraph.yaml
.bin
.latest.json
.docker

# Misc
.DS_Store
45 changes: 23 additions & 22 deletions pnpm-lock.yaml

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

8 changes: 4 additions & 4 deletions subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"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",
"test": "graph test -v 0.5.2"
"test": "graph test"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.59.0",
"@graphprotocol/graph-ts": "0.30.0",
"matchstick-as": "0.5.2",
"@graphprotocol/graph-cli": "0.60.0",
"@graphprotocol/graph-ts": "0.31.0",
"matchstick-as": "0.6.0",
"assemblyscript": "0.19.10"
}
}
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
2 changes: 1 addition & 1 deletion subgraph/src/schema-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SchemaCreated as SchemaCreatedEvent } from "../generated/SchemaRegistry
import { Counter, Schema } from "../generated/schema";

export function handleSchemaCreated(event: SchemaCreatedEvent): void {
const schema = new Schema(event.params.id.toString());
const schema = new Schema(event.params.id.toHexString());

incrementSchemasCount();

Expand Down
39 changes: 30 additions & 9 deletions subgraph/tests/module-registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, assert, clearStore, describe, log, test } from "matchstick-as";
import { afterEach, assert, clearStore, describe, test } from "matchstick-as";
import {
ModuleRegistered as ModuleRegisteredEvent,
ModuleRegistered,
Expand All @@ -8,7 +8,7 @@ import { newTypedMockEvent } from "matchstick-as/assembly/defaults";
import { handleModuleRegistered } from "../src/module-registry";

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

Expand All @@ -25,10 +25,31 @@ describe("handleModuleRegistered()", () => {

assert.entityCount("Module", 1);

assert.fieldEquals("Module", moduleAddress, "id", moduleAddress);
assert.fieldEquals("Module", moduleAddress, "name", moduleName);
assert.fieldEquals("Module", moduleAddress, "description", moduleDescription);
assert.fieldEquals("Module", moduleAddress, "moduleAddress", moduleAddress);
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");
});
});

Expand All @@ -39,12 +60,12 @@ function createModuleRegisteredEvent(
): ModuleRegistered {
const moduleRegisteredEvent = newTypedMockEvent<ModuleRegisteredEvent>();

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

return moduleRegisteredEvent;
Expand Down
100 changes: 100 additions & 0 deletions subgraph/tests/portal-registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { afterEach, assert, beforeAll, clearStore, createMockedFunction, describe, test } from "matchstick-as";
import { Address, ethereum } from "@graphprotocol/graph-ts";
import { newTypedMockEvent } from "matchstick-as/assembly/defaults";
import { handlePortalRegistered } from "../src/portal-registry";
import { PortalRegistered as PortalRegisteredEvent, PortalRegistry } from "../generated/PortalRegistry/PortalRegistry";

const portalRegistryAddress = Address.fromString("506f88a5Ca8D5F001f2909b029738A40042e42a6");

describe("handlePortalRegistered()", () => {
const portalAddress = "f75be6f9418710fd516fa82afb3aad07e11a0f1b";
const ownerAddress = "e75be6f9418710fd516fa82afb3aad07e11a0f1b";
const ownerName = "Verax";
const portalName = "portal name";
const portalDescription = "portal description";

beforeAll(() => {
const tupleArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(Address.fromString(portalAddress)),
ethereum.Value.fromAddress(Address.fromString(ownerAddress)),
ethereum.Value.fromAddressArray([Address.zero()]),
ethereum.Value.fromBoolean(true),
ethereum.Value.fromString(portalName),
ethereum.Value.fromString(portalDescription),
ethereum.Value.fromString(ownerName),
];
const tuple = changetype<ethereum.Tuple>(tupleArray);
const tupleValue = ethereum.Value.fromTuple(tuple);

createMockedFunction(
portalRegistryAddress,
"getPortalByAddress",
"getPortalByAddress(address):((address,address,address[],bool,string,string,string))",
)
.withArgs([ethereum.Value.fromAddress(Address.fromString(portalAddress))])
.returns([tupleValue]);

const contract = PortalRegistry.bind(Address.fromString(portalAddress));
const result = contract.getPortalByAddress(Address.fromString(portalAddress));

assert.tupleEquals(tuple, result);
});

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

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

const portalRegisteredEvent = createPortalRegisteredEvent(portalAddress, portalName, portalDescription);

handlePortalRegistered(portalRegisteredEvent);

assert.entityCount("Portal", 1);

assert.fieldEquals("Portal", "0x" + portalAddress, "id", "0x" + portalAddress);
assert.fieldEquals("Portal", "0x" + portalAddress, "name", portalName);
assert.fieldEquals("Portal", "0x" + portalAddress, "description", portalDescription);
});

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

const portalAddress1 = "f75be6f9418710fd516fa82afb3aad07e11a0f1b";
const portalAddress2 = "e75be6f9418710fd516fa82afb3aad07e11a0f1b";

const portalRegisteredEvent1 = createPortalRegisteredEvent(portalAddress1, portalName, portalDescription);

handlePortalRegistered(portalRegisteredEvent1);

assert.entityCount("Portal", 1);
assert.fieldEquals("Counter", "counter", "portals", "1");

const portalRegisteredEvent2 = createPortalRegisteredEvent(portalAddress2, portalName, portalDescription);

handlePortalRegistered(portalRegisteredEvent2);

assert.entityCount("Portal", 2);
assert.fieldEquals("Counter", "counter", "portals", "2");
});
});

function createPortalRegisteredEvent(
portalAddress: string,
portalName: string,
portalDescription: string,
): PortalRegisteredEvent {
const portalRegisteredEvent = newTypedMockEvent<PortalRegisteredEvent>();
portalRegisteredEvent.address = portalRegistryAddress;

portalRegisteredEvent.parameters.push(new ethereum.EventParam("name", ethereum.Value.fromString(portalName)));
portalRegisteredEvent.parameters.push(
new ethereum.EventParam("description", ethereum.Value.fromString(portalDescription)),
);
portalRegisteredEvent.parameters.push(
new ethereum.EventParam("portalAddress", ethereum.Value.fromAddress(Address.fromString(portalAddress))),
);

return portalRegisteredEvent;
}
Loading

0 comments on commit 4b0ca41

Please sign in to comment.