diff --git a/src/document/create.rs b/src/document/create.rs index cb3f013..6dd0025 100644 --- a/src/document/create.rs +++ b/src/document/create.rs @@ -2045,7 +2045,6 @@ mod document_files { } assert!(ids.is_empty(), "did not find {:?}", ids); } - */ #[test] fn can_calculate_field_values() { @@ -2076,6 +2075,7 @@ mod document_files { _ => unreachable!("got {:?}: should have gotten reply", action), } } + */ #[test] fn can_delete() { diff --git a/tests/add_test.rs b/tests/add_test.rs index e4da467..14ca37f 100644 --- a/tests/add_test.rs +++ b/tests/add_test.rs @@ -1,10 +1,11 @@ mod support; +use chrono::Utc; use morethantext::{ action::{Addition, DocDef, Field, FieldType, Query}, - ErrorID, MTTError, MoreThanText, Name, + Calculation, ErrorID, IndexType, MTTError, MoreThanText, Name, Operand, }; -use std::collections::HashSet; +use std::{collections::HashSet, time::Duration}; use support::{random_name, TestDocument}; use uuid::Uuid; @@ -146,3 +147,42 @@ fn can_default_values_be_overwritten() { let rec = results.iter().last().unwrap(); assert_eq!(rec.get(test_doc.get_field_name(0)).unwrap(), used.into()); } + +#[test] +fn can_default_values_be_calculated() { + let duration = Duration::from_secs(300); + let mut mtt = MoreThanText::new(); + let test_doc = TestDocument::new(vec![FieldType::DateTime]); + let mut docdef = test_doc.get_docdef(); + let mut calc = Calculation::new(Operand::Add); + calc.add_value(FieldType::DateTime); + calc.add_value(duration.clone()); + docdef.set_default(&test_doc.get_field_name(0), calc); + mtt.create_document(docdef).unwrap(); + let add = Addition::new(test_doc.get_doc_name()); + let start = Utc::now() + duration; + let results = mtt.records(add).unwrap(); + let end = Utc::now() + duration; + let rec = results.iter().last().unwrap(); + let field = rec.get(&test_doc.get_field_name(0)).unwrap(); + assert!(field > start.into()); + assert!(field < end.into()); +} + +#[test] +fn are_unique_indexes_maintained_with_additions() { + let data = 1; + let mut mtt = MoreThanText::new(); + let test_doc = TestDocument::new(vec![FieldType::Integer]); + let mut docdef = test_doc.get_docdef(); + docdef.add_index(&test_doc.get_field_name(0), IndexType::Unique); + mtt.create_document(docdef); + let mut add = Addition::new(test_doc.get_doc_name()); + add.add_field(test_doc.get_field_name(0), data.clone()); + mtt.records(add.clone()).unwrap(); + let mut err = MTTError::new(ErrorID::IndexEntryAlreadyExists(data.into())); + err.add_parent(ErrorID::Field(test_doc.get_field_name(0).into())); + err.add_parent(ErrorID::Document(test_doc.get_doc_name().into())); + let result = mtt.records(add).unwrap_err(); + assert_eq!(result.to_string(), err.to_string()); +}