pub const COMPONENT: &str = "knut-bifrost";
#[cfg(feature = "testmatrix")]
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
nornir_testmatrix::functional_status(component, check, ok, detail);
}
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
pub fn self_test() -> bool {
use crate::transform::{TableContext, Transform, VertexTransform};
use arrow_array::{Int64Array, RecordBatch, StringArray};
use arrow_schema::{DataType, Field, Schema};
use std::sync::Arc;
let schema = Arc::new(Schema::new(vec![
Field::new("user_id", DataType::Int64, false),
Field::new("name", DataType::Utf8, false),
]));
let batch = RecordBatch::try_new(
schema,
vec![
Arc::new(Int64Array::from(vec![1_i64, 2])),
Arc::new(StringArray::from(vec!["alice", "bob"])),
],
)
.expect("self_test batch");
let t = VertexTransform {
label: "Person".into(),
id_column: "user_id".into(),
properties: vec!["user_id".into(), "name".into()],
rename: Default::default(),
};
let ctx = TableContext {
table: "users".into(),
};
let mut all_ok = true;
match t.apply(&ctx, &[batch]) {
Ok(m) => {
let one_vertex_set = m.vertices.len() == 1;
emit(
COMPONENT,
"self_test_vertex_set_built",
one_vertex_set,
&format!("{} vertex set(s)", m.vertices.len()),
);
all_ok &= one_vertex_set;
if let Some(set) = m.vertices.first() {
let label_ok = set.label == "Person";
emit(
COMPONENT,
"self_test_label_mapped",
label_ok,
&format!("label={}", set.label),
);
all_ok &= label_ok;
let rows = set.batch.num_rows();
let rows_ok = rows == 2;
emit(
COMPONENT,
"self_test_row_count",
rows_ok,
&format!("{rows} vertex rows"),
);
all_ok &= rows_ok;
} else {
all_ok = false;
}
}
Err(e) => {
emit(
COMPONENT,
"self_test_vertex_set_built",
false,
&format!("transform errored: {e}"),
);
all_ok = false;
}
}
emit(COMPONENT, "self_test", all_ok, "core transform path");
all_ok
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn self_test_passes_and_is_idempotent() {
assert!(self_test(), "core transform self-test must pass");
assert!(self_test(), "self-test is idempotent");
emit(
COMPONENT,
"self_test_test_ran",
true,
"self_test() returned true twice",
);
}
}