Skip to content

Commit

Permalink
refactor factory event checking
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Aug 29, 2023
1 parent dcc25ce commit c7dbca1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
80 changes: 42 additions & 38 deletions starknet/src/tests/test_factory.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#[cfg(test)]
mod tests {
use array::{ArrayTrait, SpanTrait};
use starknet::{syscalls::deploy_syscall, testing, contract_address_const, };
use starknet::{syscalls::deploy_syscall, testing, contract_address_const, ContractAddress};
use traits::TryInto;
use sx::factory::factory::{Factory, IFactoryDispatcher, IFactoryDispatcherTrait};
use option::OptionTrait;
use result::ResultTrait;
use sx::space::space::Space;
use sx::types::Strategy;
use sx::types::{Strategy};
use starknet::ClassHash;

use sx::tests::setup::setup::setup::{setup, ConfigTrait, deploy};
use sx::tests::setup::setup::setup::{setup, Config, ConfigTrait, deploy};
use openzeppelin::tests::utils;
use openzeppelin::tests::utils::constants::ZERO;
use sx::space::space::Space::SpaceCreated;
Expand All @@ -19,58 +18,63 @@ mod tests {
use traits::{PartialEq};
use clone::Clone;

#[test]
#[available_gas(10000000000)]
fn test_deploy() {
// Deploy Space
let config = setup();

let (factory, space) = deploy(@config);

// Ensure the space emitted the proper event
let space_event = utils::pop_log::<SpaceCreated>(space.contract_address).unwrap();
assert(space_event.space == space.contract_address, 'space');
assert(space_event.owner == config.owner, 'owner');
assert(
space_event.min_voting_duration == config.min_voting_duration, 'min_voting_duration'
);
assert(
space_event.max_voting_duration == config.max_voting_duration, 'max_voting_duration'
);
assert(space_event.voting_delay == config.voting_delay, 'voting_delay');
fn assert_space_event_is_correct(
event: SpaceCreated, config: Config, space_address: ContractAddress
) {
assert(event.space == space_address, 'space');
assert(event.owner == config.owner, 'owner');
assert(event.min_voting_duration == config.min_voting_duration, 'min_voting_duration');
assert(event.max_voting_duration == config.max_voting_duration, 'max_voting_duration');
assert(event.voting_delay == config.voting_delay, 'voting_delay');
assert(
space_event.proposal_validation_strategy == config.proposal_validation_strategy,
event.proposal_validation_strategy == config.proposal_validation_strategy,
'proposal_validation_strategy'
);
assert(
space_event
event
.proposal_validation_strategy_metadata_URI == config
.proposal_validation_strategy_metadata_uri
.span(),
'prop_val_strat_metadata'
);
assert(event.voting_strategies == config.voting_strategies.span(), 'voting_strategies');
assert(
space_event.voting_strategies == config.voting_strategies.span(), 'voting_strategies'
);
assert(
space_event
.voting_strategy_metadata_URIs == config
.voting_strategies_metadata_uris
.span(),
event.voting_strategy_metadata_URIs == config.voting_strategies_metadata_uris.span(),
'voting_strat_metadata'
);
assert(space_event.authenticators == config.authenticators.span(), 'authenticators');
assert(space_event.metadata_URI == config.metadata_uri.span(), 'metadata_URI');
assert(space_event.dao_URI == config.dao_uri.span(), 'dao_URI');
assert(event.authenticators == config.authenticators.span(), 'authenticators');
assert(event.metadata_URI == config.metadata_uri.span(), 'metadata_URI');
assert(event.dao_URI == config.dao_uri.span(), 'dao_URI');
}

let factory_event = utils::pop_log::<NewContractDeployed>(factory.contract_address)
.unwrap();
assert(factory_event.contract_address == space.contract_address, 'space_contract_address');
fn assert_factory_event_is_correct(
factory_event: NewContractDeployed, space_address: ContractAddress
) {
assert(factory_event.contract_address == space_address, 'space_contract_address');
assert(
factory_event.class_hash == Space::TEST_CLASS_HASH.try_into().unwrap(), 'class_hash'
);
}

#[test]
#[available_gas(10000000000)]
fn test_deploy() {
// Deploy Space
let config = setup();

let (factory, space) = deploy(@config);

let space_event = utils::pop_log::<SpaceCreated>(space.contract_address).unwrap();

// Ensure the space emitted the proper event
assert_space_event_is_correct(space_event, config, space.contract_address);

let factory_event = utils::pop_log::<NewContractDeployed>(factory.contract_address)
.unwrap();

assert_factory_event_is_correct(factory_event, space.contract_address);
}


#[test]
#[available_gas(10000000000)]
Expand Down
8 changes: 6 additions & 2 deletions starknet/src/tests/test_space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(u256_from_felt252(1), new_payload);
Expand Down Expand Up @@ -393,7 +395,9 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(u256_from_felt252(1), vanilla_execution_strategy.params.clone());
Expand Down
8 changes: 6 additions & 2 deletions starknet/src/tests/vote.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Fast forward to end of voting period
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -276,7 +278,9 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Fast forward to end of voting period
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
let proposal_id = 1_u256;
Expand Down

0 comments on commit c7dbca1

Please sign in to comment.