use crate::expr::Expr;
use crate::workflow::SourceLocation;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TestCase {
pub name: String,
pub workflow: String,
pub version: String,
pub input: Value,
pub mocks: BTreeMap<String, Vec<MockBehaviour>>,
pub signals: Vec<TestSignal>,
pub expectations: Vec<Expectation>,
pub location: SourceLocation,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MockBehaviour {
pub outcome: MockOutcome,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub times: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum MockOutcome {
Returns { output: Expr },
Fails {
code: String,
message: String,
retryable: bool,
},
Hangs,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TestSignal {
pub name: String,
pub payload: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Expectation {
Status { status: String },
Output { expected: Value },
Error { code: String },
StepStatus { step: String, status: String },
StepOutput { step: String, expected: Value },
StepAttempts { step: String, attempts: u32 },
Compensated { step: String },
Runs { runs: u32 },
}
impl fmt::Display for Expectation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expectation::Status { status } => write!(f, "status {status}"),
Expectation::Output { expected } => write!(f, "output {expected}"),
Expectation::Error { code } => write!(f, "error {code:?}"),
Expectation::StepStatus { step, status } => write!(f, "step {step} {status}"),
Expectation::StepOutput { step, expected } => {
write!(f, "step {step} output {expected}")
}
Expectation::StepAttempts { step, attempts } => {
write!(f, "step {step} attempts {attempts}")
}
Expectation::Compensated { step } => write!(f, "compensated {step}"),
Expectation::Runs { runs } => write!(f, "runs {runs}"),
}
}
}
pub const TEST_WORKFLOW_STATUSES: &[&str] = &["COMPLETED", "FAILED", "CANCELLED", "TIMED_OUT"];
pub const TEST_STEP_STATUSES: &[&str] = &[
"PENDING",
"RUNNING",
"SCHEDULED",
"DISPATCHED",
"ATTEMPT_FAILED",
"UNCERTAIN",
"RETRY_WAIT",
"WAITING",
"SUCCEEDED",
"SKIPPED",
"FAILED",
"CANCELLED",
];
pub fn value_matches(expected: &Value, actual: &Value) -> bool {
match (expected, actual) {
(Value::Object(want), Value::Object(have)) => want
.iter()
.all(|(k, v)| value_matches(v, have.get(k).unwrap_or(&Value::Null))),
(Value::Array(want), Value::Array(have)) => {
want.len() == have.len() && want.iter().zip(have).all(|(w, h)| value_matches(w, h))
}
_ => crate::expr::values_equal(expected, actual),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn partial_matching() {
let actual = json!({ "a": 1, "b": { "c": "x", "d": [1, 2] } });
assert!(value_matches(&json!({}), &actual));
assert!(value_matches(&json!({ "a": 1.0 }), &actual));
assert!(value_matches(&json!({ "b": { "d": [1, 2] } }), &actual));
assert!(!value_matches(&json!({ "b": { "d": [1] } }), &actual));
let rows = json!([{ "q": "a", "n": 1 }, { "q": "b", "n": 2 }]);
assert!(value_matches(&json!([{ "q": "a" }, { "q": "b" }]), &rows));
assert!(!value_matches(&json!([{ "q": "a" }]), &rows));
assert!(!value_matches(&json!([{ "q": "b" }, { "q": "a" }]), &rows));
assert!(!value_matches(&json!({ "z": 1 }), &actual));
assert!(value_matches(&json!({ "z": null }), &actual));
}
}