Skip to content

Commit 0284e21

Browse files
authored
Refactor candidates test in paras_inherent (#2004)
Splits the test in multiple cases.
1 parent e39253c commit 0284e21

File tree

1 file changed

+52
-22
lines changed
  • polkadot/runtime/parachains/src/paras_inherent

1 file changed

+52
-22
lines changed

polkadot/runtime/parachains/src/paras_inherent/tests.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,17 @@ mod sanitizers {
12051205
}
12061206
}
12071207

1208-
#[test]
1209-
fn candidates() {
1210-
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
1208+
mod candidates {
1209+
use super::*;
1210+
1211+
// Backed candidates and scheduled parachains used for `sanitize_backed_candidates` testing
1212+
struct TestData {
1213+
backed_candidates: Vec<BackedCandidate>,
1214+
scheduled_paras: BTreeMap<primitives::Id, CoreIndex>,
1215+
}
1216+
1217+
// Generate test data for the candidates test
1218+
fn get_test_data() -> TestData {
12111219
const RELAY_PARENT_NUM: u32 = 3;
12121220

12131221
let header = default_header();
@@ -1233,9 +1241,6 @@ mod sanitizers {
12331241
.unwrap();
12341242
}
12351243

1236-
let has_concluded_invalid =
1237-
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
1238-
12391244
let scheduled = (0_usize..2)
12401245
.into_iter()
12411246
.map(|idx| (ParaId::from(1_u32 + idx as u32), CoreIndex::from(idx as u32)))
@@ -1278,29 +1283,54 @@ mod sanitizers {
12781283
})
12791284
.collect::<Vec<_>>();
12801285

1281-
// happy path
1282-
assert_eq!(
1283-
sanitize_backed_candidates::<Test, _>(
1284-
backed_candidates.clone(),
1285-
has_concluded_invalid,
1286-
&scheduled
1287-
),
1288-
backed_candidates
1289-
);
1286+
TestData { backed_candidates, scheduled_paras: scheduled }
1287+
}
12901288

1291-
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
1292-
{
1289+
#[test]
1290+
fn happy_path() {
1291+
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
1292+
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();
1293+
1294+
let has_concluded_invalid =
1295+
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
1296+
1297+
assert_eq!(
1298+
sanitize_backed_candidates::<Test, _>(
1299+
backed_candidates.clone(),
1300+
has_concluded_invalid,
1301+
&scheduled
1302+
),
1303+
backed_candidates
1304+
);
1305+
1306+
{}
1307+
});
1308+
}
1309+
1310+
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
1311+
#[test]
1312+
fn nothing_scheduled() {
1313+
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
1314+
let TestData { backed_candidates, scheduled_paras: _ } = get_test_data();
12931315
let scheduled = &BTreeMap::new();
1316+
let has_concluded_invalid =
1317+
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
1318+
12941319
assert!(sanitize_backed_candidates::<Test, _>(
12951320
backed_candidates.clone(),
12961321
has_concluded_invalid,
12971322
&scheduled
12981323
)
12991324
.is_empty());
1300-
}
1325+
});
1326+
}
1327+
1328+
// candidates that have concluded as invalid are filtered out
1329+
#[test]
1330+
fn invalid_are_filtered_out() {
1331+
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
1332+
let TestData { backed_candidates, scheduled_paras: scheduled } = get_test_data();
13011333

1302-
// candidates that have concluded as invalid are filtered out
1303-
{
13041334
// mark every second one as concluded invalid
13051335
let set = {
13061336
let mut set = std::collections::HashSet::new();
@@ -1322,7 +1352,7 @@ mod sanitizers {
13221352
.len(),
13231353
backed_candidates.len() / 2
13241354
);
1325-
}
1326-
});
1355+
});
1356+
}
13271357
}
13281358
}

0 commit comments

Comments
 (0)