Files
morethantext/tests/trigger_test.rs
Jeff Baskin b96bbbe21d
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Corrected trigger tests.
2026-03-08 17:05:47 -04:00

171 lines
6.0 KiB
Rust

mod support;
use morethantext::{
Action, CalcValue, Calculation, Delete, DocFuncType, FieldType, Include, MoreThanText, Name,
Operand, Path, Query, TestMoreThanText, Update,
};
use support::TestDocument;
#[test]
fn can_a_trigger_cause_an_update() {
let data0 = 0;
let data1 = 0;
let data1_expected = 1;
let mut test_env = TestMoreThanText::new();
let mut mtt = test_env.get_morethantext();
let test_doc = TestDocument::new(vec![FieldType::Integer, FieldType::Integer]);
let mut calc = Calculation::new(Operand::Add);
calc.add_value(CalcValue::Existing(FieldType::Integer))
.unwrap();
calc.add_value(1).unwrap();
let mut update = Update::new(test_doc.get_doc_name());
update.add_field(test_doc.get_field_name(1), calc);
let path = Path::new(
Include::All,
Include::Just(test_doc.get_doc_name().into()),
Include::Just(Action::OnQuery),
);
let function = DocFuncType::ExistingQuery(update.into());
let mut docdef = test_doc.get_docdef();
docdef.add_route(path, function);
mtt.create_document(docdef);
test_doc.populate(&mut mtt, vec![data0.clone(), data1.clone()]);
let path = Path::new(
Include::All,
Include::Just(test_doc.get_doc_name().into()),
Include::Just(Action::OnUpdate),
);
test_env.register_channel(vec![path]);
let first_qry = mtt.records(Query::new(test_doc.get_doc_name())).unwrap();
assert_eq!(first_qry.len(), 1);
let first_rec = first_qry.iter().last().unwrap();
assert_eq!(
first_rec.get(test_doc.get_field_name(0)).unwrap(),
data0.clone().into()
);
assert_eq!(
first_rec.get(test_doc.get_field_name(1)).unwrap(),
data1.clone().into()
);
let result = test_env.get_trigger_records(Action::OnUpdate);
assert_eq!(result.len(), 1);
let rec = result.iter().last().unwrap();
assert_eq!(
rec.get(test_doc.get_field_name(0)).unwrap(),
data0.clone().into()
);
assert_eq!(
rec.get(test_doc.get_field_name(1)).unwrap(),
data1_expected.clone().into()
);
}
#[test]
fn can_trigger_update_specific_record() {
let count = 3;
let selected = 1; // must be greater than or equal to 0 and less than count
let initial_data = 0;
let expected = 1;
let mut input: Vec<Vec<i128>> = Vec::new();
for i in 0..count {
input.push(vec![i.clone(), initial_data.clone()]);
}
let mut test_env = TestMoreThanText::new();
let mut mtt = test_env.get_morethantext();
let test_doc = TestDocument::new(vec![FieldType::Integer, FieldType::Integer]);
let mut calc = Calculation::new(Operand::Add);
calc.add_value(CalcValue::Existing(FieldType::Integer))
.unwrap();
calc.add_value(1).unwrap();
let mut update = Update::new(test_doc.get_doc_name());
update.add_field(test_doc.get_field_name(1), calc);
let path = Path::new(
Include::All,
Include::Just(test_doc.get_doc_name().into()),
Include::Just(Action::OnQuery),
);
let function = DocFuncType::ExistingQuery(update.into());
let mut docdef = test_doc.get_docdef();
docdef.add_route(path, function);
mtt.create_document(docdef);
test_doc.populate_multiple(&mut mtt, input);
let mut qry_calc = Calculation::new(Operand::Equal);
qry_calc
.add_value(CalcValue::Existing(FieldType::Integer))
.unwrap();
qry_calc.add_value(selected.clone()).unwrap();
let mut qry = Query::new(test_doc.get_doc_name());
qry.add(test_doc.get_field_name(0), qry_calc);
let path = Path::new(
Include::All,
Include::Just(test_doc.get_doc_name().into()),
Include::Just(Action::OnUpdate),
);
test_env.register_channel(vec![path]);
let first_result = mtt.records(qry).unwrap();
assert_eq!(first_result.len(), 1);
let first_rec = first_result.iter().last().unwrap();
assert_eq!(
first_rec.get(test_doc.get_field_name(0)).unwrap(),
selected.clone().into()
);
assert_eq!(
first_rec.get(test_doc.get_field_name(1)).unwrap(),
initial_data.clone().into()
);
let result = test_env.get_trigger_records(Action::OnUpdate);
assert_eq!(result.len(), 1);
let rec = result.iter().last().unwrap();
assert_eq!(
rec.get(test_doc.get_field_name(0)).unwrap(),
selected.clone().into()
);
assert_eq!(
rec.get(test_doc.get_field_name(1)).unwrap(),
expected.clone().into()
);
}
#[test]
fn can_a_trigger_from_another_document_be_used() {
let count = 3;
let selected = 1; // must be greater than or equal to 0 and less than count
let mut input: Vec<Vec<i128>> = Vec::new();
for i in 0..count {
input.push(vec![i]);
}
let mut test_env = TestMoreThanText::new();
let mut mtt = test_env.get_morethantext();
let test_doc = TestDocument::new(vec![FieldType::Integer]);
let mut calc = Calculation::new(Operand::Equal);
calc.add_value(CalcValue::Existing(FieldType::Integer))
.unwrap();
calc.add_value(1).unwrap();
let mut delete = Delete::new(test_doc.get_doc_name());
delete.get_query_mut().add(test_doc.get_field_name(0), calc);
let path = Path::new(
Include::All,
Include::Just(Name::english("clock").into()),
Include::Just(Action::OnUpdate),
);
let function = DocFuncType::Trigger(delete.into());
let mut docdef = test_doc.get_docdef();
docdef.add_route(path, function);
mtt.create_document(docdef);
test_doc.populate_multiple(&mut mtt, input);
let path = Path::new(
Include::All,
Include::Just(test_doc.get_doc_name().into()),
Include::Just(Action::OnDelete),
);
test_env.register_channel(vec![path]);
test_env.send_time_pulse();
let result = test_env.get_trigger_records(Action::OnDelete);
assert_eq!(result.len(), 1);
let rec = result.iter().last().unwrap();
assert_eq!(
rec.get(test_doc.get_field_name(0)).unwrap(),
selected.into()
);
}