Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
profile stake return tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Oct 4, 2023
1 parent 41dc37c commit 0ed5a17
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 8 deletions.
7 changes: 6 additions & 1 deletion pallets/profile-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub mod pallet {
CitizenNotApproved,
NotAPostOwner,
AmountFundedGreaterThanRequired,
ProfileFundAlreadyReturned,
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
Expand Down Expand Up @@ -801,6 +802,8 @@ pub mod pallet {
who.clone(),
profile_fund_info,
);
} else {
Err(Error::<T>::ProfileFundAlreadyReturned)?;
}
},
None => {
Expand All @@ -809,7 +812,7 @@ pub mod pallet {
}
}
} else if period == Period::Evidence {
T::SchellingGameSharedSource::ensure_time_for_staking_not_over_link(
T::SchellingGameSharedSource::ensure_time_for_staking_over_link(
key, phase_data, now,
)?;
match <ProfileFundDetails<T>>::get(profile_user_account.clone(), who.clone()) {
Expand All @@ -828,6 +831,8 @@ pub mod pallet {
who.clone(),
profile_fund_info,
);
} else {
Err(Error::<T>::ProfileFundAlreadyReturned)?;
}
},
None => {
Expand Down
92 changes: 91 additions & 1 deletion pallets/profile-validation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn challenge_evidence() {
challenge_content.clone()
));
let balance = Balances::free_balance(4);
assert_eq!(300000-fees, balance);
assert_eq!(300000 - fees, balance);
let period = SchellingGameShared::get_period(key.clone());
assert_eq!(Some(Period::Staking), period);

Expand All @@ -172,6 +172,96 @@ fn challenge_evidence() {
})
}

#[test]
fn challenge_profile_after_time_for_staking_over_test() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 1000));
let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 };
let period = SchellingGameShared::get_period(key.clone());
assert_eq!(Some(Period::Evidence), period);

let challenge_content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhabc"
.as_bytes()
.to_vec(),
);

let phase_data = ProfileValidation::get_phase_data();

assert_noop!(
ProfileValidation::challenge_profile(
RuntimeOrigin::signed(4),
1,
challenge_content.clone()
),
<schelling_game_shared::Error<Test>>::EvidencePeriodNotOver
);

System::set_block_number(phase_data.evidence_length + phase_data.end_of_staking_time + 1);
assert_noop!(
ProfileValidation::challenge_profile(
RuntimeOrigin::signed(4),
1,
challenge_content.clone()
),
<schelling_game_shared::Error<Test>>::TimeForStakingOver
);
});
}

#[test]
fn return_profile_stake_test() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProfileValidation::add_citizen(RuntimeOrigin::signed(1), content.clone()));
let balance = Balances::free_balance(3);
assert_eq!(300000, balance);
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(3), 1, 400));
let balance = Balances::free_balance(3);
assert_eq!(300000 - 400, balance);
assert_ok!(ProfileValidation::add_profile_stake(RuntimeOrigin::signed(4), 1, 600));
let balance = Balances::free_balance(4);
assert_eq!(300000 - 600, balance);
let key = SumTreeName::ProfileValidation { citizen_address: 1, block_number: 1 };
let period = SchellingGameShared::get_period(key.clone());
assert_eq!(Some(Period::Evidence), period);
let phase_data = ProfileValidation::get_phase_data();
System::set_block_number(phase_data.evidence_length + phase_data.end_of_staking_time);
assert_noop!(
ProfileValidation::return_profile_stake(RuntimeOrigin::signed(3), 1),
<schelling_game_shared::Error<Test>>::TimeForStakingNotOver
);
System::set_block_number(phase_data.evidence_length + phase_data.end_of_staking_time + 1);
assert_ok!(ProfileValidation::return_profile_stake(RuntimeOrigin::signed(3), 1));
let balance = Balances::free_balance(3);
assert_eq!(300000, balance);
assert_noop!(
ProfileValidation::return_profile_stake(RuntimeOrigin::signed(3), 1),
Error::<Test>::ProfileFundAlreadyReturned
);

assert_ok!(ProfileValidation::return_profile_stake(RuntimeOrigin::signed(4), 1));
let balance = Balances::free_balance(4);
assert_eq!(300000, balance);
assert_noop!(
ProfileValidation::return_profile_stake(RuntimeOrigin::signed(5), 1),
Error::<Test>::ProfileFundNotExists
);
});
}

#[test]
fn schelling_game_test() {
new_test_ext().execute_with(|| {
Expand Down
2 changes: 1 addition & 1 deletion pallets/schelling-game-shared/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<T: Config> Pallet<T> {
}

/// Check time for staking over
pub(super) fn ensure_time_for_staking_not_over(
pub(super) fn ensure_time_for_staking_over(
key: SumTreeNameType<T>,
phase_data: PhaseDataOf<T>,
now: BlockNumberOf<T>,
Expand Down
4 changes: 2 additions & 2 deletions pallets/schelling-game-shared/src/share_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ impl<T: Config> SchellingGameSharedLink for Pallet<T> {
Self::set_to_staking_period(key, phase_data, now)
}

fn ensure_time_for_staking_not_over_link(
fn ensure_time_for_staking_over_link(
key: Self::SumTreeName,
phase_data: Self::PhaseData,
now: Self::BlockNumber,
) -> DispatchResult {
Self::ensure_time_for_staking_not_over(key, phase_data, now)
Self::ensure_time_for_staking_over(key, phase_data, now)
}

fn set_to_staking_period_pe_link(
Expand Down
4 changes: 2 additions & 2 deletions pallets/schelling-game-shared/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ fn check_time_for_staking_not_over_test() {
let phase_data = get_the_phase_data();
let now2 = now + phase_data.evidence_length + phase_data.end_of_staking_time - 1;
assert_noop!(
TemplateModule::ensure_time_for_staking_not_over(key.clone(), phase_data, now2),
TemplateModule::ensure_time_for_staking_over(key.clone(), phase_data, now2),
Error::<Test>::TimeForStakingNotOver
);
let phase_data = get_the_phase_data();
let now = now + phase_data.evidence_length + phase_data.end_of_staking_time;
assert_ok!(TemplateModule::ensure_time_for_staking_not_over(key.clone(), phase_data, now));
assert_ok!(TemplateModule::ensure_time_for_staking_over(key.clone(), phase_data, now));
});
}

Expand Down
2 changes: 1 addition & 1 deletion traits/schelling-game-shared-link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait SchellingGameSharedLink {
now: Self::BlockNumber,
) -> DispatchResult;

fn ensure_time_for_staking_not_over_link(
fn ensure_time_for_staking_over_link(
key: Self::SumTreeName,
phase_data: Self::PhaseData,
now: Self::BlockNumber,
Expand Down

0 comments on commit 0ed5a17

Please sign in to comment.