Skip to content

Commit

Permalink
First Tests for event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
nigeon committed Nov 6, 2023
1 parent cb294a1 commit 18fbb9f
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/subgraph/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
TallyApproval,
TallySet,
} from '../../generated/templates/Plugin/VocdoniVoting';
import {Address, dataSource} from '@graphprotocol/graph-ts';
import {Address, Bytes, dataSource} from '@graphprotocol/graph-ts';
import {log} from 'matchstick-as';

export function handlePluginSettingsUpdated(
event: PluginSettingsUpdated
Expand Down
93 changes: 93 additions & 0 deletions packages/subgraph/tests/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {handlePluginSettingsUpdated} from '../src/plugin/plugin';
import {createPlugin, pluginSettingsUpdatedEvent} from './utils';
import {Address, log} from '@graphprotocol/graph-ts';
import {
describe,
test,
afterEach,
clearStore,
assert,
logStore,
} from 'matchstick-as/assembly/index';

const PLUGIN_ENTITY_TYPE = 'Plugin';

describe('Events', () => {
afterEach(() => {
clearStore();
});

test('Plugin Creation and update', () => {
const firstDaoAString = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const firstPluginAString = '0x758b8178A9A4B7206D1f648c4a77C515CbaC7000';
const firstDaoTokenAString = '0xa66ab9d321273fb415ff2539d38dd1a6e8b78a25';
const firstPluginId = createPlugin(
Address.fromString(firstDaoAString),
Address.fromString(firstPluginAString)
);

let newPluginEvent = pluginSettingsUpdatedEvent(
firstPluginAString,
firstDaoAString,
true,
1,
1,
1,
1,
1,
firstDaoTokenAString,
'0x2a1290d5d0Dd792Ad8e1C257a691F24E97675644',
1
);
handlePluginSettingsUpdated(newPluginEvent);

const anotherDaoAString = '0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5';
const anotherPluginAString = '0x763c396673F9c391DCe3361A9A71C8E161388000';
const anotherDaoTokenAString = '0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e8';
const secondPluginId = createPlugin(
Address.fromString(anotherDaoAString),
Address.fromString(anotherPluginAString)
);

let anotherPluginEvent = pluginSettingsUpdatedEvent(
anotherPluginAString,
anotherDaoAString,
false,
1,
1,
1,
1,
1,
anotherDaoTokenAString,
'0x974CaA59e49682CdA0AD2bbe82983419A2ECC400',
1
);
handlePluginSettingsUpdated(anotherPluginEvent);

assert.fieldEquals(
PLUGIN_ENTITY_TYPE,
firstPluginId.toHexString(),
'daoTokenAddress',
firstDaoTokenAString
);
assert.fieldEquals(
PLUGIN_ENTITY_TYPE,
firstPluginId.toHexString(),
'onlyExecutionMultisigProposalCreation',
'true'
);

assert.fieldEquals(
PLUGIN_ENTITY_TYPE,
secondPluginId.toHexString(),
'daoTokenAddress',
anotherDaoTokenAString
);
assert.fieldEquals(
PLUGIN_ENTITY_TYPE,
secondPluginId.toHexString(),
'onlyExecutionMultisigProposalCreation',
'false'
);
});
});
93 changes: 93 additions & 0 deletions packages/subgraph/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { getPluginInstallationId } from '../commons/ids';
import { Plugin } from '../generated/schema';
import {PluginSettingsUpdated} from '../generated/templates/Plugin/VocdoniVoting';
import {handlePluginSettingsUpdated} from '../src/plugin/plugin';
import {Address, Bytes, DataSourceContext, Value, ethereum} from '@graphprotocol/graph-ts';
import {dataSourceMock, newMockEvent} from 'matchstick-as';

export function createPlugin(dao: Address, plugin: Address): Bytes{
const installationId = getPluginInstallationId(dao, plugin);
if(!installationId) throw new Error('Invalid installation id');

let pluginEntity = new Plugin(installationId.toHexString());
pluginEntity.dao = dao.toHexString();
pluginEntity.save();

return installationId;
}

export function pluginSettingsUpdatedEvent(
eventAddress: string,
daoAddress: string,
onlyExecutionMultisigProposalCreation: boolean,
minTallyApprovals: number,
minParticipation: number,
supportThreshold: number,
minVoteDuration: number,
minTallyDuration: number,
daoTokenAddress: string,
censusStrategyURI: string,
minProposerVotingPower: number
): PluginSettingsUpdated {
let newPluginSettingsUpdatedEvent = changetype<PluginSettingsUpdated>(
newMockEvent()
);
newPluginSettingsUpdatedEvent.address = Address.fromString(eventAddress);

// Set the data context for the event
let context = new DataSourceContext()
context.set('daoAddress', Value.fromString(daoAddress));
dataSourceMock.setContext(context);

let onlyExecutionMultisigProposalCreationParam = new ethereum.EventParam(
'onlyExecutionMultisigProposalCreation',
ethereum.Value.fromBoolean(onlyExecutionMultisigProposalCreation)
);
let minTallyApprovalsParam = new ethereum.EventParam(
'minTallyApprovals',
ethereum.Value.fromI32(<i32>minTallyApprovals)
);
let minParticipationParam = new ethereum.EventParam(
'minParticipation',
ethereum.Value.fromI32(<i32>minParticipation)
);
let supportThresholdParam = new ethereum.EventParam(
'supportThreshold',
ethereum.Value.fromI32(<i32>supportThreshold)
);
let minVoteDurationParam = new ethereum.EventParam(
'minVoteDuration',
ethereum.Value.fromI32(<i32>minVoteDuration)
);
let minTallyDurationParam = new ethereum.EventParam(
'minTallyDuration',
ethereum.Value.fromI32(<i32>minTallyDuration)
);
let daoTokenAddressParam = new ethereum.EventParam(
'daoTokenAddress',
ethereum.Value.fromAddress(Address.fromString(daoTokenAddress))
);
let censusStrategyURIParam = new ethereum.EventParam(
'censusStrategyURI',
ethereum.Value.fromBytes(Bytes.fromHexString(censusStrategyURI))
);
let minProposerVotingPowerParam = new ethereum.EventParam(
'minProposerVotingPower',
ethereum.Value.fromI32(<i32>minProposerVotingPower)
);

newPluginSettingsUpdatedEvent.parameters = new Array();
newPluginSettingsUpdatedEvent.parameters.push(
onlyExecutionMultisigProposalCreationParam
);
newPluginSettingsUpdatedEvent.parameters.push(minTallyApprovalsParam);
newPluginSettingsUpdatedEvent.parameters.push(minParticipationParam);
newPluginSettingsUpdatedEvent.parameters.push(supportThresholdParam);
newPluginSettingsUpdatedEvent.parameters.push(minVoteDurationParam);
newPluginSettingsUpdatedEvent.parameters.push(minTallyDurationParam);
newPluginSettingsUpdatedEvent.parameters.push(daoTokenAddressParam);
newPluginSettingsUpdatedEvent.parameters.push(censusStrategyURIParam);
newPluginSettingsUpdatedEvent.parameters.push(minProposerVotingPowerParam);

return newPluginSettingsUpdatedEvent;
}

0 comments on commit 18fbb9f

Please sign in to comment.