use json_eval_rs::RLogic;
use serde_json::json;
#[test]
fn test_sum_without_threshold() {
let mut engine = RLogic::new();
let logic = json!({"SUM": [[1, 2, 3, 4, 5]]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &json!({})).unwrap();
assert_eq!(result, json!(15)); }
#[test]
fn test_sum_with_threshold() {
let mut engine = RLogic::new();
let logic = json!({"SUM": [[1, 2, 3, 4, 5], null, 2]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &json!({})).unwrap();
assert_eq!(result, json!(6)); }
#[test]
fn test_sum_with_field_and_threshold() {
let mut engine = RLogic::new();
let data = json!({
"table": [
{"value": 10},
{"value": 20},
{"value": 30},
{"value": 40},
{"value": 50}
]
});
let logic = json!({"SUM": [{"var": "table"}, "value", 2]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &data).unwrap();
assert_eq!(result, json!(60)); }
#[test]
fn test_sum_with_threshold_zero() {
let mut engine = RLogic::new();
let logic = json!({"SUM": [[1, 2, 3, 4, 5], null, 0]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &json!({})).unwrap();
assert_eq!(result, json!(1));
}
#[test]
fn test_sum_with_threshold_larger_than_array() {
let mut engine = RLogic::new();
let logic = json!({"SUM": [[1, 2, 3], null, 10]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &json!({})).unwrap();
assert_eq!(result, json!(6)); }
#[test]
fn test_sum_with_negative_threshold() {
let mut engine = RLogic::new();
let logic = json!({"SUM": [[1, 2, 3, 4, 5], null, -1]});
let compiled = engine.compile(&logic).unwrap();
let result = engine.run(&compiled, &json!({})).unwrap();
assert_eq!(result, json!(15));
}