Skip to content

Commit

Permalink
MetricSelect test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Oct 20, 2023
1 parent cde5a97 commit 77cb09e
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/services/task/nested_function/fn_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl FnIn for FnSum {
panic!("FnSum.add | method is not used")
}
}
///
///
impl FnOut for FnSum {
//
//
Expand Down Expand Up @@ -56,6 +58,8 @@ impl FnOut for FnSum {
todo!()
}
}
///
///
impl FnInOut for FnSum {}


Expand Down
3 changes: 3 additions & 0 deletions src/services/task/nested_function/fn_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ impl FnOut for FnTripGe {
self.input.borrow_mut().reset();
}
}
///
///
impl FnInOut for FnTripGe {}
7 changes: 5 additions & 2 deletions src/services/task/nested_function/metric_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
75 changes: 42 additions & 33 deletions src/services/task/nested_function/metric_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>,
id: String,
Expand Down Expand Up @@ -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)
))
},
}
}
Expand All @@ -89,3 +95,6 @@ impl FnMetric for MetricSelect {
todo!()
}
}
///
///
impl FnInOut for MetricSelect {}
8 changes: 3 additions & 5 deletions src/services/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn FnMetric>),
I64(Arc<dyn FnMetric>),
F64(Arc<dyn FnMetric>),
String(Arc<dyn FnMetric>),
Var(Arc<dyn FnOut>),
Metric(Arc<dyn FnOut>),
}

/// Task implements entity, which provides cyclically (by event) executing calculations
Expand Down
156 changes: 156 additions & 0 deletions src/tests/unit/task/metric/metric_select_test.rs
Original file line number Diff line number Diff line change
@@ -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<RefCell<Box<dyn FnInOut>>> {
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);
}
}
1 change: 1 addition & 0 deletions src/tests/unit/task/metric/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod metric_select_test;
4 changes: 3 additions & 1 deletion src/tests/unit/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod task_cycle_test;

pub mod task_test;
pub mod task_test;

pub mod metric;

0 comments on commit 77cb09e

Please sign in to comment.