Refactor candidates test in paras_inherent (#2004)

Splits the test in multiple cases.
This commit is contained in:
Tsvetomir Dimitrov
2023-10-24 18:23:21 +03:00
committed by GitHub
parent e39253c022
commit 0284e21f55
@@ -1205,9 +1205,17 @@ mod sanitizers {
} }
} }
#[test] mod candidates {
fn candidates() { use super::*;
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Backed candidates and scheduled parachains used for `sanitize_backed_candidates` testing
struct TestData {
backed_candidates: Vec<BackedCandidate>,
scheduled_paras: BTreeMap<primitives::Id, CoreIndex>,
}
// Generate test data for the candidates test
fn get_test_data() -> TestData {
const RELAY_PARENT_NUM: u32 = 3; const RELAY_PARENT_NUM: u32 = 3;
let header = default_header(); let header = default_header();
@@ -1233,9 +1241,6 @@ mod sanitizers {
.unwrap(); .unwrap();
} }
let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
let scheduled = (0_usize..2) let scheduled = (0_usize..2)
.into_iter() .into_iter()
.map(|idx| (ParaId::from(1_u32 + idx as u32), CoreIndex::from(idx as u32))) .map(|idx| (ParaId::from(1_u32 + idx as u32), CoreIndex::from(idx as u32)))
@@ -1278,29 +1283,54 @@ mod sanitizers {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// happy path TestData { backed_candidates, scheduled_paras: scheduled }
assert_eq!( }
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
),
backed_candidates
);
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped #[test]
{ fn happy_path() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();
let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
),
backed_candidates
);
{}
});
}
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
#[test]
fn nothing_scheduled() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: _ } = get_test_data();
let scheduled = &BTreeMap::new(); let scheduled = &BTreeMap::new();
let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
assert!(sanitize_backed_candidates::<Test, _>( assert!(sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(), backed_candidates.clone(),
has_concluded_invalid, has_concluded_invalid,
&scheduled &scheduled
) )
.is_empty()); .is_empty());
} });
}
// candidates that have concluded as invalid are filtered out
#[test]
fn invalid_are_filtered_out() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();
// candidates that have concluded as invalid are filtered out
{
// mark every second one as concluded invalid // mark every second one as concluded invalid
let set = { let set = {
let mut set = std::collections::HashSet::new(); let mut set = std::collections::HashSet::new();
@@ -1322,7 +1352,7 @@ mod sanitizers {
.len(), .len(),
backed_candidates.len() / 2 backed_candidates.len() / 2
); );
} });
}); }
} }
} }