Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
basic tests finished
Browse files Browse the repository at this point in the history
  • Loading branch information
ruseinov committed Jan 12, 2023
1 parent ead8b81 commit 387b7b4
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frame/stake-tracker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ impl StakingInterface for StakingMock {
fn stake(
who: &Self::AccountId,
) -> Result<Stake<Self::AccountId, Self::Balance>, DispatchError> {
if *who >= 30 {
return Err(DispatchError::Other("not bonded"))
}
let stake = <Runtime as pallet_stake_tracker::Config>::Currency::total_balance(who);
Ok(Stake {
stash: *who,
Expand Down
180 changes: 180 additions & 0 deletions frame/stake-tracker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ mod on_stake_update {
});
}

#[test]
#[should_panic]
fn panics_when_not_bonded() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);
// user without stake
assert_storage_noop!(StakeTracker::on_stake_update(&30, None));
});
}

#[test]
fn noop_when_not_validator_or_nominator() {
ExtBuilder::default().build_and_execute(|| {
Expand Down Expand Up @@ -50,3 +60,173 @@ mod on_stake_update {
});
}
}

mod on_nominator_update {
use super::*;
#[test]
fn noop_when_in_the_list() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for (idx, id) in [1, 10, 20].iter().enumerate() {
let _ = VoterList::on_insert(*id, 1000);
assert_storage_noop!(StakeTracker::on_nominator_update(id, Vec::new()));
}
});
}

#[test]
#[should_panic]
fn panics_when_not_bonded() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);
// user without stake
assert_storage_noop!(StakeTracker::on_nominator_update(&30, Vec::new()));
});
}

#[test]
// It is the caller's problem to make sure `on_nominator_update` is called in the right context.
fn works_for_everyone() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for id in [1, 10, 20] {
StakeTracker::on_nominator_update(&id, Vec::new());
assert_eq!(
VoterList::get_score(&id).unwrap(),
Pallet::<Runtime>::to_vote(Staking::stake(&id).map(|s| s.active).unwrap())
);
}
});
}
}

mod on_validator_add {
use super::*;
#[test]
fn noop_when_in_the_list() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for id in [1, 10, 20] {
let _ = VoterList::on_insert(id, 1000);
assert_storage_noop!(StakeTracker::on_validator_add(&id));
}
});
}

#[test]
#[should_panic]
fn panics_when_not_bonded() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);
// user without stake
assert_storage_noop!(StakeTracker::on_validator_add(&30));
});
}

#[test]
// It is the caller's problem to make sure `on_validator_add` is called in the right context.
fn works_for_everyone() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for id in [1, 10, 20] {
StakeTracker::on_validator_add(&id);
assert_eq!(
VoterList::get_score(&id).unwrap(),
Pallet::<Runtime>::to_vote(Staking::stake(&id).map(|s| s.active).unwrap())
);
}
});
}
}

mod on_validator_remove {
use super::*;
#[test]
fn noop_when_not_in_the_list() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator, not bonded
for id in [1, 10, 20, 30] {
assert_storage_noop!(StakeTracker::on_validator_remove(&id));
}
});
}

#[test]
// It is the caller's problem to make sure `on_validator_remove` is called in the right context.
fn works_for_everyone_also_unbonded() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for id in [1, 10, 20, 30] {
let _ = VoterList::on_insert(id, 100);
assert_eq!(VoterList::count(), 1);
StakeTracker::on_validator_remove(&id);
assert_eq!(VoterList::count(), 0);
}
});
}
}

mod on_nominator_remove {
use super::*;
#[test]
fn noop_when_not_in_the_list() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator, not bonded
for id in [1, 10, 20, 30] {
assert_storage_noop!(StakeTracker::on_nominator_remove(&id, Vec::new()));
}
});
}

#[test]
// It is the caller's problem to make sure `on_nominator_remove` is called in the right context.
fn works_for_everyone_also_unbonded() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator
for id in [1, 10, 20, 30] {
let _ = VoterList::on_insert(id, 100);
assert_eq!(VoterList::count(), 1);
StakeTracker::on_nominator_remove(&id, Vec::new());
assert_eq!(VoterList::count(), 0);
}
});
}
}

mod on_unstake {
use super::*;

#[test]
fn noop() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(VoterList::count(), 0);

// usual user, validator, nominator, not bonded
for id in [1, 10, 20, 30] {
assert_storage_noop!(StakeTracker::on_unstake(&id));
}

// usual user, validator, nominator, not bonded
for id in [1, 10, 20, 30] {
VoterList::on_insert(id, 100);
assert_storage_noop!(StakeTracker::on_unstake(&id));
}
});
}
}

0 comments on commit 387b7b4

Please sign in to comment.