Skip to content

Commit

Permalink
added more test cases based on the comment and added benchmark setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mshankarrao committed Sep 21, 2023
1 parent 12fdab4 commit 0254064
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 40 deletions.
61 changes: 39 additions & 22 deletions pallets/disputes/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
//! Benchmarking setup for pallet-disputes
#![cfg(feature = "runtime-benchmarks")]
use super::*;

#[allow(unused)]
use crate::Pallet as Disputes;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use crate::Pallet as PalletDisputes;
use crate::{traits::DisputeRaiser};
use frame_benchmarking::{v2::*};
use frame_support::assert_ok;
use sp_runtime::SaturatedConversion;
use orml_traits::{MultiCurrency};
use common_types::CurrencyId;


#[benchmarks( where <T as frame_system::Config>::AccountId: AsRef<[u8]>, crate::Event::<T>: Into<<T as frame_system::Config>::RuntimeEvent>)]
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn do_something() {
let value = 100u32.into();
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
do_something(RawOrigin::Signed(caller), value);

assert_eq!(Something::<T>::get(), Some(value));
fn raise_a_dispute() {
let alice: T::AccountId = create_funded_user::<T>("alice", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId = create_funded_user::<T>("bob", 1, 1_000_000_000_000_000_000u128);
let charlie: T::AccountId = create_funded_user::<T>("charlie", 1, 1_000_000_000_000_000_000u128);
let dispute_key = 10;
let jury = get_jury::<Test>(vec![alice, bob]);
let specifics = get_specifics::<Test>(vec![0, 1]);
assert_ok!(<PalletDisputes as DisputeRaiser<AccountId>>::raise_dispute(
dispute_key,
alice,
jury,
specifics,
));
assert!(PalletDisputes::disputes(dispute_key).is_some());
assert_eq!(1, PalletDisputes::disputes(dispute_key).iter().count());
}

#[benchmark]
fn cause_error() {
Something::<T>::put(100u32);
let caller: T::AccountId = whitelisted_caller();
#[extrinsic_call]
cause_error(RawOrigin::Signed(caller));

assert_eq!(Something::<T>::get(), Some(101u32));
}
impl_benchmark_test_suite!(PalletDisputes, crate::mock::new_test_ext(), crate::mock::Test);
}

impl_benchmark_test_suite!(Disputes, crate::mock::new_test_ext(), crate::mock::Test);
pub fn create_funded_user<T: Config>(
seed: &'static str,
n: u32,
balance_factor: u128,
) -> T::AccountId {
let user = account(seed, n, 0);
assert_ok!(<T::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::deposit(
CurrencyId::Native, &user, balance_factor.saturated_into()
));
user
}

115 changes: 97 additions & 18 deletions pallets/disputes/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use test_utils::*;

mod test_utils {
use super::*;

pub fn run_to_block<T: Config>(n: T::BlockNumber)
where
T::BlockNumber: Into<u64>,
where
T::BlockNumber: Into<u64>,
{
loop {
let mut block: T::BlockNumber = frame_system::Pallet::<T>::block_number();
Expand Down Expand Up @@ -56,7 +57,7 @@ fn raise_dispute_assert_state() {

#[test]
fn raise_dispute_assert_event() {
new_test_ext().execute_with(||{
new_test_ext().execute_with(|| {
let dispute_key = 10;
let jury = get_jury::<Test>(vec![*CHARLIE, *BOB]);
let specifics = get_specifics::<Test>(vec![0, 1]);
Expand All @@ -67,7 +68,7 @@ fn raise_dispute_assert_event() {
specifics,
));
System::assert_last_event(RuntimeEvent::PalletDisputes(
Event::<Test>::DisputeRaised{ dispute_key:dispute_key},
Event::<Test>::DisputeRaised { dispute_key: dispute_key },
));
});
}
Expand Down Expand Up @@ -109,16 +110,16 @@ fn vote_on_dispute_assert_state() {
specifics,
));
let dispute_before_vote = Disputes::<Test>::get(dispute_key).expect("dispute should exist");
assert_eq!(0,dispute_before_vote.votes.len());
assert_eq!(0, dispute_before_vote.votes.len());
assert_ok!(PalletDisputes::vote_on_dispute(
RuntimeOrigin::signed(*BOB),
dispute_key,
true
));
let dispute_after_vote = Disputes::<Test>::get(dispute_key).expect("dispute should exist");
let vote = dispute_after_vote.votes.get(&BOB).unwrap();
assert_eq!(true,*vote);
assert_eq!(1,dispute_after_vote.votes.len());
assert_eq!(true, *vote);
assert_eq!(1, dispute_after_vote.votes.len());
});
}

Expand Down Expand Up @@ -172,12 +173,40 @@ fn vote_on_dispute_autofinalises_on_unanimous_yes() {
true
));
//verify that the dispute has been removed once auto_finalization is done in case of unanimous yes
assert_eq!(0,PalletDisputes::disputes(dispute_key).iter().count());
assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count());
});
}

