Skip to content

Commit

Permalink
Add extra tests to the subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Sep 20, 2024
1 parent 3bd5735 commit 7bc7b57
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 22 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file modified contracts/councilhaus/subgraph/bun.lockb
Binary file not shown.
21 changes: 0 additions & 21 deletions contracts/councilhaus/subgraph/tests/council-factory-utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
test,
} from "matchstick-as/assembly/index";
import { handleCouncilCreated } from "../src/council-factory";
import { createCouncilCreatedEvent } from "./council-factory-utils";
import { createCouncilCreatedEvent } from "./utils";

describe("Describe entity assertions", () => {
beforeAll(() => {
Expand Down
233 changes: 233 additions & 0 deletions contracts/councilhaus/subgraph/tests/council.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { Address, BigInt } from "@graphprotocol/graph-ts";
import {
assert,
beforeEach,
clearStore,
describe,
test,
} from "matchstick-as/assembly/index";
import {
handleBudgetAllocated,
handleCouncilMemberAdded,
handleCouncilMemberRemoved,
handleGranteeAdded,
handleGranteeRemoved,
} from "../src/council";
import {
createBudgetAllocatedEvent,
createCouncilMemberAddedEvent,
createCouncilMemberRemovedEvent,
createGranteeAddedEvent,
createGranteeRemovedEvent,
} from "./utils";

const COUNCIL_ADDRESS = "0x0000000000000000000000000000000000000001";

describe("Council entity assertions", () => {
beforeEach(() => {
clearStore();
});

test("CouncilMember added and stored", () => {
const memberAddress = Address.fromString(
"0x0000000000000000000000000000000000000002",
);
const votingPower = BigInt.fromI32(100);
const newCouncilMemberAddedEvent = createCouncilMemberAddedEvent(
memberAddress,
votingPower,
Address.fromString(COUNCIL_ADDRESS),
);

handleCouncilMemberAdded(newCouncilMemberAddedEvent);

assert.entityCount("CouncilMember", 1);
assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"account",
memberAddress.toHexString(),
);
assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"votingPower",
votingPower.toString(),
);
assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"council",
COUNCIL_ADDRESS,
);
assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"enabled",
"true",
);
});

test("CouncilMember removed", () => {
// First, add a council member
const memberAddress = Address.fromString(
"0x0000000000000000000000000000000000000002",
);
const votingPower = BigInt.fromI32(100);
const addEvent = createCouncilMemberAddedEvent(
memberAddress,
votingPower,
Address.fromString(COUNCIL_ADDRESS),
);
handleCouncilMemberAdded(addEvent);

// Now remove the council member
const removeEvent = createCouncilMemberRemovedEvent(
memberAddress,
Address.fromString(COUNCIL_ADDRESS),
);
handleCouncilMemberRemoved(removeEvent);

assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"votingPower",
"0",
);
assert.fieldEquals(
"CouncilMember",
memberAddress.toHexString(),
"enabled",
"false",
);

// We check that running the handler again does not throw an error
handleCouncilMemberRemoved(removeEvent);
});

test("Grantee added and stored", () => {
const granteeAddress = Address.fromString(
"0x0000000000000000000000000000000000000003",
);
const granteeName = "Test Grantee";
const newGranteeAddedEvent = createGranteeAddedEvent(
granteeAddress,
granteeName,
Address.fromString(COUNCIL_ADDRESS),
);

handleGranteeAdded(newGranteeAddedEvent);

assert.entityCount("Grantee", 1);
assert.fieldEquals(
"Grantee",
granteeAddress.toHexString(),
"name",
granteeName,
);
assert.fieldEquals(
"Grantee",
granteeAddress.toHexString(),
"account",
granteeAddress.toHexString(),
);
assert.fieldEquals(
"Grantee",
granteeAddress.toHexString(),
"council",
COUNCIL_ADDRESS,
);
assert.fieldEquals(
"Grantee",
granteeAddress.toHexString(),
"enabled",
"true",
);
});

test("Grantee removed", () => {
// First, add a grantee
const granteeAddress = Address.fromString(
"0x0000000000000000000000000000000000000003",
);
const granteeName = "Test Grantee";

const addEvent = createGranteeAddedEvent(
granteeAddress,
granteeName,
Address.fromString(COUNCIL_ADDRESS),
);
handleGranteeAdded(addEvent);

// // Now remove the grantee
const removeEvent = createGranteeRemovedEvent(
granteeAddress,
Address.fromString(COUNCIL_ADDRESS),
);
handleGranteeRemoved(removeEvent);

assert.fieldEquals(
"Grantee",
granteeAddress.toHexString(),
"enabled",
"false",
);

// We check that running the handler again does not throw an error
handleGranteeRemoved(removeEvent);
});

test("Budget allocated", () => {
// First, add a council member and a grantee
const memberAddress = Address.fromString(
"0x0000000000000000000000000000000000000002",
);
const granteeAddress = Address.fromString(
"0x0000000000000000000000000000000000000003",
);

handleCouncilMemberAdded(
createCouncilMemberAddedEvent(
memberAddress,
BigInt.fromI32(100),
Address.fromString(COUNCIL_ADDRESS),
),
);
handleGranteeAdded(
createGranteeAddedEvent(
granteeAddress,
"Test Grantee",
Address.fromString(COUNCIL_ADDRESS),
),
);

// Now allocate budget
const amounts = [BigInt.fromI32(1000)];
const accounts = [granteeAddress];
const newBudgetAllocatedEvent = createBudgetAllocatedEvent(
memberAddress,
amounts,
accounts,
Address.fromString(COUNCIL_ADDRESS),
);

handleBudgetAllocated(newBudgetAllocatedEvent);

assert.entityCount("Allocation", 1);
const allocationId = `${newBudgetAllocatedEvent.transaction.hash.toHex()}-${newBudgetAllocatedEvent.logIndex.toString()}`;
assert.fieldEquals("Allocation", allocationId, "council", COUNCIL_ADDRESS);
assert.fieldEquals(
"Allocation",
allocationId,
"councilMember",
memberAddress.toHexString(),
);
assert.fieldEquals("Allocation", allocationId, "amounts", "[1000]");
assert.fieldEquals(
"Allocation",
allocationId,
"grantees",
`[${granteeAddress.toHexString()}]`,
);
});
});
116 changes: 116 additions & 0 deletions contracts/councilhaus/subgraph/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts";
import { newMockEvent } from "matchstick-as";
import { CouncilCreated } from "../generated/CouncilFactory/CouncilFactory";
import {
BudgetAllocated,
CouncilMemberAdded,
CouncilMemberRemoved,
GranteeAdded,
GranteeRemoved,
} from "../generated/templates/Council/Council";

