Skip to content

Commit

Permalink
chore(referenda-tracks): add test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
pandres95 committed Dec 22, 2023
1 parent 938e4ed commit f144d64
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 27 deletions.
16 changes: 14 additions & 2 deletions substrate/frame/referenda-tracks/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Remark Storage Pallet
# Referenda Tracks Pallet

Allows storing arbitrary data off chain.
- [`Config`][Config]
- [`Call`][Call]

## Overview

Manage referenda voting tracks.

## Interface

### Dispatchable Functions

- `insert` - Insert a new referenda Track.
- `update` - Update the configuration of an existing referenda Track.
- `remove` - Remove an existing track

License: Apache-2.0
28 changes: 25 additions & 3 deletions substrate/frame/referenda-tracks/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ use frame_support::{
weights::Weight,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_referenda::{PalletsOriginOf, TrackIdOf, TrackInfoOf};
use scale_info::TypeInfo;
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Perbill,
Expand Down Expand Up @@ -118,7 +120,8 @@ impl pallet_referenda_tracks::Config for Test {
type TrackId = u8;
type RuntimeEvent = RuntimeEvent;
type MaxTracks = MaxTracks;
// type WeightInfo = ();
type UpdateOrigin = EnsureRoot<u64>;
type WeightInfo = ();
}

parameter_types! {
Expand Down Expand Up @@ -177,13 +180,32 @@ impl<Class> VoteTally<u32, Class> for Tally {
}
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext(
maybe_tracks: Option<Vec<(TrackIdOf<Test, ()>, TrackInfoOf<Test, ()>, PalletsOriginOf<Test>)>>,
) -> sp_io::TestExternalities {
let balances = vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100), (6, 100)];

let t = RuntimeGenesisConfig {
system: Default::default(),
balances: pallet_balances::GenesisConfig::<Test> { balances },
}
.build_storage()
.unwrap();
t.into()

let mut ext = TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);

if let Some(tracks) = maybe_tracks {
for (id, info, pallet_origin) in tracks {
crate::Pallet::<Test, ()>::insert(RuntimeOrigin::root(), id, info, pallet_origin)
.expect("can insert track");
}

System::reset_events();
} else {
}
});

ext
}
235 changes: 213 additions & 22 deletions substrate/frame/referenda-tracks/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,219 @@

//! Tests for referenda tracks pallet.

use super::{Error, Event, Pallet as Tracks};
use super::{Error, Event, Pallet as ReferendaTracks, Tracks};
use crate::mock::*;
use frame_support::{assert_noop, assert_ok};
use frame_system::RawOrigin;

#[test]
fn generates_event() {
new_test_ext().execute_with(|| {
// let caller = 1;
// let data = vec![0u8; 100];
// System::set_block_number(System::block_number() + 1); //otherwise event won't be
// registered. assert_ok!(Remark::<Test>::store(RawOrigin::Signed(caller).into(),
// data.clone(),)); let events = System::events();
// // this one we create as we expect it
// let system_event: <Test as frame_system::Config>::RuntimeEvent = Event::Stored {
// content_hash: sp_io::hashing::blake2_256(&data).into(),
// sender: caller,
// }
// .into();
// // this one we actually go into the system pallet and get the last event
// // because we know its there from block +1
// let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(true, true);
});
use frame_system::{EventRecord, Phase, RawOrigin};
use pallet_referenda::TrackInfo;
use sp_runtime::{str_array as s, traits::BadOrigin, Perbill};

const TRACK: pallet_referenda::TrackInfoOf<Test, ()> = TrackInfo {
name: s("Test Track"),
max_deciding: 1,
decision_deposit: 0,
prepare_period: 10,
decision_period: 100,
confirm_period: 10,
min_enactment_period: 2,
min_approval: pallet_referenda::Curve::LinearDecreasing {
length: Perbill::from_percent(100),
floor: Perbill::from_percent(50),
ceil: Perbill::from_percent(100),
},
min_support: pallet_referenda::Curve::LinearDecreasing {
length: Perbill::from_percent(100),
floor: Perbill::from_percent(0),
ceil: Perbill::from_percent(50),
},
};

const ORIGIN_SIGNED_1: OriginCaller = OriginCaller::system(RawOrigin::Signed(1));
const ORIGIN_SIGNED_2: OriginCaller = OriginCaller::system(RawOrigin::Signed(2));
const ORIGIN_SIGNED_3: OriginCaller = OriginCaller::system(RawOrigin::Signed(3));

mod insert {
use super::*;

#[test]
fn fails_if_incorrect_origin() {
new_test_ext(None).execute_with(|| {
assert_noop!(
ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::signed(1),
1,
TRACK,
ORIGIN_SIGNED_1
),
BadOrigin
);
});
}

#[test]
fn it_works() {
new_test_ext(None).execute_with(|| {
System::set_block_number(1);

assert_ok!(ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
1,
TRACK,
ORIGIN_SIGNED_1
));

assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::Tracks(Event::Created { id: 1 }),
topics: vec![],
}],
);

