2026-03-06 10:41:10 -05:00
|
|
|
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;
|
2026-03-08 17:05:47 -04:00
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mut mtt = test_env.get_morethantext();
|
2026-03-06 10:41:10 -05:00
|
|
|
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()]);
|
2026-03-08 17:05:47 -04:00
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(test_doc.get_doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnUpdate),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
2026-03-06 10:41:10 -05:00
|
|
|
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()
|
|
|
|
|
);
|
2026-03-08 17:05:47 -04:00
|
|
|
let result = test_env.get_trigger_records(Action::OnUpdate);
|
|
|
|
|
assert_eq!(result.len(), 1);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
2026-03-06 10:41:10 -05:00
|
|
|
assert_eq!(
|
2026-03-08 17:05:47 -04:00
|
|
|
rec.get(test_doc.get_field_name(0)).unwrap(),
|
2026-03-06 10:41:10 -05:00
|
|
|
data0.clone().into()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2026-03-08 17:05:47 -04:00
|
|
|
rec.get(test_doc.get_field_name(1)).unwrap(),
|
|
|
|
|
data1_expected.clone().into()
|
2026-03-06 10:41:10 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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()]);
|
|
|
|
|
}
|
2026-03-08 17:05:47 -04:00
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mut mtt = test_env.get_morethantext();
|
2026-03-06 10:41:10 -05:00
|
|
|
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);
|
2026-03-08 17:05:47 -04:00
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(test_doc.get_doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnUpdate),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
2026-03-06 10:41:10 -05:00
|
|
|
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()
|
|
|
|
|
);
|
2026-03-08 17:05:47 -04:00
|
|
|
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()
|
|
|
|
|
);
|
2026-03-06 10:41:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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]);
|
|
|
|
|
}
|
2026-03-08 17:05:47 -04:00
|
|
|
let mut test_env = TestMoreThanText::new();
|
2026-03-06 10:41:10 -05:00
|
|
|
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);
|
2026-03-08 17:05:47 -04:00
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(test_doc.get_doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnDelete),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
2026-03-06 10:41:10 -05:00
|
|
|
test_env.send_time_pulse();
|
2026-03-08 17:05:47 -04:00
|
|
|
let result = test_env.get_trigger_records(Action::OnDelete);
|
|
|
|
|
assert_eq!(result.len(), 1);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
2026-03-06 10:41:10 -05:00
|
|
|
assert_eq!(
|
2026-03-08 17:05:47 -04:00
|
|
|
rec.get(test_doc.get_field_name(0)).unwrap(),
|
|
|
|
|
selected.into()
|
2026-03-06 10:41:10 -05:00
|
|
|
);
|
|
|
|
|
}
|