use chrono::{Datelike, NaiveDate, Weekday};
use findates::algebra;
use findates::calendar;
use findates::calendar::Calendar;
use findates::conventions::AdjustRule;
#[test]
fn is_business_day_test() {
let mut basic_cal = calendar::basic_calendar();
let sunday = NaiveDate::from_isoywd_opt(2015, 10, Weekday::Sun);
assert!(!algebra::is_business_day(&sunday.unwrap(), &basic_cal));
let monday = NaiveDate::from_isoywd_opt(2015, 10, Weekday::Mon);
assert!(algebra::is_business_day(&monday.unwrap(), &basic_cal));
let christmas_day = NaiveDate::from_ymd_opt(2023, 12, 25).unwrap();
assert!(algebra::is_business_day(&christmas_day, &basic_cal));
basic_cal.add_holidays([christmas_day]);
assert!(!algebra::is_business_day(&christmas_day, &basic_cal));
}
#[test]
fn convenience_constructors_work_in_business_day_logic() {
let xmas = NaiveDate::from_ymd_opt(2024, 12, 25).unwrap();
let boxing_day = NaiveDate::from_ymd_opt(2024, 12, 26).unwrap();
let saturday = NaiveDate::from_ymd_opt(2024, 12, 28).unwrap();
let mut cal = Calendar::with_weekends([Weekday::Sat, Weekday::Sun]);
cal.add_holidays(vec![xmas, xmas, boxing_day]);
let holiday_only = Calendar::with_holidays([xmas, boxing_day].into_iter().take(1));
assert!(!algebra::is_business_day(&xmas, &cal));
assert!(!algebra::is_business_day(&saturday, &cal));
assert!(holiday_only.get_holidays().contains(&xmas));
assert_eq!(holiday_only.get_holidays().len(), 1);
assert_eq!(
algebra::adjust(&xmas, Some(&cal), Some(AdjustRule::Following)),
NaiveDate::from_ymd_opt(2024, 12, 27).unwrap()
);
}
#[test]
fn schedule_test() {
let mut test_schedule: Vec<NaiveDate> = [].to_vec();
let hol = NaiveDate::from_ymd_opt(2023, 9, 22).unwrap();
for i in 2..31 {
let dt = NaiveDate::from_ymd_opt(2023, 9, i).unwrap();
if dt.weekday() != Weekday::Sat && dt.weekday() != Weekday::Sun && dt != hol {
test_schedule.push(dt);
}
}
let mut cal = calendar::basic_calendar();
cal.add_holidays([hol]);
let start_date: NaiveDate = NaiveDate::from_ymd_opt(2023, 9, 2).unwrap();
let end_date: NaiveDate = NaiveDate::from_ymd_opt(2023, 9, 30).unwrap();
let res: Vec<NaiveDate> =
algebra::bus_day_schedule(&start_date, &end_date, &cal, Some(AdjustRule::ModFollowing));
assert_eq!(test_schedule, res);
}
#[test]
fn bus_days_between_test() {
let mut test_schedule: Vec<NaiveDate> = [].to_vec();
let hol: NaiveDate = NaiveDate::from_ymd_opt(2023, 9, 22).unwrap();
for i in 1..29 {
let dt = NaiveDate::from_ymd_opt(2023, 9, i).unwrap();
if dt.weekday() != Weekday::Sat && dt.weekday() != Weekday::Sun && dt != hol {
test_schedule.push(dt);
}
}
let mut cal = calendar::basic_calendar();
cal.add_holidays([hol]);
let start_date: NaiveDate = NaiveDate::from_ymd_opt(2023, 9, 1).unwrap();
let end_date: NaiveDate = NaiveDate::from_ymd_opt(2023, 9, 29).unwrap();
let res: u64 =
algebra::business_days_between(&start_date, &end_date, &cal, Some(AdjustRule::Preceding));
assert_eq!(test_schedule.len() as u64, res);
}
#[test]
fn bus_day_schedule_single_day_test() {
let cal = calendar::basic_calendar();
let monday = NaiveDate::from_ymd_opt(2024, 3, 18).unwrap();
let result = algebra::bus_day_schedule(&monday, &monday, &cal, None);
assert_eq!(result, vec![monday]);
}
#[test]
fn business_days_between_same_day_test() {
let cal = calendar::basic_calendar();
let monday = NaiveDate::from_ymd_opt(2024, 3, 18).unwrap();
assert_eq!(
algebra::business_days_between(&monday, &monday, &cal, None),
0
);
}
#[test]
fn bus_day_schedule_holiday_gap_test() {
let mut cal = calendar::basic_calendar();
let xmas = NaiveDate::from_ymd_opt(2024, 12, 25).unwrap();
let boxing_day = NaiveDate::from_ymd_opt(2024, 12, 26).unwrap();
cal.add_holidays([xmas, boxing_day]);
let start = NaiveDate::from_ymd_opt(2024, 12, 23).unwrap(); let end = NaiveDate::from_ymd_opt(2024, 12, 27).unwrap(); let days = algebra::bus_day_schedule(&start, &end, &cal, None);
assert_eq!(
days,
vec![
NaiveDate::from_ymd_opt(2024, 12, 23).unwrap(),
NaiveDate::from_ymd_opt(2024, 12, 24).unwrap(),
NaiveDate::from_ymd_opt(2024, 12, 27).unwrap(),
]
);
}
#[test]
fn calendar_default_is_empty_test() {
let cal = calendar::Calendar::default();
assert!(cal.get_holidays().is_empty());
assert!(cal.get_weekend().is_empty());
}