|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use log::{error, info}; |
| 4 | +use serde::Deserialize; |
| 5 | +use serde_json::json; |
| 6 | +use taurus_core::runtime::engine::ExecutionEngine; |
| 7 | +use tucana::shared::{ |
| 8 | + ValidationFlow, |
| 9 | + helper::value::{from_json_value, to_json_value}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Clone, Deserialize)] |
| 13 | +pub struct Input { |
| 14 | + pub input: Option<serde_json::Value>, |
| 15 | + pub expected_result: serde_json::Value, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Clone, Deserialize)] |
| 19 | +pub struct Case { |
| 20 | + pub name: String, |
| 21 | + pub description: String, |
| 22 | + pub inputs: Vec<Input>, |
| 23 | + pub flow: ValidationFlow, |
| 24 | +} |
| 25 | + |
| 26 | +pub enum CaseResult { |
| 27 | + Success, |
| 28 | + Failure(Input, serde_json::Value), |
| 29 | +} |
| 30 | + |
| 31 | +pub trait Testable { |
| 32 | + fn run(&self) -> CaseResult; |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Clone, Deserialize)] |
| 36 | +pub struct Cases { |
| 37 | + pub cases: Vec<Case>, |
| 38 | +} |
| 39 | + |
| 40 | +pub fn print_success(case: &Case) { |
| 41 | + info!("test {} ... ok", case.name); |
| 42 | +} |
| 43 | + |
| 44 | +pub fn print_failure(case: &Case, input: &Input, result: serde_json::Value) { |
| 45 | + error!("test {} ... FAILED", case.name); |
| 46 | + error!(" input: {:?}", input.input); |
| 47 | + error!(" expected: {:?}", input.expected_result); |
| 48 | + error!(" real_value: {:?}", result); |
| 49 | + error!(" message: {}", case.description); |
| 50 | +} |
| 51 | + |
| 52 | +fn get_test_case<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Option<Case> { |
| 53 | + let content = match std::fs::read_to_string(&path) { |
| 54 | + Ok(it) => it, |
| 55 | + Err(err) => { |
| 56 | + log::error!("Cannot read file ({:?}): {:?}", path, err); |
| 57 | + return None; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + match serde_json::from_str(&content) { |
| 62 | + Ok(it) => it, |
| 63 | + Err(err) => { |
| 64 | + log::error!("Cannot read json ({:?}): {:?}", path, err); |
| 65 | + None |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn get_test_cases(path: &str) -> Cases { |
| 71 | + let mut items = Vec::new(); |
| 72 | + let dir = match std::fs::read_dir(path) { |
| 73 | + Ok(d) => d, |
| 74 | + Err(err) => { |
| 75 | + panic!("Cannot open path: {:?}", err) |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + for entry in dir { |
| 80 | + let entry = match entry { |
| 81 | + Ok(it) => it, |
| 82 | + Err(err) => { |
| 83 | + log::error!("Cannot read entry: {:?}", err); |
| 84 | + continue; |
| 85 | + } |
| 86 | + }; |
| 87 | + let file_path = entry.path(); |
| 88 | + items.push(match get_test_case(&file_path) { |
| 89 | + Some(it) => it, |
| 90 | + None => { |
| 91 | + continue; |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + Cases { cases: items } |
| 97 | +} |
| 98 | + |
| 99 | +impl Case { |
| 100 | + pub fn from_path(path: &str) -> Self { |
| 101 | + match get_test_case(path) { |
| 102 | + Some(s) => s, |
| 103 | + None => panic!("flow was not found"), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl Cases { |
| 109 | + pub fn from_path(path: &str) -> Self { |
| 110 | + get_test_cases(path) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fn run_tests(cases: Cases) { |
| 115 | + for case in &cases.cases { |
| 116 | + match case.run() { |
| 117 | + CaseResult::Success => print_success(case), |
| 118 | + CaseResult::Failure(input, result) => print_failure(case, &input, result), |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl Testable for Case { |
| 124 | + fn run(&self) -> CaseResult { |
| 125 | + let engine = ExecutionEngine::new(); |
| 126 | + |
| 127 | + for input in self.inputs.clone() { |
| 128 | + let flow_input = input.clone().input.map(from_json_value); |
| 129 | + let (res, _) = engine.execute_graph( |
| 130 | + self.flow.starting_node_id, |
| 131 | + self.flow.node_functions.clone(), |
| 132 | + flow_input, |
| 133 | + None, |
| 134 | + None, |
| 135 | + true, |
| 136 | + ); |
| 137 | + |
| 138 | + match res { |
| 139 | + taurus_core::types::signal::Signal::Failure(err) => { |
| 140 | + let json = json!({ |
| 141 | + "name": err.category, |
| 142 | + "message": err.message, |
| 143 | + }); |
| 144 | + if json != input.clone().expected_result { |
| 145 | + return CaseResult::Failure(input, json); |
| 146 | + } |
| 147 | + } |
| 148 | + taurus_core::types::signal::Signal::Success(value) => { |
| 149 | + let json = to_json_value(value); |
| 150 | + if json != input.clone().expected_result { |
| 151 | + return CaseResult::Failure(input, json); |
| 152 | + } |
| 153 | + } |
| 154 | + taurus_core::types::signal::Signal::Return(value) => { |
| 155 | + let json = to_json_value(value); |
| 156 | + if json != input.clone().expected_result { |
| 157 | + return CaseResult::Failure(input, json); |
| 158 | + } |
| 159 | + } |
| 160 | + taurus_core::types::signal::Signal::Respond(value) => { |
| 161 | + let json = to_json_value(value); |
| 162 | + if json != input.clone().expected_result { |
| 163 | + return CaseResult::Failure(input, json); |
| 164 | + } |
| 165 | + } |
| 166 | + taurus_core::types::signal::Signal::Stop => continue, |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + CaseResult::Success |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +fn main() { |
| 175 | + env_logger::Builder::from_default_env() |
| 176 | + .filter_level(log::LevelFilter::Info) |
| 177 | + .init(); |
| 178 | + |
| 179 | + let cases = Cases::from_path("./flows/"); |
| 180 | + run_tests(cases); |
| 181 | +} |
0 commit comments