Skip to content

Commit

Permalink
Iv thea executor tests (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauthamastro authored Jun 24, 2023
2 parents bd82a15 + 3404aec commit b20fe99
Showing 1 changed file with 169 additions and 2 deletions.
171 changes: 169 additions & 2 deletions pallets/thea-executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@

use crate::{
mock::{new_test_ext, Assets, Test, *},
PendingWithdrawals,
PendingWithdrawals, WithdrawalFees, *,
};
use frame_support::{
assert_noop, assert_ok,
traits::{fungible::Mutate as FungibleMutate, fungibles::Mutate as FungiblesMutate},
};
use frame_system::EventRecord;
use parity_scale_codec::Encode;
use sp_runtime::{
traits::{AccountIdConversion, BadOrigin},
SaturatedConversion,
};
use thea_primitives::types::{AssetMetadata, Deposit, Withdraw};
use xcm::{opaque::lts::Junctions, v3::MultiLocation, VersionedMultiLocation};

use thea_primitives::types::{Deposit, Withdraw};
fn assert_last_event<T: crate::Config>(generic_event: <T as crate::Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

#[test]
fn test_withdraw_returns_ok() {
Expand Down Expand Up @@ -171,3 +184,157 @@ fn test_claim_deposit_returns_asset_not_registered() {
);
})
}

#[test]
fn test_set_withdrawal_fee_full() {
new_test_ext().execute_with(|| {
// bad origins
assert_noop!(TheaExecutor::set_withdrawal_fee(RuntimeOrigin::none(), 1, 1), BadOrigin);
assert!(<WithdrawalFees<Test>>::get(1).is_none());
assert_noop!(TheaExecutor::set_withdrawal_fee(RuntimeOrigin::signed(1), 1, 1), BadOrigin);
assert!(<WithdrawalFees<Test>>::get(1).is_none());
// proper origin
// max inputs
assert_ok!(TheaExecutor::set_withdrawal_fee(RuntimeOrigin::root(), u8::MAX, u128::MAX));
assert_eq!(<WithdrawalFees<Test>>::get(u8::MAX).unwrap(), u128::MAX);
// half max inputs
assert_ok!(TheaExecutor::set_withdrawal_fee(
RuntimeOrigin::root(),
u8::MAX / 2,
u128::MAX / 2
));
// min inputs
System::set_block_number(1);
assert_ok!(TheaExecutor::set_withdrawal_fee(RuntimeOrigin::root(), 0, 0));
assert_last_event::<Test>(crate::Event::<Test>::WithdrawalFeeSet(0, 0).into());
})
}

#[test]
fn test_parachain_withdraw_full() {
new_test_ext().execute_with(|| {
// setup code
let asset_id: <Test as Config>::AssetId = 100u128.into();
let admin = 1u64;
let network_id = 1;
Balances::set_balance(&admin, 100_000_000_000_000_000_000u128.saturated_into());
<Test as Config>::Currency::mint_into(
&admin,
100_000_000_000_000_000_000u128.saturated_into(),
)
.unwrap();
<Test as Config>::Assets::create(
RuntimeOrigin::signed(admin),
asset_id.into(),
admin,
1u128.saturated_into(),
)
.unwrap();
let pallet_acc = <Test as crate::Config>::TheaPalletId::get().into_account_truncating();
Balances::set_balance(&pallet_acc, 100_000_000_000_000_000_000u128.saturated_into());
<Test as Config>::Currency::mint_into(
&pallet_acc,
100_000_000_000_000_000_000u128.saturated_into(),
)
.unwrap();
let account = 2u64;
Balances::set_balance(&account, 100_000_000_000_000_000_000u128.saturated_into());
<Test as Config>::Currency::mint_into(
&account,
100_000_000_000_000_000_000u128.saturated_into(),
)
.unwrap();
Assets::mint_into(
asset_id.into(),
&account,
100_000_000_000_000_000_000u128.saturated_into(),
)
.unwrap();
<Test as Config>::Currency::mint_into(&account, 100_000_000_000_000u128.saturated_into())
.unwrap();
Balances::set_balance(&account, 100_000_000_000_000u128.saturated_into());
let metadata = AssetMetadata::new(10).unwrap();
<Metadata<Test>>::insert(100, metadata);
<WithdrawalFees<Test>>::insert(network_id, 1_000);
let multilocation = MultiLocation { parents: 1, interior: Junctions::Here };
let beneficiary = Box::new(VersionedMultiLocation::V3(multilocation));
// bad origins
assert_noop!(
TheaExecutor::parachain_withdraw(
RuntimeOrigin::root(),
u128::MAX,
1_000_000_000,
beneficiary.clone(),
false
),
BadOrigin
);
assert_noop!(
TheaExecutor::parachain_withdraw(
RuntimeOrigin::none(),
u128::MAX,
1_000_000_000,
beneficiary.clone(),
false
),
BadOrigin
);
// asset not registered
assert_noop!(
TheaExecutor::parachain_withdraw(
RuntimeOrigin::signed(account),
u128::MAX,
1_000_000_000,
beneficiary.clone(),
false
),
Error::<Test>::AssetNotRegistered
);
// funds unavailable
assert_noop!(
TheaExecutor::parachain_withdraw(
RuntimeOrigin::signed(admin),
asset_id.into(),
1_000_000_000,
beneficiary.clone(),
false
),
sp_runtime::TokenError::FundsUnavailable
);
// proper case
assert_ok!(TheaExecutor::parachain_withdraw(
RuntimeOrigin::signed(account),
asset_id.into(),
1_000_000_000,
beneficiary.clone(),
false
));
})
}

#[test]
fn test_update_asset_metadata_full() {
new_test_ext().execute_with(|| {
// bad origins
assert_noop!(
TheaExecutor::update_asset_metadata(RuntimeOrigin::signed(1), 1, 1),
BadOrigin
);
assert_noop!(
TheaExecutor::update_asset_metadata(RuntimeOrigin::signed(u64::MAX), 1, 1),
BadOrigin
);
assert_noop!(TheaExecutor::update_asset_metadata(RuntimeOrigin::none(), 1, 1), BadOrigin);
// invalid decimal
assert_noop!(
TheaExecutor::update_asset_metadata(RuntimeOrigin::root(), u128::MAX, u8::MIN),
Error::<Test>::InvalidDecimal
);
// proper cases
System::set_block_number(1);
assert_ok!(TheaExecutor::update_asset_metadata(RuntimeOrigin::root(), 0, u8::MAX));
assert_ok!(TheaExecutor::update_asset_metadata(RuntimeOrigin::root(), u128::MAX, u8::MAX));
let md = AssetMetadata::new(u8::MAX).unwrap();
assert_last_event::<Test>(Event::<Test>::AssetMetadataSet(md).into());
})
}

0 comments on commit b20fe99

Please sign in to comment.