Skip to content

Commit 510ca9b

Browse files
committed
Added test helper to prevent duplicate code
1 parent b6c6e37 commit 510ca9b

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

backend/apportionment/src/candidate_nomination/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,6 @@ fn update_candidate_ranking<T: CandidateVotesTrait>(
262262
mod tests {
263263
use test_log::test;
264264

265-
use crate::test_helpers::{
266-
candidate_nomination_fixture_with_given_list_numbers_and_number_of_seats,
267-
seat_assignment_fixture_with_given_candidate_votes,
268-
seat_assignment_fixture_with_given_list_numbers_and_candidate_votes,
269-
};
270265
use crate::{
271266
ApportionmentError, CandidateVotesTrait,
272267
candidate_nomination::{
@@ -276,8 +271,10 @@ mod tests {
276271
structs::ListNumber,
277272
test_helpers::{
278273
ApportionmentInputMock, CandidateVotesMock,
274+
candidate_nomination_fixture_with_given_list_numbers_and_number_of_seats,
279275
candidate_nomination_fixture_with_given_number_of_seats,
280276
seat_assignment_fixture_with_given_candidate_votes,
277+
seat_assignment_fixture_with_given_list_numbers_and_candidate_votes,
281278
},
282279
};
283280

backend/apportionment/src/seat_assignment/mod.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,12 @@ pub(crate) mod tests {
283283
use crate::{
284284
SeatAssignmentResult,
285285
fraction::Fraction,
286-
seat_assignment::{ListStanding, SeatChange, list_numbers},
286+
seat_assignment::{
287+
ListStanding, SeatChange, get_total_seats_per_list_number_from_apportionment_result,
288+
list_numbers,
289+
},
287290
structs::ListNumber,
291+
test_helpers::convert_total_seats_per_u32_list_number_to_total_seats_per_list_number,
288292
};
289293
use test_log::test;
290294

@@ -297,17 +301,14 @@ pub(crate) mod tests {
297301

298302
fn check_total_seats_per_list(
299303
result: &SeatAssignmentResult,
300-
expected_total_seats: Vec<(u32, u32)>,
304+
expected_total_seats_per_list: Vec<(u32, u32)>,
301305
) {
302-
let total_seats_per_list_number = result
303-
.final_standing
304-
.iter()
305-
.map(|p| (p.list_number, p.total_seats))
306-
.collect::<Vec<_>>();
307-
let expected_total_seats_per_list_number: Vec<(ListNumber, u32)> = expected_total_seats
308-
.iter()
309-
.map(|(number, seats)| (ListNumber::from(*number), *seats))
310-
.collect();
306+
let total_seats_per_list_number =
307+
get_total_seats_per_list_number_from_apportionment_result(result);
308+
let expected_total_seats_per_list_number =
309+
convert_total_seats_per_u32_list_number_to_total_seats_per_list_number(
310+
expected_total_seats_per_list,
311+
);
311312
assert_eq!(
312313
expected_total_seats_per_list_number,
313314
total_seats_per_list_number

backend/apportionment/src/test_helpers.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ impl ListVotesMock {
8484
}
8585
}
8686

87+
#[cfg(test)]
88+
pub fn convert_total_seats_per_u32_list_number_to_total_seats_per_list_number(
89+
total_seats_per_list_number: Vec<(u32, u32)>,
90+
) -> Vec<(ListNumber, u32)> {
91+
total_seats_per_list_number
92+
.iter()
93+
.map(|(number, total_seats)| (ListNumber::from(*number), *total_seats))
94+
.collect()
95+
}
96+
8797
/// Create a CandidateNominationInput with consecutive list numbers and
8898
/// given quota, number of seats, candidate votes and total seats per list.
8999
pub fn candidate_nomination_fixture_with_given_number_of_seats(
@@ -116,10 +126,10 @@ pub fn candidate_nomination_fixture_with_given_list_numbers_and_number_of_seats(
116126
number_of_seats: seat_assignment_input.number_of_seats,
117127
list_votes: &seat_assignment_input.list_votes,
118128
quota,
119-
total_seats_per_list: total_seats_per_list_number
120-
.iter()
121-
.map(|(number, total_seats)| (ListNumber::from(*number), *total_seats))
122-
.collect(),
129+
total_seats_per_list:
130+
convert_total_seats_per_u32_list_number_to_total_seats_per_list_number(
131+
total_seats_per_list_number,
132+
),
123133
}
124134
}
125135

0 commit comments

Comments
 (0)