Reorganize integration tests, simplify fn signatures

This commit is contained in:
2026-02-22 13:01:28 +02:00
parent a41d1cd469
commit 76d308351a
8 changed files with 270 additions and 344 deletions

View File

@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::{
resident::{Resident, ResidentDTO, ResidentId},
schedule::ShiftType,
slot::Day,
slot::{Day, Slot},
};
const MONTH: u8 = 4;
@@ -58,6 +58,32 @@ pub struct UserConfig {
}
impl UserConfig {
pub fn new(month: u8, year: i32) -> Self {
let month = Month::try_from(month).unwrap();
let total_days = month.num_days(year).unwrap();
let total_slots = (1..=total_days)
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum();
let total_holiday_slots = (1..=total_days)
.filter(|&d| Day(d).is_weekend(month.number_from_month(), year))
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum();
Self {
month,
year,
holidays: vec![],
residents: vec![],
toxic_pairs: vec![],
total_days,
total_slots,
total_holiday_slots,
}
}
pub fn with_holidays(mut self, holidays: Vec<u8>) -> Self {
self.holidays = holidays;
self.total_holiday_slots = self.total_holiday_slots();
@@ -80,15 +106,18 @@ impl UserConfig {
fn total_holiday_slots(&self) -> u8 {
(1..=self.total_days)
.filter(|&d| self.is_holiday_or_weekend_slot(d))
.filter(|&d| self.is_holiday_or_weekend(Day(d)))
.map(|d| if Day(d).is_open_shift() { 2 } else { 1 })
.sum()
}
pub fn is_holiday_or_weekend_slot(&self, day: u8) -> bool {
let day = Day(day);
day.is_weekend(self.month.number_from_month(), self.year)
|| self.holidays.contains(&(day.0))
pub fn is_holiday_or_weekend(&self, day: Day) -> bool {
let month = self.month.number_from_month();
day.is_weekend(month, self.year) || self.holidays.contains(&(day.0))
}
pub fn is_holiday_or_weekend_slot(&self, slot: Slot) -> bool {
self.is_holiday_or_weekend(slot.day)
}
pub fn get_initial_supply(&self) -> HashMap<ShiftType, u8> {
@@ -174,7 +203,12 @@ impl TryFrom<UserConfigDTO> for UserConfig {
#[cfg(test)]
mod tests {
use crate::{config::UserConfig, fixtures::complex_config, schedule::ShiftType};
use crate::{
config::UserConfig,
fixtures::complex_config,
schedule::ShiftType,
slot::{Day, ShiftPosition, Slot},
};
use rstest::rstest;
#[rstest]
@@ -185,4 +219,25 @@ mod tests {
assert_eq!(15, *supply.get(&ShiftType::OpenSecond).unwrap());
assert_eq!(15, *supply.get(&ShiftType::Closed).unwrap());
}
#[rstest]
fn test_is_holiday_or_weekend(complex_config: UserConfig) {
assert!(!complex_config.is_holiday_or_weekend(Day(1)));
assert!(complex_config.is_holiday_or_weekend(Day(4)));
assert!(complex_config.is_holiday_or_weekend(Day(5)));
assert!(complex_config.is_holiday_or_weekend(Day(10)));
}
#[rstest]
fn test_is_holiday_or_weekend_slot(complex_config: UserConfig) {
let weekday = Slot::new(Day(1), ShiftPosition::First);
let sat = Slot::new(Day(4), ShiftPosition::First);
let sun = Slot::new(Day(5), ShiftPosition::First);
let manual_holiday = Slot::new(Day(10), ShiftPosition::First);
assert!(!complex_config.is_holiday_or_weekend_slot(weekday));
assert!(complex_config.is_holiday_or_weekend_slot(sat));
assert!(complex_config.is_holiday_or_weekend_slot(sun));
assert!(complex_config.is_holiday_or_weekend_slot(manual_holiday));
}
}