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 a0f3e81
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 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
27 changes: 23 additions & 4 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 Down Expand Up @@ -30,6 +30,22 @@ describe("handleModuleRegistered()", () => {
assert.fieldEquals("Module", moduleAddress, "description", moduleDescription);
assert.fieldEquals("Module", moduleAddress, "moduleAddress", moduleAddress);
});

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

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

handleModuleRegistered(moduleRegisteredEvent);

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

handleModuleRegistered(moduleRegisteredEvent);

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

function createModuleRegisteredEvent(
Expand All @@ -39,12 +55,15 @@ 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("moduleAddress", ethereum.Value.fromAddress(Address.fromString(moduleAddress))),
);*/
moduleRegisteredEvent.parameters.push(
new ethereum.EventParam("moduleDescription", ethereum.Value.fromString(moduleDescription)),
new ethereum.EventParam("moduleAddress", ethereum.Value.fromAddress(Address.zero())),
);

return moduleRegisteredEvent;
Expand Down
26 changes: 24 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,28 @@ 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 schemaCreatedEvent = createSchemaCreatedEvent(
schemaId,
schemaName,
schemaDescription,
schemaContext,
schemaString,
);

handleSchemaCreated(schemaCreatedEvent);

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

handleSchemaCreated(schemaCreatedEvent);

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

function createSchemaCreatedEvent(
Expand All @@ -48,7 +70,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 a0f3e81

Please sign in to comment.