From e923ecb9d88061d2ef6f1342613315f1677551c7 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 25 Dec 2024 01:54:16 +0100 Subject: [PATCH] Add tests and fix is_on calculation --- src/db/mod.rs | 3 ++ src/db/model_utils.rs | 5 ++-- src/lib.rs | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index d406c94..542772a 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -23,6 +23,9 @@ pub use relays::DbRelay; pub use schedules::{DbPeriods, DbSchedule}; pub use tag::DbTag; +#[cfg(test)] +pub(crate) use model_utils::Period; + use crate::errors::{DatabaseError, EmgauwaError}; static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations" diff --git a/src/db/model_utils.rs b/src/db/model_utils.rs index db845bc..2421be2 100644 --- a/src/db/model_utils.rs +++ b/src/db/model_utils.rs @@ -63,12 +63,13 @@ impl Period { let start_after_now = self.start.gt(now); // add check for end time being 00:00 because end being 00:00 would cause end_after_now to always be false // this will handle end like 24:00 and end_after_now will be true + // same for start_before_end let end_after_now = self.end.gt(now) || self.end.eq(&NaiveTime::MIN); - let start_before_end = self.start.lt(&self.end); + let start_before_end = self.start.lt(&self.end) || self.end.eq(&NaiveTime::MIN); match (start_after_now, end_after_now, start_before_end) { (false, false, true) => false, // both before now; start before end means "normal" period before now - (false, false, false) => true, // both before now; end before start means "inversed" period around now + (false, false, false) => true, // both before now; end before start means "inverse" period around now (true, false, _) => false, // only start after now (false, true, _) => true, // only end after now (true, true, true) => false, // both after now but start first diff --git a/src/lib.rs b/src/lib.rs index f939d0c..540a8a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,68 @@ pub mod models; pub mod settings; pub mod types; pub mod utils; + +#[cfg(test)] +mod periods { + use chrono::NaiveTime; + use crate::db::Period; + use crate::types::EmgauwaNow; + + const MIDNIGHT: NaiveTime = NaiveTime::MIN; + + fn new_time(hour: u32, minute: u32) -> NaiveTime { + NaiveTime::from_hms_opt(hour, minute, 0).expect("Failed to create NaiveTime") + } + fn new_period(start_hour: u32, start_minute: u32, end_hour: u32, end_minute: u32) -> Period { + Period { + start: new_time(start_hour, start_minute), + end: new_time(end_hour, end_minute), + } + } + + #[test] + fn always_on() { + let period = Period::new_on(); + let now: EmgauwaNow = EmgauwaNow::now(&MIDNIGHT); + assert_eq!(period.is_always_on(), true); + assert_eq!(period.is_on(&MIDNIGHT), true); + assert_eq!(period.is_on(&new_time(12, 00)), true); + assert_eq!(period.is_on(&now.time), true); + } + + #[test] + fn simple_period() { + let period = new_period(11, 00, 13, 00); + assert_eq!(period.is_always_on(), false); + assert_eq!(period.is_on(&MIDNIGHT), false); + assert_eq!(period.is_on(&new_time(10, 00)), false); + assert_eq!(period.is_on(&new_time(11, 00)), true); + assert_eq!(period.is_on(&new_time(12, 00)), true); + assert_eq!(period.is_on(&new_time(13, 00)), false); + assert_eq!(period.is_on(&new_time(14, 00)), false); + } + + #[test] + fn to_midnight_period() { + let period = new_period(22, 00, 00, 00); + assert_eq!(period.is_always_on(), false); + assert_eq!(period.is_on(&MIDNIGHT), false); + assert_eq!(period.is_on(&new_time(21, 00)), false); + assert_eq!(period.is_on(&new_time(22, 00)), true); + assert_eq!(period.is_on(&new_time(23, 00)), true); + assert_eq!(period.is_on(&new_time(00, 00)), false); + assert_eq!(period.is_on(&new_time(01, 00)), false); + } + + #[test] + fn from_midnight_period() { + let period = new_period(00, 00, 02, 00); + assert_eq!(period.is_always_on(), false); + assert_eq!(period.is_on(&MIDNIGHT), true); + assert_eq!(period.is_on(&new_time(23, 00)), false); + assert_eq!(period.is_on(&new_time(00, 00)), true); + assert_eq!(period.is_on(&new_time(01, 00)), true); + assert_eq!(period.is_on(&new_time(02, 00)), false); + assert_eq!(period.is_on(&new_time(03, 00)), false); + } +} \ No newline at end of file