-
Notifications
You must be signed in to change notification settings - Fork 696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests for pallet EPM-MB #6332
base: gpestana/pallet-epm-mb
Are you sure you want to change the base?
Changes from all commits
9b41a95
1010c1a
3c02986
72f4f92
28607d4
ff72aad
4552f6b
70a5e94
d0fccad
ec12271
09884c2
11ccb86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,6 +35,7 @@ use crate::{ | |||||
}; | ||||||
use frame_support::{ | ||||||
derive_impl, pallet_prelude::*, parameter_types, traits::fungible::InspectHold, | ||||||
weights::RuntimeDbWeight, | ||||||
}; | ||||||
use parking_lot::RwLock; | ||||||
use sp_runtime::{ | ||||||
|
@@ -66,6 +67,14 @@ pub type T = Runtime; | |||||
pub type Block = frame_system::mocking::MockBlock<Runtime>; | ||||||
pub(crate) type Solver = SequentialPhragmen<AccountId, sp_runtime::PerU16, ()>; | ||||||
|
||||||
pub struct Weighter; | ||||||
|
||||||
impl Get<RuntimeDbWeight> for Weighter { | ||||||
fn get() -> RuntimeDbWeight { | ||||||
return RuntimeDbWeight { read: 12, write: 12 } | ||||||
} | ||||||
} | ||||||
|
||||||
frame_election_provider_support::generate_solution_type!( | ||||||
#[compact] | ||||||
pub struct TestNposSolution::< | ||||||
|
@@ -80,6 +89,7 @@ frame_election_provider_support::generate_solution_type!( | |||||
impl frame_system::Config for Runtime { | ||||||
type Block = Block; | ||||||
type AccountData = pallet_balances::AccountData<Balance>; | ||||||
type DbWeight = Weighter; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
parameter_types! { | ||||||
|
@@ -274,6 +284,7 @@ impl ElectionProvider for MockFallback { | |||||
|
||||||
#[derive(Default)] | ||||||
pub struct ExtBuilder { | ||||||
minimum_score: Option<ElectionScore>, | ||||||
with_verifier: bool, | ||||||
} | ||||||
|
||||||
|
@@ -325,7 +336,12 @@ impl ExtBuilder { | |||||
} | ||||||
|
||||||
pub(crate) fn desired_targets(self, desired: u32) -> Self { | ||||||
DesiredTargets::set(desired); | ||||||
DesiredTargets::set(Ok(desired)); | ||||||
self | ||||||
} | ||||||
|
||||||
pub(crate) fn no_desired_targets(self) -> Self { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this? I think we should remove this, also setting desired targets to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In short to make some actions fail due to desired targets not being configured.
Are these not valid scenarios to test? |
||||||
DesiredTargets::set(Err("none")); | ||||||
self | ||||||
} | ||||||
|
||||||
|
@@ -339,8 +355,13 @@ impl ExtBuilder { | |||||
self | ||||||
} | ||||||
|
||||||
pub(crate) fn minimum_score(mut self, score: ElectionScore) -> Self { | ||||||
self.minimum_score = Some(score); | ||||||
self | ||||||
} | ||||||
|
||||||
pub(crate) fn verifier() -> Self { | ||||||
ExtBuilder { with_verifier: true } | ||||||
ExtBuilder { with_verifier: true, ..Default::default() } | ||||||
} | ||||||
|
||||||
pub(crate) fn build(self) -> sp_io::TestExternalities { | ||||||
|
@@ -372,6 +393,12 @@ impl ExtBuilder { | |||||
} | ||||||
.assimilate_storage(&mut storage); | ||||||
|
||||||
let _ = verifier_pallet::GenesisConfig::<T> { | ||||||
minimum_score: self.minimum_score, | ||||||
..Default::default() | ||||||
} | ||||||
.assimilate_storage(&mut storage); | ||||||
|
||||||
if self.with_verifier { | ||||||
// nothing special for now | ||||||
} | ||||||
|
@@ -484,6 +511,10 @@ pub fn roll_to_phase(phase: Phase<BlockNumber>) { | |||||
} | ||||||
} | ||||||
|
||||||
pub fn set_phase_to(phase: Phase<BlockNumber>) { | ||||||
CurrentPhase::<Runtime>::set(phase); | ||||||
} | ||||||
|
||||||
pub fn roll_to_export() { | ||||||
while !MultiPhase::current_phase().is_export() { | ||||||
roll_to(System::block_number() + 1); | ||||||
|
@@ -612,6 +643,16 @@ pub(crate) fn signed_events() -> Vec<crate::signed::Event<T>> { | |||||
.collect() | ||||||
} | ||||||
|
||||||
pub(crate) fn verifier_events() -> Vec<crate::verifier::Event<T>> { | ||||||
System::events() | ||||||
.into_iter() | ||||||
.map(|r| r.event) | ||||||
.filter_map( | ||||||
|e| if let RuntimeEvent::VerifierPallet(inner) = e { Some(inner) } else { None }, | ||||||
) | ||||||
.collect() | ||||||
} | ||||||
|
||||||
// TODO fix or use macro. | ||||||
pub(crate) fn filter_events( | ||||||
types: Vec<RuntimeEvent>, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this? The
type DbWeight
can be set to unit in the configs in test mocks, ie.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take a look at test called
on_initialize_returns_specific_weight_in_off_phase
.If DbWeight is set to () it fails.
So I defined it as such for the reason explained in the test.