From 270a1a9bdffa051efefd754c1ad0fad062d6a060 Mon Sep 17 00:00:00 2001 From: ppom Date: Thu, 10 Jul 2025 12:00:00 +0200 Subject: [PATCH] Duplicate: Fix tests, more tests --- src/daemon/filter/tests.rs | 42 ++++++-------------------------------- src/treedb/helpers.rs | 35 ++++++++++++++++++++++++++++++- tests/simple.rs | 1 + 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/daemon/filter/tests.rs b/src/daemon/filter/tests.rs index 077c64c..44a4a02 100644 --- a/src/daemon/filter/tests.rs +++ b/src/daemon/filter/tests.rs @@ -11,7 +11,7 @@ use tokio::sync::Semaphore; use super::{state::filter_ordered_times_db_name, FilterManager, React}; use crate::{ - concepts::{Action, Filter, MatchTime, Pattern, Patterns, Time}, + concepts::{Action, Filter, Pattern, Patterns, Time}, daemon::shutdown::ShutdownController, tests::TempDatabase, }; @@ -173,13 +173,7 @@ async fn three_matches_then_action_then_delayed_action() { ); assert_eq!( state.triggers.tree(), - &BTreeMap::from([( - MatchTime { - m: one.clone(), - t: now2s - }, - 1 - )]), + &BTreeMap::from([(one.clone(), BTreeMap::from([(now2s, 1)]))]), // 1 and not 2 because the decrement_trigger() doesn't wait for the semaphore "triggers now contain the triggered match with 1 action left" ); @@ -191,13 +185,7 @@ async fn three_matches_then_action_then_delayed_action() { // Check first action assert_eq!( bed.manager.state.lock().unwrap().triggers.tree(), - &BTreeMap::from([( - MatchTime { - m: one.clone(), - t: now2s - }, - 1 - )]), + &BTreeMap::from([(one.clone(), BTreeMap::from([(now2s, 1)]))]), "triggers still contain the triggered match with 1 action left" ); assert_eq!( @@ -302,13 +290,7 @@ async fn one_match_one_delayed_action() { assert!(state.ordered_times.is_empty(), "ordered_times stay empty"); assert_eq!( state.triggers.tree(), - &BTreeMap::from([( - MatchTime { - m: one.clone(), - t: now, - }, - 1 - )]), + &BTreeMap::from([(one.clone(), BTreeMap::from([(now, 1)]))]), "triggers still contain the triggered match with 1 action left" ); } @@ -497,13 +479,7 @@ async fn trigger_unmatched_pattern() { assert!(state.ordered_times.is_empty()); assert_eq!( state.triggers.tree(), - &BTreeMap::from([( - MatchTime { - m: one.clone(), - t: now, - }, - 1 - )]) + &BTreeMap::from([(one.clone(), BTreeMap::from([(now, 1)]))]), ); } assert_eq!( @@ -578,13 +554,7 @@ async fn trigger_matched_pattern() { assert!(state.ordered_times.is_empty()); assert_eq!( state.triggers.tree(), - &BTreeMap::from([( - MatchTime { - m: one.clone(), - t: now, - }, - 1 - )]) + &BTreeMap::from([(one.clone(), BTreeMap::from([(now, 1)]))]), ); } assert_eq!( diff --git a/src/treedb/helpers.rs b/src/treedb/helpers.rs index ae4db1d..f7afbeb 100644 --- a/src/treedb/helpers.rs +++ b/src/treedb/helpers.rs @@ -23,7 +23,7 @@ fn string_to_time(val: &str) -> Result { /// Tries to convert a [`Value`] into a [`Time`] pub fn to_time(val: &Value) -> Result { - Ok(string_to_time(val.as_str().ok_or("not a number")?)?) + Ok(string_to_time(val.as_str().ok_or("not a datetime")?)?) } /// Tries to convert a [`Value`] into a [`Match`] @@ -200,4 +200,37 @@ mod tests { assert!(to_timeset(&(8.into())).is_err()); assert!(to_timeset(&(None::.into())).is_err()); } + + #[test] + fn test_to_timemap() { + let time1 = "2025-07-10T12:35:00.000+02:00"; + let time1_t = DateTime::parse_from_rfc3339(time1) + .unwrap() + .with_timezone(&Local); + let time2 = "2026-08-11T12:36:01.000+02:00"; + let time2_t = DateTime::parse_from_rfc3339(time2) + .unwrap() + .with_timezone(&Local); + + assert_eq!( + to_timemap(&Value::from_iter([(time2, 1)])), + Ok(BTreeMap::from([(time2_t, 1)])) + ); + assert_eq!( + to_timemap(&Value::from_iter([(time1, 4), (time2, 0)])), + Ok(BTreeMap::from([(time1_t, 4), (time2_t, 0)])) + ); + + assert!(to_timemap(&Value::from_iter([("1", time2)])).is_err()); + assert!(to_timemap(&Value::from_iter([(time2, time2)])).is_err()); + assert!(to_timemap(&Value::from_iter([(time2)])).is_err()); + assert!(to_timemap(&Value::from_iter([(1)])).is_err()); + + assert!(to_timemap(&(["1970-01-01T01:20:34.567+01:00"].into())).is_err()); + assert!(to_timemap(&([""].into())).is_err()); + assert!(to_timemap(&(["ploup"].into())).is_err()); + assert!(to_timemap(&(true.into())).is_err()); + assert!(to_timemap(&(8.into())).is_err()); + assert!(to_timemap(&(None::.into())).is_err()); + } } diff --git a/tests/simple.rs b/tests/simple.rs index dcbca3e..1e7905e 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -36,6 +36,7 @@ fn config_with_cmd(config_path: &str, cmd: &str) { regex: ['here is '], retry: 2, retryperiod: '2s', + duplicate: 'rerun', actions: { // Don't mix code and data at home! // You may permit arbitrary execution from vilains,