export function createCouncilCreatedEvent(
council: Address,
pool: Address,
): CouncilCreated {
const councilCreatedEvent = changetype<CouncilCreated>(newMockEvent());

councilCreatedEvent.parameters = new Array();

councilCreatedEvent.parameters.push(
new ethereum.EventParam("council", ethereum.Value.fromAddress(council)),
);
councilCreatedEvent.parameters.push(
new ethereum.EventParam("pool", ethereum.Value.fromAddress(pool)),
);

return councilCreatedEvent;
}

export function createCouncilMemberAddedEvent(
member: Address,
votingPower: BigInt,
council: Address,
): CouncilMemberAdded {
const event = changetype<CouncilMemberAdded>(newMockEvent());
event.address = council;
event.parameters = new Array();
event.parameters.push(
new ethereum.EventParam("member", ethereum.Value.fromAddress(member)),
);
event.parameters.push(
new ethereum.EventParam(
"votingPower",
ethereum.Value.fromUnsignedBigInt(votingPower),
),
);
return event;
}

export function createCouncilMemberRemovedEvent(
member: Address,
council: Address,
): CouncilMemberRemoved {
const event = changetype<CouncilMemberRemoved>(newMockEvent());
event.address = council;
event.parameters = new Array();
event.parameters.push(
new ethereum.EventParam("member", ethereum.Value.fromAddress(member)),
);
return event;
}

export function createGranteeAddedEvent(
grantee: Address,
name: string,
council: Address,
): GranteeAdded {
const event = changetype<GranteeAdded>(newMockEvent());
event.address = council;
event.parameters = new Array();
event.parameters.push(
new ethereum.EventParam("name", ethereum.Value.fromString(name)),
);
event.parameters.push(
new ethereum.EventParam("grantee", ethereum.Value.fromAddress(grantee)),
);
return event;
}

export function createGranteeRemovedEvent(
grantee: Address,
council: Address,
): GranteeRemoved {
const event = changetype<GranteeRemoved>(newMockEvent());
event.address = council;
event.parameters = new Array();
event.parameters.push(
new ethereum.EventParam("grantee", ethereum.Value.fromAddress(grantee)),
);
return event;
}

export function createBudgetAllocatedEvent(
member: Address,
amounts: BigInt[],
accounts: Address[],
council: Address,
): BudgetAllocated {
const event = changetype<BudgetAllocated>(newMockEvent());
event.address = council;
event.parameters = new Array();
event.parameters.push(
new ethereum.EventParam("member", ethereum.Value.fromAddress(member)),
);
const allocation: Array<ethereum.Value> = [
ethereum.Value.fromAddressArray(accounts),
ethereum.Value.fromUnsignedBigIntArray(amounts),
];
event.parameters.push(
new ethereum.EventParam(
"allocation",
ethereum.Value.fromTuple(changetype<ethereum.Tuple>(allocation)),
),
);
return event;
}

0 comments on commit 7bc7b57

Please sign in to comment.