Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve subgraph tests #10

Merged
merged 10 commits into from
Apr 2, 2024
2 changes: 1 addition & 1 deletion packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@graphprotocol/graph-ts": "^0.31.0",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"matchstick-as": "^0.5.2",
"matchstick-as": "^0.6.0",
"mustache": "^4.2.0",
"ts-morph": "^17.0.1",
"ts-node": "^10.9.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/subgraph/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ProposalCreated,
ProposalExecuted,
} from '../../generated/templates/Plugin/Admin';
import {EXECUTE_PROPOSAL_PERMISSION_HASH} from '../utils/constants';
import {EXECUTE_PROPOSAL_PERMISSION_ID} from '../utils/constants';
import {generateAdministratorAdminPluginEntityId} from '../utils/ids';
import {
generateActionEntityId,
Expand Down Expand Up @@ -110,6 +110,6 @@ export function handleMembershipContractAnnounced(
): void {
const context = new DataSourceContext();
context.setString('pluginAddress', event.address.toHexString());
context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_HASH);
context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_ID);
AdminMembers.createWithContext(event.params.definingContract, context);
}
2 changes: 1 addition & 1 deletion packages/subgraph/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// keccack256 of EXECUTE_PROPOSAL_PERMISSION
export const EXECUTE_PROPOSAL_PERMISSION_HASH =
export const EXECUTE_PROPOSAL_PERMISSION_ID =
'0xf281525e53675515a6ba7cc7bea8a81e649b3608423ee2d73be1752cea887889';
54 changes: 35 additions & 19 deletions packages/subgraph/tests/plugin/adminMembers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Administrator, AdministratorAdminPlugin} from '../../generated/schema';
import {handleGranted, handleRevoked} from '../../src/plugin/adminMembers';
import {EXECUTE_PROPOSAL_PERMISSION_HASH} from '../../src/utils/constants';
import {EXECUTE_PROPOSAL_PERMISSION_ID} from '../../src/utils/constants';
import {generateAdministratorAdminPluginEntityId} from '../../src/utils/ids';
import {ADDRESS_ONE, ADDRESS_TWO, DAO_ADDRESS} from '../utils/constants';
import {createGrantedEvent, createRevokedEvent} from '../utils/events/plugin';
Expand All @@ -17,17 +17,16 @@ import {
} from 'matchstick-as/assembly/index';

const adminAddress = Address.fromString(ADDRESS_ONE);
const adminEntityId = generateEntityIdFromAddress(adminAddress);
const administratorEntityId = generateEntityIdFromAddress(adminAddress);
const pluginAddress = Address.fromString(ADDRESS_TWO);
const pluginEntityId = generateEntityIdFromAddress(pluginAddress);

