Skip to content

Commit

Permalink
Merge pull request #112 from EmmanuelAR/feat/fix_donator_manager_test
Browse files Browse the repository at this point in the history
Add setup method in donator manager test to avoid repeat code
  • Loading branch information
adrianvrj authored Oct 1, 2024
2 parents cdcc90e + d28d1ed commit fd099fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
1 change: 0 additions & 1 deletion contracts/src/donator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mod Donator {
fn getLevel(self: @ContractState) -> u32 {
return self.level.read();
}

fn getTotalStarkDonations(self: @ContractState) -> u64 {
return self.total_stark_donations.read();
}
Expand Down
1 change: 0 additions & 1 deletion contracts/src/donatorManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ mod DonatorManager {
impl DonatorManagerImpl of super::IDonatorManager<ContractState> {
fn newDonator(ref self: ContractState) {
let mut calldata = ArrayTrait::<felt252>::new();

calldata.append(get_caller_address().try_into().unwrap());

let (address_0, _) = deploy_syscall(
Expand Down
48 changes: 17 additions & 31 deletions contracts/tests/test_donator_manager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// DONATOR MANAGER TEST
// *************************************************************************
use starknet::{ContractAddress, contract_address_const};
use starknet::class_hash::{ClassHash, class_hash_const};
use starknet::class_hash::{ClassHash};
use starknet::syscalls::deploy_syscall;

use snforge_std::{
Expand All @@ -18,60 +18,46 @@ fn OWNER() -> ContractAddress {
contract_address_const::<'OWNER'>()
}

// *************************************************************************
// TEST
// *************************************************************************

#[test]
fn test_constructor() {
start_cheat_caller_address_global(OWNER());

fn __setup__() -> (ContractAddress, ClassHash) {
// Donator
let donator = declare("Donator").unwrap();
let mut donator_calldata: Array<felt252> = array![];
donator_calldata.append_serde(OWNER());
let (donator_contract_address, _) = donator.deploy(@donator_calldata).unwrap();
let donator_class_hash = get_class_hash(donator_contract_address);

// Donator Manager
let donator_manager = declare("DonatorManager").unwrap();
let mut donator_manager_calldata: Array<felt252> = array![];
donator_manager_calldata.append_serde(donator_class_hash);
let (contract_address, _) = donator_manager.deploy(@donator_manager_calldata).unwrap();
let donator_manager_contract = IDonatorManagerDispatcher { contract_address };

return (contract_address, donator_class_hash,);
}

// *************************************************************************
// TEST
// *************************************************************************
#[test]
fn test_constructor() {
// Put owner address like caller
start_cheat_caller_address_global(OWNER());
let (contract_address, donator_class_hash) = __setup__();
let donator_manager_contract = IDonatorManagerDispatcher { contract_address };
let expected_donator_address = donator_manager_contract.getDonatorClassHash();
let owner = donator_manager_contract.getOwner();

assert(owner == OWNER(), 'Invalid owner');
assert(donator_class_hash == expected_donator_address, 'Invalid donator class hash');
}


#[test]
fn test_new_donator() {
start_cheat_caller_address_global(OWNER());

// Deploy Donator Contract
let donator = declare("Donator").unwrap();
let mut donator_calldata: Array<felt252> = array![];
donator_calldata.append_serde(OWNER());
let (expected_donator_address, _) = donator.deploy(@donator_calldata).unwrap();
let donator_class_hash = get_class_hash(
expected_donator_address
); // Retrieve Class Hash from the deployed contract

// Deploy Donator Manager Contract
let donator_manager = declare("DonatorManager").unwrap();
let mut donator_manager_calldata: Array<felt252> = array![];
donator_manager_calldata.append_serde(donator_class_hash);
let (contract_address, _) = donator_manager.deploy(@donator_manager_calldata).unwrap();
let (contract_address, donator_class_hash) = __setup__();
let donator_manager_contract = IDonatorManagerDispatcher { contract_address };

donator_manager_contract.newDonator();

let expected_donator_class_hash = get_class_hash(
donator_manager_contract.getDonatorByAddress(OWNER())
);

// Assert that in the Donator Manager contract the deployed Donator match de expected class hash
assert(expected_donator_class_hash == donator_class_hash, 'Invalid donator address');
}

0 comments on commit fd099fc

Please sign in to comment.