use crate::ExecutionResult;
use super::common::{Math, MathContext, MathError, MathInputs, SingleAdd, SingleAddInputs};
#[tokio::test]
async fn underflow_triggers_compensation() {
let saga = Math::new(MathInputs {
add: ("a".into(), 1, 2), sub: ("b".into(), 0, 1), halt: (),
});
let exec = saga.build(MathContext::default());
match exec.start().await {
ExecutionResult::Failed(e, err) => {
assert_eq!(err, MathError::Underflow);
assert!(!e.context().r.contains_key("a"));
}
other => panic!("Expected Failed, got {:?}", std::mem::discriminant(&other)),
}
}
#[tokio::test]
async fn overflow_triggers_failure() {
let saga = SingleAdd::new(SingleAddInputs {
add: ("x".into(), 250, 20), });
let exec = saga.build(MathContext::default());
match exec.start().await {
ExecutionResult::Failed(_, err) => {
assert_eq!(err, MathError::Overflow);
}
other => panic!("Expected Failed, got {:?}", std::mem::discriminant(&other)),
}
}
#[tokio::test]
async fn multiple_steps_rollback() {
let saga = Math::new(MathInputs {
add: ("a".into(), 1, 2), sub: ("b".into(), 0, 1), halt: (),
});
let exec = saga.build(MathContext::default());
match exec.start().await {
ExecutionResult::Failed(e, _) => {
assert!(!e.context().r.contains_key("a"));
assert!(!e.context().r.contains_key("b"));
}
other => panic!("Expected Failed, got {:?}", std::mem::discriminant(&other)),
}
}