Skip to content

Commit

Permalink
fix test case in Ring pallet
Browse files Browse the repository at this point in the history
- fix ring 42 test cases
- rm test case with extend_lock in Ring pallet
  • Loading branch information
yanganto committed Feb 14, 2020
1 parent 908aee0 commit 0edafae
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 98 deletions.
2 changes: 1 addition & 1 deletion frame/balances/kton/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;

use darwinia_ring as ring;
use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use sp_core::H256;
use sp_io;
Expand All @@ -14,7 +15,6 @@ use sp_runtime::{
};

use crate::*;
use darwinia_ring as ring;

pub type AccountId = u64;
pub type Balance = u128;
Expand Down
3 changes: 3 additions & 0 deletions frame/balances/ring/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use sp_runtime::{
};
use std::cell::RefCell;

pub type Ring = Module<Test>;

use frame_system as system;
impl_outer_origin! {
pub enum Origin for Test {}
Expand Down Expand Up @@ -88,6 +90,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
}

parameter_types! {
pub const TransactionBaseFee: u64 = 0;
pub const TransactionByteFee: u64 = 1;
Expand Down
230 changes: 133 additions & 97 deletions frame/balances/ring/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

//! Tests for the module.

use sp_runtime::traits::SignedExtension;

use super::*;
use crate::mock::*;
use crate::mock::{info_from_weight, Balances, ExtBuilder, System, Test, CALL};
use darwinia_support::{LockIdentifier, LockableCurrency, NormalLock, WithdrawLock, WithdrawReason, WithdrawReasons};
use frame_support::{
assert_err, assert_noop, assert_ok,
traits::{
Currency, ExistenceRequirement::AllowDeath, LockIdentifier, LockableCurrency, ReservableCurrency,
WithdrawReason, WithdrawReasons,
},
traits::{Currency, ExistenceRequirement::AllowDeath, ReservableCurrency},
};
use frame_system::RawOrigin;
use mock::{info_from_weight, Balances, ExtBuilder, System, Test, CALL};
use pallet_transaction_payment::ChargeTransactionPayment;
use sp_runtime::traits::{BadOrigin, SignedExtension};

const ID_1: LockIdentifier = *b"1 ";
const ID_2: LockIdentifier = *b"2 ";
Expand All @@ -41,7 +41,15 @@ fn basic_locking_should_work() {
.build()
.execute_with(|| {
assert_eq!(Balances::free_balance(&1), 10);
Balances::set_lock(ID_1, &1, 9, u64::max_value(), WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 9,
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 5, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Expand All @@ -56,7 +64,15 @@ fn partial_locking_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 5,
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
}
Expand All @@ -68,7 +84,15 @@ fn lock_removal_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: u64::max_value(),
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
Balances::remove_lock(ID_1, &1);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
Expand All @@ -81,8 +105,24 @@ fn lock_replacement_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: u64::max_value(),
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 5,
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
}
Expand All @@ -94,8 +134,24 @@ fn double_locking_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
Balances::set_lock(ID_2, &1, 5, u64::max_value(), WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 5,
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
Ring::set_lock(
ID_2,
&1,
WithdrawLock::Normal(NormalLock {
amount: 5,
until: u64::max_value(),
}),
WithdrawReasons::all(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
}
Expand All @@ -107,35 +163,31 @@ fn combination_locking_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, u64::max_value(), 0, WithdrawReasons::none());
Balances::set_lock(ID_2, &1, 0, u64::max_value(), WithdrawReasons::none());
Balances::set_lock(ID_3, &1, 0, 0, WithdrawReasons::all());
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
}

#[test]
fn lock_value_extension_should_work() {
ExtBuilder::default()
.existential_deposit(1)
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: u64::max_value(),
until: 0,
}),
WithdrawReasons::none(),
);
Balances::extend_lock(ID_1, &1, 2, u64::max_value(), WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Ring::set_lock(
ID_2,
&1,
WithdrawLock::Normal(NormalLock {
amount: 0,
until: u64::max_value(),
}),
WithdrawReasons::none(),
);
Balances::extend_lock(ID_1, &1, 8, u64::max_value(), WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 3, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Ring::set_lock(
ID_3,
&1,
WithdrawLock::Normal(NormalLock { amount: 0, until: 0 }),
WithdrawReasons::all(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
}

Expand All @@ -146,7 +198,16 @@ fn lock_reasons_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Transfer.into());
// Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Transfer.into());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 10,
until: u64::max_value(),
}),
WithdrawReason::Transfer.into(),
);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Expand All @@ -162,7 +223,16 @@ fn lock_reasons_should_work() {
)
.is_ok());

Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Reserve.into());
// Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Reserve.into());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock {
amount: 10,
until: u64::max_value(),
}),
WithdrawReason::Reserve.into(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
assert_noop!(
<Balances as ReservableCurrency<_>>::reserve(&1, 1),
Expand All @@ -177,11 +247,20 @@ fn lock_reasons_should_work() {
)
.is_ok());

Balances::set_lock(
// Balances::set_lock(
// ID_1,
// &1,
// 10,
// u64::max_value(),
// WithdrawReason::TransactionPayment.into(),
// );
Ring::set_lock(
ID_1,
&1,
10,
u64::max_value(),
WithdrawLock::Normal(NormalLock {
amount: 10,
until: u64::max_value(),
}),
WithdrawReason::TransactionPayment.into(),
);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
Expand All @@ -204,7 +283,12 @@ fn lock_block_number_should_work() {
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
Ring::set_lock(
ID_1,
&1,
WithdrawLock::Normal(NormalLock { amount: 10, until: 2 }),
WithdrawReasons::all(),
);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
Expand All @@ -215,57 +299,6 @@ fn lock_block_number_should_work() {
});
}

#[test]
fn lock_block_number_extension_should_work() {
ExtBuilder::default()
.existential_deposit(1)
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
Balances::extend_lock(ID_1, &1, 10, 1, WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
System::set_block_number(2);
Balances::extend_lock(ID_1, &1, 10, 8, WithdrawReasons::all());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 3, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
});
}

#[test]
fn lock_reasons_extension_should_work() {
ExtBuilder::default()
.existential_deposit(1)
.monied(true)
.build()
.execute_with(|| {
Balances::set_lock(ID_1, &1, 10, 10, WithdrawReason::Transfer.into());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReasons::none());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReason::Reserve.into());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<Test, _>::LiquidityRestrictions
);
});
}

#[test]
fn default_indexing_on_new_accounts_should_not_work2() {
ExtBuilder::default()
Expand Down Expand Up @@ -395,7 +428,10 @@ fn balance_transfer_works() {
fn force_transfer_works() {
ExtBuilder::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);
assert_noop!(Balances::force_transfer(Some(2).into(), 1, 2, 69), BadOrigin,);
assert_err!(
Balances::force_transfer(Some(2).into(), 1, 2, 69),
DispatchError::BadOrigin,
);
assert_ok!(Balances::force_transfer(RawOrigin::Root.into(), 1, 2, 69));
assert_eq!(Balances::total_balance(&1), 42);
assert_eq!(Balances::total_balance(&2), 69);
Expand Down

0 comments on commit 0edafae

Please sign in to comment.