use std::collections::{BTreeMap, HashMap};
use automapper_validation::eval::{
ConditionEvaluator, ConditionResult, EvaluationContext, NoOpExternalProvider,
};
use automapper_validation::validator::validate::{AhbFieldRule, AhbWorkflow};
use automapper_validation::validator::{EdifactValidator, ValidationLevel};
use mig_types::segment::OwnedSegment;
struct TestEvaluator {
results: HashMap<u32, ConditionResult>,
}
impl TestEvaluator {
fn new(results: HashMap<u32, ConditionResult>) -> Self {
Self { results }
}
}
impl ConditionEvaluator for TestEvaluator {
fn evaluate(&self, condition: u32, _ctx: &EvaluationContext) -> ConditionResult {
self.results
.get(&condition)
.copied()
.unwrap_or(ConditionResult::Unknown)
}
fn is_external(&self, _condition: u32) -> bool {
false
}
fn message_type(&self) -> &str {
"UTILMD"
}
fn format_version(&self) -> &str {
"FV2510"
}
}
fn make_segment(id: &str, elements: Vec<Vec<&str>>) -> OwnedSegment {
OwnedSegment {
id: id.to_string(),
elements: elements
.into_iter()
.map(|e| e.into_iter().map(|c| c.to_string()).collect())
.collect(),
segment_number: 1,
}
}
#[test]
fn test_snapshot_clean_report() {
let evaluator = TestEvaluator::new(HashMap::new());
let validator = EdifactValidator::new(evaluator);
let external = NoOpExternalProvider;
let workflow = AhbWorkflow {
pruefidentifikator: "55001".to_string(),
description: "Anmeldung MaLo".to_string(),
communication_direction: Some("NB an LF".to_string()),
fields: vec![AhbFieldRule {
segment_path: "SG2/NAD/3035".to_string(),
name: "Partnerrolle".to_string(),
ahb_status: "Muss".to_string(),
codes: vec![],
parent_group_ahb_status: None,
segment_ahb_status: None,
..Default::default()
}],
ub_definitions: BTreeMap::new(),
};
let segments = vec![make_segment(
"NAD",
vec![vec!["MS"], vec!["1234567890123", "", "293"]],
)];
let report = validator.validate(&segments, &workflow, &external, ValidationLevel::Full);
let json = serde_json::to_value(&report).expect("report serializes to JSON");
insta::assert_json_snapshot!("clean_report", json);
}
#[test]
fn test_snapshot_report_with_errors() {
let mut conditions = HashMap::new();
conditions.insert(182, ConditionResult::True);
conditions.insert(152, ConditionResult::True);
let evaluator = TestEvaluator::new(conditions);
let validator = EdifactValidator::new(evaluator);
let external = NoOpExternalProvider;
let workflow = AhbWorkflow {
pruefidentifikator: "11001".to_string(),
description: "Lieferbeginn".to_string(),
communication_direction: Some("LF an NB".to_string()),
fields: vec![
AhbFieldRule {
segment_path: "SG2/NAD/3035".to_string(),
name: "Partnerrolle".to_string(),
ahb_status: "Muss".to_string(),
codes: vec![],
parent_group_ahb_status: None,
segment_ahb_status: None,
..Default::default()
},
AhbFieldRule {
segment_path: "SG2/NAD/C082/3039".to_string(),
name: "MP-ID des MSB".to_string(),
ahb_status: "Muss [182] ∧ [152]".to_string(),
codes: vec![],
parent_group_ahb_status: None,
segment_ahb_status: None,
..Default::default()
},
AhbFieldRule {
segment_path: "SG4/DTM/C507/2380".to_string(),
name: "Lieferbeginn-Datum".to_string(),
ahb_status: "X".to_string(),
codes: vec![],
parent_group_ahb_status: None,
segment_ahb_status: None,
..Default::default()
},
],
ub_definitions: BTreeMap::new(),
};
let report = validator.validate(&[], &workflow, &external, ValidationLevel::Full);
let json = serde_json::to_value(&report).expect("report serializes to JSON");
insta::assert_json_snapshot!("report_with_errors", json);
}