Migrate more pallet tests to construct_runtime (#8051)

* Migrate bounties tests to use construct_runtime

* Migrate contracts tests to use construct_runtime

* Migrate democracy tests to use construct_runtime

* review: rename TreasuryEvent -> TreasuryError
This commit is contained in:
Andrew Jones
2021-02-04 16:34:15 +00:00
committed by GitHub
parent 86498a1a0c
commit e5ef38330d
4 changed files with 105 additions and 141 deletions
+27 -31
View File
@@ -19,12 +19,12 @@
#![cfg(test)]
use crate as pallet_bounties;
use super::*;
use std::cell::RefCell;
use frame_support::{
assert_noop, assert_ok, impl_outer_origin, parameter_types, weights::Weight,
impl_outer_event, traits::{OnInitialize}
assert_noop, assert_ok, parameter_types, weights::Weight, traits::OnInitialize
};
use sp_core::H256;
@@ -34,32 +34,29 @@ use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup, BadOrigin},
};
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
mod bounties {
// Re-export needed for `impl_outer_event!`.
pub use crate::*;
}
impl_outer_event! {
pub enum Event for Test {
system<T>,
pallet_balances<T>,
pallet_treasury<T>,
bounties<T>,
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Bounties: pallet_bounties::{Module, Call, Storage, Event<T>},
Treasury: pallet_treasury::{Module, Call, Storage, Config, Event<T>},
}
}
);
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Config for Test {
type BaseCallFilter = ();
type BlockWeights = ();
@@ -68,7 +65,7 @@ impl frame_system::Config for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = ();
type Call = Call;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u128; // u64 is not enough to hold bytes used to generate bounty account
@@ -77,7 +74,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
@@ -142,10 +139,8 @@ impl Config for Test {
type MaximumReasonLength = MaximumReasonLength;
type WeightInfo = ();
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Treasury = pallet_treasury::Module<Test>;
type Bounties = Module<Test>;
type TreasuryError = pallet_treasury::Error::<Test, pallet_treasury::DefaultInstance>;
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
@@ -160,7 +155,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
fn last_event() -> RawEvent<u64, u128> {
System::events().into_iter().map(|r| r.event)
.filter_map(|e| {
if let Event::bounties(inner) = e { Some(inner) } else { None }
if let Event::pallet_bounties(inner) = e { Some(inner) } else { None }
})
.last()
.unwrap()
@@ -206,7 +201,7 @@ fn spend_proposal_fails_when_proposer_poor() {
new_test_ext().execute_with(|| {
assert_noop!(
Treasury::propose_spend(Origin::signed(2), 100, 3),
Error::<Test>::InsufficientProposersBalance,
TreasuryError::InsufficientProposersBalance,
);
});
}
@@ -259,21 +254,22 @@ fn reject_already_rejected_spend_proposal_fails() {
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
assert_ok!(Treasury::reject_proposal(Origin::root(), 0));
assert_noop!(Treasury::reject_proposal(Origin::root(), 0), Error::<Test>::InvalidIndex);
assert_noop!(Treasury::reject_proposal(Origin::root(), 0), TreasuryError::InvalidIndex);
});
}
#[test]
fn reject_non_existent_spend_proposal_fails() {
new_test_ext().execute_with(|| {
assert_noop!(Treasury::reject_proposal(Origin::root(), 0), Error::<Test>::InvalidIndex);
assert_noop!(Treasury::reject_proposal(Origin::root(), 0),
pallet_treasury::Error::<Test, pallet_treasury::DefaultInstance>::InvalidIndex);
});
}
#[test]
fn accept_non_existent_spend_proposal_fails() {
new_test_ext().execute_with(|| {
assert_noop!(Treasury::approve_proposal(Origin::root(), 0), Error::<Test>::InvalidIndex);
assert_noop!(Treasury::approve_proposal(Origin::root(), 0), TreasuryError::InvalidIndex);
});
}
@@ -284,7 +280,7 @@ fn accept_already_rejected_spend_proposal_fails() {
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
assert_ok!(Treasury::reject_proposal(Origin::root(), 0));
assert_noop!(Treasury::approve_proposal(Origin::root(), 0), Error::<Test>::InvalidIndex);
assert_noop!(Treasury::approve_proposal(Origin::root(), 0), TreasuryError::InvalidIndex);
});
}
+2 -2
View File
@@ -772,7 +772,7 @@ fn deposit_event<T: Config>(
mod tests {
use super::*;
use crate::{
gas::GasMeter, tests::{ExtBuilder, Test, MetaEvent},
gas::GasMeter, tests::{ExtBuilder, Test, Event as MetaEvent},
gas::Gas,
storage::Storage,
tests::{
@@ -797,7 +797,7 @@ mod tests {
<frame_system::Module<Test>>::events()
.into_iter()
.filter_map(|meta| match meta.event {
MetaEvent::contracts(contract_event) => Some(contract_event),
MetaEvent::pallet_contracts(contract_event) => Some(contract_event),
_ => None,
})
.collect()
+57 -73
View File
@@ -16,7 +16,7 @@
// limitations under the License.
use crate::{
BalanceOf, ContractInfo, ContractInfoOf, GenesisConfig, Module,
BalanceOf, ContractInfo, ContractInfoOf, Module,
RawAliveContractInfo, RawEvent, Config, Schedule, gas::Gas,
Error, ConfigCache, RuntimeReturnCode, storage::Storage,
chain_extension::{
@@ -34,8 +34,8 @@ use sp_runtime::{
};
use sp_io::hashing::blake2_256;
use frame_support::{
assert_ok, assert_err, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event,
impl_outer_origin, parameter_types, StorageMap, assert_storage_noop,
assert_ok, assert_err, assert_err_ignore_postinfo,
parameter_types, StorageMap, assert_storage_noop,
traits::{Currency, ReservableCurrency, OnInitialize},
weights::{Weight, PostDispatchInfo, DispatchClass, constants::WEIGHT_PER_SECOND},
dispatch::DispatchErrorWithPostInfo,
@@ -44,32 +44,24 @@ use frame_support::{
use frame_system::{self as system, EventRecord, Phase};
use pretty_assertions::assert_eq;
mod contracts {
// Re-export contents of the root. This basically
// needs to give a name for the current crate.
// This hack is required for `impl_outer_event!`.
pub use super::super::*;
pub use frame_support::impl_outer_event;
}
use crate as pallet_contracts;
use pallet_balances as balances;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
impl_outer_event! {
pub enum MetaEvent for Test {
system<T>,
balances<T>,
contracts<T>,
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Randomness: pallet_randomness_collective_flip::{Module, Call, Storage},
Contracts: pallet_contracts::{Module, Call, Config<T>, Storage, Event<T>},
}
}
impl_outer_origin! {
pub enum Origin for Test where system = frame_system { }
}
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
balances::Balances,
contracts::Contracts,
}
}
);
#[macro_use]
pub mod test_utils {
@@ -198,8 +190,6 @@ impl ChainExtension<Test> for TestExtension {
}
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
@@ -220,10 +210,10 @@ impl frame_system::Config for Test {
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = MetaEvent;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
@@ -233,7 +223,7 @@ impl frame_system::Config for Test {
impl pallet_balances::Config for Test {
type MaxLocks = ();
type Balance = u64;
type Event = MetaEvent;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
@@ -276,7 +266,7 @@ impl Config for Test {
type Time = Timestamp;
type Randomness = Randomness;
type Currency = Balances;
type Event = MetaEvent;
type Event = Event;
type RentPayment = ();
type SignedClaimHandicap = SignedClaimHandicap;
type TombstoneDeposit = TombstoneDeposit;
@@ -294,12 +284,6 @@ impl Config for Test {
type DeletionWeightLimit = DeletionWeightLimit;
}
type Balances = pallet_balances::Module<Test>;
type Timestamp = pallet_timestamp::Module<Test>;
type Contracts = Module<Test>;
type System = frame_system::Module<Test>;
type Randomness = pallet_randomness_collective_flip::Module<Test>;
pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]);
@@ -331,7 +315,7 @@ impl ExtBuilder {
pallet_balances::GenesisConfig::<Test> {
balances: vec![],
}.assimilate_storage(&mut t).unwrap();
GenesisConfig {
pallet_contracts::GenesisConfig {
current_schedule: Schedule::<Test> {
enable_println: true,
..Default::default()
@@ -483,50 +467,50 @@ fn instantiate_and_call_and_deposit_event() {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(ALICE.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(ALICE.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(addr.clone(), subsistence * 100)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(ALICE, addr.clone(), subsistence * 100)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::ContractEmitted(addr.clone(), vec![1, 2, 3, 4])
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, addr.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(ALICE, addr.clone())),
topics: vec![],
},
]);
@@ -1212,43 +1196,43 @@ fn restoration(
let mut events = vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(ALICE)),
event: Event::frame_system(frame_system::Event::NewAccount(ALICE)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr_bob.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr_bob.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(addr_bob.clone(), 30_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(ALICE, addr_bob.clone(), 30_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(set_rent_code_hash.into())),
event: Event::pallet_contracts(RawEvent::CodeStored(set_rent_code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, addr_bob.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(ALICE, addr_bob.clone())),
topics: vec![],
},
];
@@ -1269,26 +1253,26 @@ fn restoration(
events.extend([
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr_dummy.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr_dummy.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Endowed(addr_dummy.clone(), 20_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(ALICE, addr_dummy.clone(), 20_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, addr_dummy.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(ALICE, addr_dummy.clone())),
topics: vec![],
},
].iter().cloned());
@@ -1414,44 +1398,44 @@ fn restoration(
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Evicted(addr_bob)),
event: Event::pallet_contracts(RawEvent::Evicted(addr_bob)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(CHARLIE)),
event: Event::frame_system(frame_system::Event::NewAccount(CHARLIE)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)),
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::Event::NewAccount(addr_django.clone())),
event: Event::frame_system(frame_system::Event::NewAccount(addr_django.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)),
event: Event::pallet_balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(CHARLIE, addr_django.clone(), 30_000)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(restoration_code_hash)),
event: Event::pallet_contracts(RawEvent::CodeStored(restoration_code_hash)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(CHARLIE, addr_django.clone())),
event: Event::pallet_contracts(RawEvent::Instantiated(CHARLIE, addr_django.clone())),
topics: vec![],
},
@@ -1483,17 +1467,17 @@ fn restoration(
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeRemoved(restoration_code_hash)),
event: Event::pallet_contracts(RawEvent::CodeRemoved(restoration_code_hash)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(system::Event::KilledAccount(addr_django.clone())),
event: Event::frame_system(system::Event::KilledAccount(addr_django.clone())),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::Restored(addr_django, addr_bob, bob_contract.code_hash, 50)
),
topics: vec![],
@@ -1719,26 +1703,26 @@ fn self_destruct_works() {
pretty_assertions::assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::system(
event: Event::frame_system(
frame_system::Event::KilledAccount(addr.clone())
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::balances(
event: Event::pallet_balances(
pallet_balances::RawEvent::Transfer(addr.clone(), DJANGO, 93_654)
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeRemoved(code_hash)),
event: Event::pallet_contracts(RawEvent::CodeRemoved(code_hash)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: MetaEvent::contracts(
event: Event::pallet_contracts(
RawEvent::Terminated(addr.clone(), DJANGO)
),
topics: vec![],
+19 -35
View File
@@ -17,11 +17,12 @@
//! The crate's tests.
use crate as pallet_democracy;
use super::*;
use codec::Encode;
use frame_support::{
impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types,
impl_outer_event, ord_parameter_types, traits::{Contains, OnInitialize, Filter},
assert_noop, assert_ok, parameter_types, ord_parameter_types,
traits::{Contains, OnInitialize, Filter},
weights::Weight,
};
use sp_core::H256;
@@ -50,30 +51,21 @@ const BIG_NAY: Vote = Vote { aye: false, conviction: Conviction::Locked1x };
const MAX_PROPOSALS: u32 = 100;
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
frame_system::System,
pallet_balances::Balances,
democracy::Democracy,
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Scheduler: pallet_scheduler::{Module, Call, Storage, Config, Event<T>},
Democracy: pallet_democracy::{Module, Call, Storage, Config, Event<T>},
}
}
mod democracy {
pub use crate::Event;
}
impl_outer_event! {
pub enum Event for Test {
system<T>,
pallet_balances<T>,
pallet_scheduler<T>,
democracy<T>,
}
}
);
// Test that a fitlered call can be dispatched.
pub struct BaseFilter;
@@ -83,9 +75,6 @@ impl Filter<Call> for BaseFilter {
}
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
@@ -108,7 +97,7 @@ impl frame_system::Config for Test {
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
@@ -169,7 +158,7 @@ impl Contains<u64> for OneToFive {
fn add(_m: &u64) {}
}
impl super::Config for Test {
impl Config for Test {
type Proposal = Call;
type Event = Event;
type Currency = pallet_balances::Module<Self>;
@@ -204,7 +193,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
pallet_balances::GenesisConfig::<Test>{
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
}.assimilate_storage(&mut t).unwrap();
GenesisConfig::default().assimilate_storage(&mut t).unwrap();
pallet_democracy::GenesisConfig::default().assimilate_storage(&mut t).unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
@@ -216,11 +205,6 @@ pub fn new_test_ext_execute_with_cond(execute: impl FnOnce(bool) -> () + Clone)
new_test_ext().execute_with(|| execute(true));
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Scheduler = pallet_scheduler::Module<Test>;
type Democracy = Module<Test>;
#[test]
fn params_should_work() {
new_test_ext().execute_with(|| {