assert_eq!(Tracks::<Test, ()>::get(1), Some(TRACK));
});
}

#[test]
fn it_fails_if_inserting_an_already_existing_track() {
new_test_ext(None).execute_with(|| {
assert_ok!(ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
1,
TRACK,
ORIGIN_SIGNED_1
));

assert_noop!(
ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
1,
TRACK,
ORIGIN_SIGNED_2
),
Error::<Test, ()>::TrackIdAlreadyExisting
);
});
}

#[test]
fn fails_if_exceeds_max_tracks() {
new_test_ext(None).execute_with(|| {
assert_ok!(ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
1,
TRACK,
ORIGIN_SIGNED_1
));

assert_ok!(ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
2,
TRACK,
ORIGIN_SIGNED_2
));

assert_noop!(
ReferendaTracks::<Test, ()>::insert(
RuntimeOrigin::root(),
3,
TRACK,
ORIGIN_SIGNED_3
),
Error::<Test, ()>::MaxTracksExceeded
);
});
}
}

mod update {
use super::*;

#[test]
fn fails_if_incorrect_origin() {
new_test_ext(None).execute_with(|| {
assert_noop!(
ReferendaTracks::<Test, ()>::update(RuntimeOrigin::signed(1), 1, TRACK),
BadOrigin
);
});
}

#[test]
fn fails_if_non_existing() {
new_test_ext(None).execute_with(|| {
assert_noop!(
ReferendaTracks::<Test, ()>::update(RuntimeOrigin::root(), 1, TRACK),
Error::<Test, ()>::TrackIdNotFound,
);
});
}

#[test]
fn it_works() {
new_test_ext(Some(vec![(1, TRACK, ORIGIN_SIGNED_1)])).execute_with(|| {
let mut track = TRACK.clone();
track.max_deciding = 2;

assert_ok!(ReferendaTracks::<Test, ()>::update(
RuntimeOrigin::root(),
1,
track.clone()
));

assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::Tracks(Event::Updated { id: 1, info: track.clone() }),
topics: vec![],
}],
);

assert_eq!(Tracks::<Test, ()>::get(1), Some(track));
});
}
}

mod remove {
use super::*;

#[test]
fn fails_if_incorrect_origin() {
new_test_ext(None).execute_with(|| {
assert_noop!(
ReferendaTracks::<Test, ()>::remove(RuntimeOrigin::signed(1), 1),
BadOrigin
);
});
}

#[test]
fn fails_if_non_existing() {
new_test_ext(None).execute_with(|| {
assert_noop!(
ReferendaTracks::<Test, ()>::remove(RuntimeOrigin::root(), 1),
Error::<Test, ()>::TrackIdNotFound,
);
});
}

#[test]
fn it_works() {
new_test_ext(Some(vec![(1, TRACK, ORIGIN_SIGNED_1)])).execute_with(|| {
assert_ok!(ReferendaTracks::<Test, ()>::remove(RuntimeOrigin::root(), 1));

assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::Tracks(Event::Removed { id: 1 }),
topics: vec![],
}],
);

assert_eq!(Tracks::<Test, ()>::get(1), None);
});
}
}

0 comments on commit f144d64

Please sign in to comment.