Skip to content

Commit

Permalink
MetricConfig test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Oct 10, 2023
1 parent 8a98d3d commit 9a0f908
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/core_/conf/metric_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ impl MetricConfig {
///
/// reads config from path
#[allow(dead_code)]
pub fn read(path: &str) -> FnConfig {
pub fn read(path: &str) -> MetricConfig {
let mut vars = vec![];
match fs::read_to_string(&path) {
Ok(yamlString) => {
match serde_yaml::from_str(&yamlString) {
Ok(config) => {
FnConfig::fromYamlValue(&config, &mut vars)
MetricConfig::fromYamlValue(&config, &mut vars)
},
Err(err) => {
panic!("Error in config: {:?}\n\terror: {:?}", yamlString, err)
Expand Down
74 changes: 74 additions & 0 deletions src/tests/unit/metric_config/metric_config_read_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![allow(non_snake_case)]
#[cfg(test)]
use log::{trace, info};
use std::{sync::Once, env, collections::HashMap};

use crate::core_::{conf::{fn_config::FnConfig, fn_config_type::FnConfigType, metric_config::MetricConfig}, debug::debug_session::{DebugSession, LogLevel}};

// 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() -> () {

}

#[test]
fn test_fn_config_read_valid() {
DebugSession::init(LogLevel::Trace);
initOnce();
initEach();
info!("test_fn_config_read_valid");
let target = MetricConfig {
name: String::from("metric sqlSelectMetric"),
table: String::from("table_name"),
sql: String::from("UPDATE {table} SET kind = '{input1}' WHERE id = '{input2}';"),
initial: String::from("0"),
vars: vec![String::from("VarName2")],
inputs: HashMap::from([
(String::from("input1"), FnConfig {
fnType: FnConfigType::Var, name: String::from("VarName2"), inputs: HashMap::from([
(String::from("input"), FnConfig {
fnType: FnConfigType::Fn, name: String::from("functionName"), inputs: HashMap::from([
(String::from("initial"), FnConfig { fnType: FnConfigType::Var, name: String::from("VarName2"), inputs: HashMap::new() }),
(String::from("input"), FnConfig {
fnType: FnConfigType::Fn, name: String::from("functionName"), inputs: HashMap::from([
(String::from("input1"), FnConfig { fnType: FnConfigType::Const, name: String::from("someValue"), inputs: HashMap::new() }),
(String::from("input2"), FnConfig { fnType: FnConfigType::Point, name: String::from("/path/Point.Name/"), inputs: HashMap::new() }),
(String::from("input"), FnConfig {
fnType: FnConfigType::Fn, name: String::from("functionName"), inputs: HashMap::from([
(String::from("input"), FnConfig { fnType: FnConfigType::Point, name: String::from("/path/Point.Name/"), inputs: HashMap::new() }),
])
}),
])
}),
])
})
])
}),
(String::from("input2"), FnConfig { fnType: FnConfigType::Const, name: String::from("1"), inputs: HashMap::new() })
]),
};

// let (initial, switches) = initEach();
trace!("dir: {:?}", env::current_dir());
let path = "./src/tests/unit/metric_config/metric_config_test.yaml";
let metricConfig = MetricConfig::read(path);
trace!("fnConfig: {:?}", metricConfig);
assert_eq!(metricConfig, target);
}

3 changes: 2 additions & 1 deletion src/tests/unit/metric_config/metric_config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# fn functionName:
# input: point '/path/Point.Name/'
metric sqlSelectMetric:
initial: 0 # начальное значение
initial: "0" # начальное значение
table: table_name
sql: "UPDATE {table} SET kind = '{input1}' WHERE id = '{input2}';"
inputs:
input1:
Expand Down
3 changes: 2 additions & 1 deletion src/tests/unit/metric_config/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod metric_config_new_test;
pub mod metric_config_new_test;
pub mod metric_config_read_test;

0 comments on commit 9a0f908

Please sign in to comment.