From 18fbb9f5005c69fa0233c448be5f0ff47f57bd8f Mon Sep 17 00:00:00 2001 From: Aleix Date: Mon, 6 Nov 2023 12:06:15 +0100 Subject: [PATCH] First Tests for event handlers --- packages/subgraph/src/plugin/plugin.ts | 3 +- packages/subgraph/tests/plugin.test.ts | 93 ++++++++++++++++++++++++++ packages/subgraph/tests/utils.ts | 93 ++++++++++++++++++++++++++ 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 packages/subgraph/tests/utils.ts diff --git a/packages/subgraph/src/plugin/plugin.ts b/packages/subgraph/src/plugin/plugin.ts index 716af379..b2f0bcc3 100644 --- a/packages/subgraph/src/plugin/plugin.ts +++ b/packages/subgraph/src/plugin/plugin.ts @@ -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 diff --git a/packages/subgraph/tests/plugin.test.ts b/packages/subgraph/tests/plugin.test.ts index e69de29b..a46fc0df 100644 --- a/packages/subgraph/tests/plugin.test.ts +++ b/packages/subgraph/tests/plugin.test.ts @@ -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' + ); + }); +}); diff --git a/packages/subgraph/tests/utils.ts b/packages/subgraph/tests/utils.ts new file mode 100644 index 00000000..1d838517 --- /dev/null +++ b/packages/subgraph/tests/utils.ts @@ -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( + 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(minTallyApprovals) + ); + let minParticipationParam = new ethereum.EventParam( + 'minParticipation', + ethereum.Value.fromI32(minParticipation) + ); + let supportThresholdParam = new ethereum.EventParam( + 'supportThreshold', + ethereum.Value.fromI32(supportThreshold) + ); + let minVoteDurationParam = new ethereum.EventParam( + 'minVoteDuration', + ethereum.Value.fromI32(minVoteDuration) + ); + let minTallyDurationParam = new ethereum.EventParam( + 'minTallyDuration', + ethereum.Value.fromI32(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(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; +}