From 77cb09ec68e6fff8aab82fad1f5ad692ed75c792 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Fri, 20 Oct 2023 15:29:42 +0300 Subject: [PATCH] MetricSelect test --- src/services/task/nested_function/fn_sum.rs | 4 + src/services/task/nested_function/fn_trip.rs | 3 + .../task/nested_function/metric_builder.rs | 7 +- .../task/nested_function/metric_select.rs | 75 +++++---- src/services/task/task.rs | 8 +- .../unit/task/metric/metric_select_test.rs | 156 ++++++++++++++++++ src/tests/unit/task/metric/mod.rs | 1 + src/tests/unit/task/mod.rs | 4 +- 8 files changed, 217 insertions(+), 41 deletions(-) create mode 100644 src/tests/unit/task/metric/metric_select_test.rs create mode 100644 src/tests/unit/task/metric/mod.rs diff --git a/src/services/task/nested_function/fn_sum.rs b/src/services/task/nested_function/fn_sum.rs index f64cb9683..4ba8bd5b9 100644 --- a/src/services/task/nested_function/fn_sum.rs +++ b/src/services/task/nested_function/fn_sum.rs @@ -27,6 +27,8 @@ impl FnIn for FnSum { panic!("FnSum.add | method is not used") } } +/// +/// impl FnOut for FnSum { // // @@ -56,6 +58,8 @@ impl FnOut for FnSum { todo!() } } +/// +/// impl FnInOut for FnSum {} diff --git a/src/services/task/nested_function/fn_trip.rs b/src/services/task/nested_function/fn_trip.rs index d4c3dd692..e55ae6ce7 100644 --- a/src/services/task/nested_function/fn_trip.rs +++ b/src/services/task/nested_function/fn_trip.rs @@ -74,3 +74,6 @@ impl FnOut for FnTripGe { self.input.borrow_mut().reset(); } } +/// +/// +impl FnInOut for FnTripGe {} diff --git a/src/services/task/nested_function/metric_builder.rs b/src/services/task/nested_function/metric_builder.rs index 5edc67e41..02724dbe8 100644 --- a/src/services/task/nested_function/metric_builder.rs +++ b/src/services/task/nested_function/metric_builder.rs @@ -2,7 +2,10 @@ use std::sync::Arc; use log::debug; -use crate::{core_::conf::metric_config::MetricConfig, services::task::{task::TaskNode, nested_function::{metric_select::{MetricSelect, FnMetric}, nested_fn::NestedFn}}}; +use crate::{ + core_::conf::metric_config::MetricConfig, + services::task::{task::TaskNode, nested_function::metric_select::MetricSelect}, +}; use super::fn_inputs::FnInputs; @@ -18,7 +21,7 @@ impl MetricBuilder { match conf.name.as_str() { "sqlSelectMetric" => { debug!("MetricBuilder.new | fnConf: {:?}: {:?}", conf.name, conf); - TaskNode::String( + TaskNode::Metric( Arc::new( MetricSelect::new( conf, diff --git a/src/services/task/nested_function/metric_select.rs b/src/services/task/nested_function/metric_select.rs index c30a00b88..741b039da 100644 --- a/src/services/task/nested_function/metric_select.rs +++ b/src/services/task/nested_function/metric_select.rs @@ -2,28 +2,28 @@ use std::{cell::RefCell, rc::Rc}; -use crate::core_::{conf::metric_config::MetricConfig, point::point_type::PointType}; +use crate::core_::{conf::metric_config::MetricConfig, point::{point_type::PointType, point::Point}}; -use super::{fn_::FnInOut, fn_inputs::FnInputs, nested_fn::NestedFn}; +use super::{fn_::{FnInOut, FnOut, FnIn}, fn_inputs::FnInputs, nested_fn::NestedFn}; -pub trait FnMetric { - /// - /// Creates new MetricXyz instance deppending on config - // fn new(conf: &mut MetricConfig, inputs: &mut FnInputs) -> Self; - /// - /// returns output string containing sql - fn out(&self) -> String; - /// - /// - fn reset(&mut self); -} +// pub trait FnMetric { +// /// +// /// Creates new MetricXyz instance deppending on config +// // fn new(conf: &mut MetricConfig, inputs: &mut FnInputs) -> Self; +// /// +// /// returns output string containing sql +// fn out(&self) -> String; +// /// +// /// +// fn reset(&mut self); +// } /// /// Counts number of raised fronts of boolean input -// #[derive(Debug)] +#[derive(Debug)] pub struct MetricSelect { // _marker: PhantomData, id: String, @@ -51,36 +51,42 @@ impl MetricSelect { } /// /// -impl FnMetric for MetricSelect { - // - // - // fn new(conf: &mut MetricConfig, inputs: &mut FnInputs) -> MetricSelect { - // let - // let func = NestedFn::new(conf, inputs); - // MetricSelect { - // id: conf.name, - // input: func, - // initial: conf.initial, - // table: conf.table, - // sql: conf.sql, - // } - // } +impl FnIn for MetricSelect { + fn add(&mut self, point: PointType) { + panic!("MetricSelect.add | method is not used") + } +} +/// +/// +impl FnOut for MetricSelect { // // - fn out(&self) -> String { + fn out(&mut self) -> PointType { let pointType = self.input.borrow_mut().out(); match pointType { PointType::Bool(point) => { - format!("insert into table values(id, value, timestamp) ({},{},{})", self.id, point.value, point.timestamp) + PointType::String(Point::newString( + "asBool", + format!("insert into {} values(id, value, timestamp) ({},{},{})", self.table, self.id, point.value, point.timestamp) + )) }, PointType::Int(point) => { - format!("insert into table values(id, value, timestamp) ({},{},{})", self.id, point.value, point.timestamp) + PointType::String(Point::newString( + "asBool", + format!("insert into {} values(id, value, timestamp) ({},{},{})", self.table, self.id, point.value, point.timestamp) + )) }, PointType::Float(point) => { - format!("insert into table values(id, value, timestamp) ({},{},{})", self.id, point.value, point.timestamp) + PointType::String(Point::newString( + "asBool", + format!("insert into {} values(id, value, timestamp) ({},{},{})", self.table, self.id, point.value, point.timestamp) + )) }, PointType::String(point) => { - format!("insert into table values(id, value, timestamp) ({},{},{})", self.id, point.value, point.timestamp) + PointType::String(Point::newString( + "asBool", + format!("insert into {} values(id, value, timestamp) ({},{},{})", self.table, self.id, point.value, point.timestamp) + )) }, } } @@ -89,3 +95,6 @@ impl FnMetric for MetricSelect { todo!() } } +/// +/// +impl FnInOut for MetricSelect {} diff --git a/src/services/task/task.rs b/src/services/task/task.rs index 427fccba5..4ab128e4b 100644 --- a/src/services/task/task.rs +++ b/src/services/task/task.rs @@ -14,14 +14,12 @@ use crate::services::task::nested_function::metric_builder::MetricBuilder; use crate::services::task::task_cycle::TaskCycle; use crate::services::task::task_stuff::TaskStuff; +use super::nested_function::fn_::FnOut; use super::nested_function::fn_inputs::FnInputs; -use super::nested_function::metric_select::FnMetric; pub enum TaskNode { - Bool(Arc), - I64(Arc), - F64(Arc), - String(Arc), + Var(Arc), + Metric(Arc), } /// Task implements entity, which provides cyclically (by event) executing calculations diff --git a/src/tests/unit/task/metric/metric_select_test.rs b/src/tests/unit/task/metric/metric_select_test.rs new file mode 100644 index 000000000..f28251f30 --- /dev/null +++ b/src/tests/unit/task/metric/metric_select_test.rs @@ -0,0 +1,156 @@ +#![allow(non_snake_case)] +#[cfg(test)] +use log::{debug, info}; +use std::{sync::Once, rc::Rc, cell::RefCell}; + +use crate::{ + core_::{debug::debug_session::{DebugSession, LogLevel}, + point::{point_type::PointType, point::Point}}, + services::task::nested_function::{fn_::{FnInOut, FnOut}, + fn_count::FnCount, fn_input::FnInput, metric_select::MetricSelect}, +}; + +// Note this useful idiom: importing names from outer (for mod tests) scope. +// use super::*; + +static INIT: Once = Once::new(); + +/// +/// once called initialisation +fn initOnce() { + INIT.call_once(|| { + // implement your initialisation code to be called only once for current test file + } + ) +} + + +/// +/// returns: +/// - ... +fn initEach(initial: PointType) -> Rc>> { + fn boxFnInput(input: MetricSelect) -> Box<(dyn FnInOut)> { + Box::new(input) + } + Rc::new(RefCell::new( + boxFnInput( + FnInput::new("test", initial) + ) + )) +} + + +#[test] +fn test_single() { + DebugSession::init(LogLevel::Debug); + initOnce(); + info!("test_single"); + let input = initEach(PointType::Bool(Point::newBool("bool", false))); + let mut fnCount = FnCount::new( + 0, + input.clone(), + ); + let testData = vec![ + (false, 0), + (false, 0), + (true, 1), + (false, 1), + (false, 1), + (true, 2), + (false, 2), + (true, 3), + (false, 3), + (false, 3), + (true, 4), + (true, 4), + (false, 4), + (false, 4), + ]; + for (value, targetState) in testData { + let point = PointType::Bool(Point::newBool("test", value)); + input.borrow_mut().add(point); + // debug!("input: {:?}", &input); + let state = fnCount.out(); + // debug!("input: {:?}", &mut input); + debug!("value: {:?} | state: {:?}", value, state); + assert_eq!(state.asInt().value, targetState); + } +} +// + +#[test] +fn test_multiple() { + DebugSession::init(LogLevel::Debug); + initOnce(); + info!("test_multiple"); + let input = initEach(PointType::Bool(Point::newBool("bool", false))); + let mut fnCount = FnCount::new( + 0, + input.clone(), + ); + let testData = vec![ + (false, 0), + (false, 0), + (true, 1), + (false, 1), + (false, 1), + (true, 2), + (false, 2), + (true, 3), + (false, 3), + (false, 3), + (true, 4), + (true, 4), + (false, 4), + (false, 4), + ]; + for (value, targetState) in testData { + let point = PointType::Bool(Point::newBool("test", value)); + input.borrow_mut().add(point); + // debug!("input: {:?}", &input); + let state = fnCount.out(); + // debug!("input: {:?}", &mut input); + debug!("value: {:?} | state: {:?}", value, state); + assert_eq!(state.asInt().value, targetState); + } +} + +#[test] +fn test_multiple_reset() { + DebugSession::init(LogLevel::Debug); + initOnce(); + info!("test_multiple_reset"); + let input = initEach(PointType::Bool(Point::newBool("bool", false))); + let mut fnCount = FnCount::new( + 0, + input.clone(), + ); + let testData = vec![ + (false, 0, false), + (false, 0, false), + (true, 1, false), + (false, 1, false), + (false, 1, false), + (true, 2, false), + (false, 0, true), + (true, 1, false), + (false, 1, false), + (false, 1, false), + (true, 2, false), + (true, 2, false), + (false, 0, true), + (false, 0, false), + ]; + for (value, targetState, reset) in testData { + if reset { + fnCount.reset(); + } + let point = PointType::Bool(Point::newBool("test", value)); + input.borrow_mut().add(point); + // debug!("input: {:?}", &input); + let state = fnCount.out(); + // debug!("input: {:?}", &mut input); + debug!("value: {:?} | state: {:?}", value, state); + assert_eq!(state.asInt().value, targetState); + } +} diff --git a/src/tests/unit/task/metric/mod.rs b/src/tests/unit/task/metric/mod.rs new file mode 100644 index 000000000..142513ba6 --- /dev/null +++ b/src/tests/unit/task/metric/mod.rs @@ -0,0 +1 @@ +pub mod metric_select_test; \ No newline at end of file diff --git a/src/tests/unit/task/mod.rs b/src/tests/unit/task/mod.rs index b70c5b71e..e162bb643 100644 --- a/src/tests/unit/task/mod.rs +++ b/src/tests/unit/task/mod.rs @@ -1,3 +1,5 @@ pub mod task_cycle_test; -pub mod task_test; \ No newline at end of file +pub mod task_test; + +pub mod metric; \ No newline at end of file