Finished moving tests into lib test.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
This commit is contained in:
160
tests/trigger_test.rs
Normal file
160
tests/trigger_test.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
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 mtt = MoreThanText::new();
|
||||
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 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 second_qry = mtt.records(Query::new(test_doc.get_doc_name())).unwrap();
|
||||
assert_eq!(second_qry.len(), 1);
|
||||
let second_rec = second_qry.iter().last().unwrap();
|
||||
assert_eq!(
|
||||
second_rec.get(test_doc.get_field_name(0)).unwrap(),
|
||||
data0.clone().into()
|
||||
);
|
||||
assert_eq!(
|
||||
second_rec.get(test_doc.get_field_name(1)).unwrap(),
|
||||
data1_expected.clone().into(),
|
||||
"not updated"
|
||||
);
|
||||
}
|
||||
|
||||
#[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 mtt = MoreThanText::new();
|
||||
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 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 second_result = mtt.records(Query::new(test_doc.get_doc_name())).unwrap();
|
||||
assert_eq!(second_result.len(), count.try_into().unwrap());
|
||||
for rec in second_result.iter() {
|
||||
if 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()
|
||||
);
|
||||
} else {
|
||||
assert_eq!(
|
||||
rec.get(test_doc.get_field_name(1)).unwrap(),
|
||||
initial_data.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 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);
|
||||
test_env.send_time_pulse();
|
||||
let result = mtt.records(Query::new(test_doc.get_doc_name())).unwrap();
|
||||
assert_eq!(
|
||||
result.len(),
|
||||
(count - 1).try_into().unwrap(),
|
||||
"wrong number of records"
|
||||
);
|
||||
for rec in result.iter() {
|
||||
assert!(
|
||||
rec.get(test_doc.get_field_name(0)).unwrap() != selected.clone().into(),
|
||||
"did not remove selected"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user