Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Sep 9, 2024
1 parent 86ebc2a commit f99f611
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions treasuries/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//! Unit tests for the encointer_treasuries module.

use std::str::FromStr;
use super::*;
use crate::mock::{Balances, EncointerTreasuries, System};
use frame_support::assert_ok;
Expand All @@ -24,43 +25,50 @@ use sp_core::crypto::Ss58Codec;
use test_utils::{helpers::*, *};

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[test]
fn treasury_spending_works() {
new_test_ext().execute_with(|| {
System::set_block_number(System::block_number() + 1); // this is needed to assert events
let beneficiary = AccountId::from(AccountKeyring::Alice);
let amount: BalanceOf<TestRuntime> = 100_000_000;
let cid = CommunityIdentifier::default();
new_test_ext().execute_with(|| {
System::set_block_number(System::block_number() + 1); // this is needed to assert events
let beneficiary = AccountId::from(AccountKeyring::Alice);
let amount: BalanceOf<TestRuntime> = 100_000_000;
let cid = CommunityIdentifier::default();

let treasury = EncointerTreasuries::get_community_treasury_account_unchecked(Some(cid));
Balances::make_free_balance_be(&treasury, 500_000_000);
let treasury = EncointerTreasuries::get_community_treasury_account_unchecked(Some(cid));
Balances::make_free_balance_be(&treasury, 500_000_000);

assert_ok!(EncointerTreasuries::do_spend_native(Some(cid), beneficiary.clone(), amount));
assert_eq!(Balances::free_balance(&treasury), 400_000_000);
assert_eq!(Balances::free_balance(&beneficiary), amount);
assert_eq!(
last_event::<TestRuntime>(),
Some(Event::SpentNative { treasury, beneficiary, amount }.into())
);
});
assert_ok!(EncointerTreasuries::do_spend_native(Some(cid), beneficiary.clone(), amount));
assert_eq!(Balances::free_balance(&treasury), 400_000_000);
assert_eq!(Balances::free_balance(&beneficiary), amount);
assert_eq!(
last_event::<TestRuntime>(),
Some(Event::SpentNative { treasury, beneficiary, amount }.into())
);
});
}

#[test]
fn treasury_getter_works() {
new_test_ext().execute_with(|| {
let treasury_account = EncointerTreasuries::get_community_treasury_account_unchecked(None);
assert_eq!(
treasury_account.to_ss58check(),
"5FU79FVXdN3RYSj8857XjNT2SgeRrhk8iUzjb75X1yc8YDkZ"
);
let cid = CommunityIdentifier::default();
let treasury_account =
EncointerTreasuries::get_community_treasury_account_unchecked(Some(cid));
assert_eq!(
treasury_account.to_ss58check(),
"5D58hM12H6Gkc1h1chuzbbJ3FitGHAyTM6ECkdz4hi5dFheH"
)
});
new_test_ext().execute_with(|| {
let treasury_account = EncointerTreasuries::get_community_treasury_account_unchecked(None);
assert_eq!(
treasury_account.to_ss58check(),
"5FU79FVXdN3RYSj8857XjNT2SgeRrhk8iUzjb75X1yc8YDkZ"
);
let cid = CommunityIdentifier::default();
let treasury_account =
EncointerTreasuries::get_community_treasury_account_unchecked(Some(cid));
assert_eq!(
treasury_account.to_ss58check(),
"5D58hM12H6Gkc1h1chuzbbJ3FitGHAyTM6ECkdz4hi5dFheH"
);
let cid = CommunityIdentifier::from_str("sqm1v79dF6b").expect("invalid community id");
let treasury_account =
EncointerTreasuries::get_community_treasury_account_unchecked(Some(cid));
assert_eq!(
treasury_account.to_ss58check(),
"5CWoc3mGF9VEnuZzBbPWxhKPvY743AGwxUbvkYQHS8yWZbem"
)
});
}

0 comments on commit f99f611

Please sign in to comment.