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 da70d71
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
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 = "E85BE6f9418710fd516fA82Afb3AAD07e11a0f1b";

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
37 changes: 35 additions & 2 deletions subgraph/tests/schema-registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, assert, clearStore, describe, test } from "matchstick-as";
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { Bytes, ethereum } from "@graphprotocol/graph-ts";
import { newTypedMockEvent } from "matchstick-as/assembly/defaults";
import { handleSchemaCreated } from "../src/schema-registry";
import { SchemaCreated, SchemaCreated as SchemaCreatedEvent } from "../generated/SchemaRegistry/SchemaRegistry";
Expand Down Expand Up @@ -36,6 +36,39 @@ describe("handleSchemaCreated()", () => {
assert.fieldEquals("Schema", schemaId, "context", schemaContext);
assert.fieldEquals("Schema", schemaId, "schema", schemaString);
});

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

const schemaId1 = "0x7930a5ebfabdd4ef76bbb8cdcbc2225b6256d9511d9cf5ff0d6514c1bdb4d7dc";
const schemaId2 = "0x8930a5ebfabdd4ef76bbb8cdcbc2225b6256d9511d9cf5ff0d6514c1bdb4d7dc";

const schemaCreatedEvent1 = createSchemaCreatedEvent(
schemaId1,
schemaName,
schemaDescription,
schemaContext,
schemaString,
);

handleSchemaCreated(schemaCreatedEvent1);

assert.entityCount("Schema", 1);
assert.fieldEquals("Counter", "counter", "schemas", "1");

const schemaCreatedEvent2 = createSchemaCreatedEvent(
schemaId2,
schemaName,
schemaDescription,
schemaContext,
schemaString,
);

handleSchemaCreated(schemaCreatedEvent2);

assert.entityCount("Schema", 2);
assert.fieldEquals("Counter", "counter", "schemas", "2");
});
});

function createSchemaCreatedEvent(
Expand All @@ -48,7 +81,7 @@ function createSchemaCreatedEvent(
const schemaCreatedEvent = newTypedMockEvent<SchemaCreatedEvent>();

schemaCreatedEvent.parameters.push(
new ethereum.EventParam("id", ethereum.Value.fromI32(BigInt.fromString(schemaId).toI32())),
new ethereum.EventParam("id", ethereum.Value.fromBytes(Bytes.fromHexString(schemaId))),
);
schemaCreatedEvent.parameters.push(new ethereum.EventParam("name", ethereum.Value.fromString(schemaName)));
schemaCreatedEvent.parameters.push(
Expand Down

0 comments on commit da70d71

Please sign in to comment.