#[test]
fn vote_on_dispute_autofinalises_on_unanimous_no() {
new_test_ext().execute_with(|| {
let dispute_key = 10;
let jury = get_jury::<Test>(vec![*CHARLIE, *BOB]);
let specifics = get_specifics::<Test>(vec![0, 1]);
assert_ok!(<PalletDisputes as DisputeRaiser<AccountId>>::raise_dispute(
dispute_key,
*ALICE,
jury,
specifics,
));
assert_ok!(PalletDisputes::vote_on_dispute(
RuntimeOrigin::signed(*BOB),
dispute_key,
false
));
assert_ok!(PalletDisputes::vote_on_dispute(
RuntimeOrigin::signed(*CHARLIE),
dispute_key,
false
));
//verify that the dispute has been removed once auto_finalization is done in case of unanimous no
assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count());
});
}

///SHANKAR: What does this mean?
#[test]
fn try_auto_finalise_removes_autofinalise() {
new_test_ext().execute_with(|| {
new_test_ext().execute_with(|| {
let dispute_key = 10;
Expand All @@ -200,19 +229,14 @@ fn vote_on_dispute_autofinalises_on_unanimous_no() {
false
));
//verify that the dispute has been removed once auto_finalization is done in case of unanimous no
assert_eq!(0,PalletDisputes::disputes(dispute_key).iter().count());
assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count());
//After the dispute has been autofinalized and the we again tru to autofinalize it throws an error saying that
// the dispute doesnt exists as it has been removed
assert_noop!(Dispute::<Test>::try_finalise_with_result(dispute_key,DisputeResult::Success),Error::<Test>::DisputeDoesNotExist);
});
});
}

///SHANKAR: What does this mean?
#[test]
fn try_auto_finalise_removes_autofinalise() {
new_test_ext().execute_with(|| {
todo!()
});
}

///testing if the non jury account tries to vote it should throw the error saying its not a jury account
#[test]
fn vote_on_dispute_not_jury_account() {
Expand Down Expand Up @@ -321,7 +345,31 @@ fn extend_dispute_already_extended() {

/// testing trying to extend the voting time and it successfully extend by setting the flag to true
#[test]
fn extend_dispute_works() {
fn extend_dispute_works_assert_last_event() {
new_test_ext().execute_with(|| {
let dispute_key = 10;
let jury = get_jury::<Test>(vec![*BOB]);
let specific_ids = get_specifics::<Test>(vec![0]);
assert_ok!(<PalletDisputes as DisputeRaiser<AccountId>>::raise_dispute(
dispute_key,
*ALICE,
jury,
specific_ids
));
let d = Disputes::<Test>::get(dispute_key).expect("dispute should exist");
assert!(!d.is_extended);
assert_ok!(PalletDisputes::extend_dispute(
RuntimeOrigin::signed(*BOB),
10
));
System::assert_last_event(RuntimeEvent::PalletDisputes(
Event::<Test>::DisputeExtended { dispute_key: dispute_key },
));
});
}

#[test]
fn extend_dispute_works_assert_state() {
new_test_ext().execute_with(|| {
let dispute_key = 10;
let jury = get_jury::<Test>(vec![*BOB]);
Expand All @@ -334,17 +382,48 @@ fn extend_dispute_works() {
));
let d = Disputes::<Test>::get(dispute_key).expect("dispute should exist");
assert!(!d.is_extended);
assert_eq!(11,d.expiration);
assert_ok!(PalletDisputes::extend_dispute(
RuntimeOrigin::signed(*BOB),
10
));
let d = Disputes::<Test>::get(dispute_key).expect("dispute should exist");
assert!(d.is_extended);
assert_eq!(21,d.expiration);
});
}

#[test]
fn calculate_winner_works() {
new_test_ext().execute_with(|| {});
}


///e2e
#[test]
fn e2e() {
new_test_ext().execute_with(|| {
let dispute_key = 10;
let jury = get_jury::<Test>(vec![*CHARLIE, *BOB]);
let specifics = get_specifics::<Test>(vec![0, 1]);
assert_ok!(<PalletDisputes as DisputeRaiser<AccountId>>::raise_dispute(
dispute_key,
*ALICE,
jury,
specifics,
));
assert_ok!(PalletDisputes::vote_on_dispute(
RuntimeOrigin::signed(*BOB),
dispute_key,
true
));
assert_ok!(PalletDisputes::vote_on_dispute(
RuntimeOrigin::signed(*CHARLIE),
dispute_key,
true
));
//verify that the dispute has been removed once auto_finalization is done in case of unanimous yes
assert_eq!(0, PalletDisputes::disputes(dispute_key).iter().count());
});
}

0 comments on commit 0254064

Please sign in to comment.