describe('AdminMembers', function () {
// keccack256 of EXECUTE_PROPOSAL_PERMISSION
const AdminPermission = EXECUTE_PROPOSAL_PERMISSION_HASH;

beforeEach(function () {
let context = new DataSourceContext();
context.setString('permissionId', AdminPermission);
context.setString('permissionId', EXECUTE_PROPOSAL_PERMISSION_ID);
context.setString('pluginAddress', pluginEntityId);
dataSourceMock.setContext(context);
});
Expand All @@ -37,25 +36,36 @@ describe('AdminMembers', function () {
});

test('handleGranted', function () {
// check the entities are not in the store
assert.entityCount('Administrator', 0);
assert.entityCount('AdministratorAdminPlugin', 0);

// create the event and handle it
let event = createGrantedEvent(
EXECUTE_PROPOSAL_PERMISSION_ID,
DAO_ADDRESS,
pluginEntityId,
adminEntityId,
AdminPermission
administratorEntityId
);
handleGranted(event);

// check the administrator entity
assert.entityCount('Administrator', 1);
assert.fieldEquals('Administrator', adminEntityId, 'id', adminEntityId);
assert.fieldEquals(
'Administrator',
adminEntityId,
administratorEntityId,
'id',
administratorEntityId
);
assert.fieldEquals(
'Administrator',
administratorEntityId,
'address',
adminEntityId
administratorEntityId
);

// check the mapping with the admin pluging entity
assert.entityCount('AdministratorAdminPlugin', 1);

let administratorAdminPluginId = generateAdministratorAdminPluginEntityId(
pluginAddress,
adminAddress
Expand All @@ -70,7 +80,7 @@ describe('AdminMembers', function () {
'AdministratorAdminPlugin',
administratorAdminPluginId,
'administrator',
adminEntityId
administratorEntityId
);
assert.fieldEquals(
'AdministratorAdminPlugin',
Expand All @@ -81,8 +91,8 @@ describe('AdminMembers', function () {
});

test('handleRevoked', function () {
let administrator = new Administrator(adminEntityId);
administrator.address = adminEntityId;
let administrator = new Administrator(administratorEntityId);
administrator.address = administratorEntityId;
administrator.save();

let administratorAdminPluginId = generateAdministratorAdminPluginEntityId(
Expand All @@ -92,20 +102,26 @@ describe('AdminMembers', function () {
let administratorAdminPluginEntity = new AdministratorAdminPlugin(
administratorAdminPluginId
);
administratorAdminPluginEntity.administrator = ADDRESS_TWO;
administratorAdminPluginEntity.plugin = ADDRESS_ONE;
administratorAdminPluginEntity.administrator = administratorEntityId;
administratorAdminPluginEntity.plugin = pluginEntityId;
administratorAdminPluginEntity.save();

// check the entities are in the store
assert.entityCount('Administrator', 1);
assert.entityCount('AdministratorAdminPlugin', 1);

// create revoke event and handle it
let revokedEvent = createRevokedEvent(
EXECUTE_PROPOSAL_PERMISSION_ID,
DAO_ADDRESS,
adminEntityId,
pluginEntityId,
AdminPermission
administratorEntityId
);

handleRevoked(revokedEvent);

// when revoking the permission the admin is not removed, only the mapping with the admin plugin
assert.entityCount('Administrator', 1);
// assert.notInStore('AdministratorAdminPlugin', administratorAdminPluginId);
assert.entityCount('AdministratorAdminPlugin', 0);
assert.notInStore('AdministratorAdminPlugin', administratorAdminPluginId);
});
});
38 changes: 34 additions & 4 deletions packages/subgraph/tests/plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Action} from '../../generated/schema';
import {
handleProposalExecuted,
_handleProposalCreated,
handleMembershipContractAnnounced,
} from '../../src/plugin/plugin';
import {
ADDRESS_ONE,
Expand All @@ -16,6 +17,7 @@ import {
import {
createNewProposalCreatedEvent,
createProposalExecutedEvent,
createMembershipContractAnnouncedEvent,
createAdminPluginState,
createAdminProposalState,
} from '../utils/events/plugin';
Expand All @@ -31,6 +33,7 @@ import {
BigInt,
DataSourceContext,
} from '@graphprotocol/graph-ts';
import {dataSource} from '@graphprotocol/graph-ts';
import {
assert,
afterEach,
Expand Down Expand Up @@ -66,6 +69,10 @@ describe('Plugin', () => {

describe('handleProposalCreated', () => {
test('test the event', () => {
// check the entities are not in the store
assert.entityCount('AdminProposal', 0);
assert.entityCount('Action', 0);

// create state
createAdminPluginState(pluginEntityId);

Expand All @@ -89,7 +96,8 @@ describe('Plugin', () => {
BigInt.fromString(PLUGIN_PROPOSAL_ID)
);

// checks
// checks proposal
assert.entityCount('AdminProposal', 1);
assert.fieldEquals(
'AdminProposal',
proposalEntityId,
Expand Down Expand Up @@ -158,6 +166,7 @@ describe('Plugin', () => {
);

// check actions
assert.entityCount('Action', 1);
const actionEntityId = generateActionEntityId(proposalEntityId, 0);
const actionEntity = Action.load(actionEntityId);
if (actionEntity) {
Expand All @@ -173,8 +182,6 @@ describe('Plugin', () => {
proposalEntityId
);
}

clearStore();
});
});

Expand All @@ -196,6 +203,8 @@ describe('Plugin', () => {
action.proposal = proposalEntityId;
action.save();

assert.entityCount('AdminProposal', 1);

// create event
let event = createProposalExecutedEvent(
PLUGIN_PROPOSAL_ID,
Expand All @@ -219,8 +228,29 @@ describe('Plugin', () => {
'executionTxHash',
event.transaction.hash.toHexString()
);
});
});

describe('handleMembershipContractAnnounced', () => {
test('test the event', () => {
let context = dataSource.context();

assert.dataSourceCount('AdminMembers', 0);
assert.assertNull(context.get('pluginAddress'));
assert.assertNull(context.get('permissionId'));

// create event
let event = createMembershipContractAnnouncedEvent(
DAO_ADDRESS,
pluginAddress
);

// handle event
handleMembershipContractAnnounced(event);

clearStore();
// check
assert.dataSourceCount('AdminMembers', 1);
assert.dataSourceExists('AdminMembers', DAO_ADDRESS);
Rekard0 marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
32 changes: 25 additions & 7 deletions packages/subgraph/tests/utils/events/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import {
import {
ProposalCreated,
ProposalExecuted,
MembershipContractAnnounced,
} from '../../../generated/templates/Plugin/Admin';
import {
ADDRESS_ZERO,
ADDRESS_ONE,
ADDRESS_TWO,
STRING_DATA,
PLUGIN_PROPOSAL_ID,
START_DATE,
ALLOW_FAILURE_MAP,
CONTRACT_ADDRESS,
DAO_ADDRESS,
} from '../constants';
import {AdminMembers} from './../../../generated/templates';
import {Address, BigInt, Bytes, ethereum} from '@graphprotocol/graph-ts';
import {newMockEvent} from 'matchstick-as';

Expand Down Expand Up @@ -99,10 +97,10 @@ export function createProposalExecutedEvent(
}

export function createGrantedEvent(
permissionId: string,
dao: string,
plugin: string,
member: string,
permissionId: string
member: string
): Granted {
let newGrantedEvent = changetype<Granted>(newMockEvent());

Expand Down Expand Up @@ -140,10 +138,10 @@ export function createGrantedEvent(
}

export function createRevokedEvent(
permissionId: string,
dao: string,
plugin: string,
member: string,
permissionId: string
member: string
): Revoked {
let newRevokedEvent = changetype<Revoked>(newMockEvent());

Expand Down Expand Up @@ -175,6 +173,26 @@ export function createRevokedEvent(
return newRevokedEvent;
}

export function createMembershipContractAnnouncedEvent(
definingContract: string,
contractAddress: Address
): MembershipContractAnnounced {
let newMembershipContractAnnouncedEvent =
changetype<MembershipContractAnnounced>(newMockEvent());

newMembershipContractAnnouncedEvent.address = contractAddress;
newMembershipContractAnnouncedEvent.parameters = [];

let definingContractParam = new ethereum.EventParam(
'definingContract',
ethereum.Value.fromAddress(Address.fromString(definingContract))
);

newMembershipContractAnnouncedEvent.parameters.push(definingContractParam);

return newMembershipContractAnnouncedEvent;
}

// state

export function createAdminPluginState(
Expand Down
8 changes: 4 additions & 4 deletions packages/subgraph/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2702,10 +2702,10 @@ make-error@^1.1.1:
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

matchstick-as@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/matchstick-as/-/matchstick-as-0.5.2.tgz#6a6dde02d1d939c32458bd67bac688891a07a34c"
integrity sha512-fb1OVphDKEvJY06Ue02Eh1CNncuW95vp6b8tNAP7UIqplICSLoU/zgN6U7ge7R0upsoO78C7CRi4EyK/7Jxz7g==
matchstick-as@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/matchstick-as/-/matchstick-as-0.6.0.tgz#c65296b1f51b1014d605c52067d9b5321ea630e8"
integrity sha512-E36fWsC1AbCkBFt05VsDDRoFvGSdcZg6oZJrtIe/YDBbuFh8SKbR5FcoqDhNWqSN+F7bN/iS2u8Md0SM+4pUpw==
dependencies:
wabt "1.0.24"

Expand Down
Loading