mod support; use morethantext::{Field, FieldType, MoreThanText, Name, Query}; use support::TestDocument; #[test] fn are_document_names_unique() { let test_doc1 = TestDocument::new(Vec::new()); let test_doc2 = TestDocument::new(Vec::new()); assert_ne!(test_doc1.get_doc_name(), test_doc2.get_doc_name()); } #[test] fn does_test_doc_create_docdef() { let test_doc = TestDocument::new(Vec::new()); let expected = vec![test_doc.get_doc_name().clone()]; let docdef = test_doc.get_docdef(); assert_eq!(docdef.get_document_names(), &expected); } #[test] fn can_fields_be_created() { let count = 5; let mut expected_fields = Vec::new(); for i in 0..count { let name = Name::english(format!("field{}", i).as_str()); expected_fields.push(name); } let ftypes = [FieldType::Uuid, FieldType::Integer]; for ftype in ftypes.iter() { let mut input = Vec::new(); for _ in 0..count { input.push(ftype.clone()); } let test_doc = TestDocument::new(input); for i in 0..count { assert_eq!(test_doc.get_field_name(i), expected_fields[i]); } let docdef = test_doc.get_docdef(); for name in expected_fields.iter() { docdef.validate(name, &ftype.get_default()).unwrap(); } } } #[test] fn can_field_types_be_varied() { let field_types = vec![FieldType::Uuid, FieldType::Integer]; let test_doc = TestDocument::new(field_types.clone()); let docdef = test_doc.get_docdef(); for i in 0..field_types.len() { docdef .validate(test_doc.get_field_name(i), &field_types[i].get_default()) .unwrap(); } } #[test] fn can_record_be_added_to_document() { let count = 5; let mut mtt = MoreThanText::new(); let mut fields: Vec = Vec::new(); let mut input: Vec = Vec::new(); for i in 0..count { fields.push(FieldType::StaticString); input.push(i.to_string()); } let test_doc = TestDocument::new(fields); mtt.create_document(test_doc.get_docdef()); test_doc.populate(mtt.clone(), input); let query = Query::new(test_doc.get_doc_name()); let results = mtt.records(query).unwrap(); assert_eq!(results.len(), 1); for rec in results.iter() { for i in 0..count { let field = rec.get(test_doc.get_field_name(i)).unwrap(); assert_eq!(field, i.to_string().into()); } } } #[test] fn can_document_be_populated_with_multiple_records() { let count = 5; let mut mtt = MoreThanText::new(); let test_doc = TestDocument::new(vec![FieldType::Integer]); mtt.create_document(test_doc.get_docdef()); let mut data: Vec> = Vec::new(); for i in 0..count { let item: i128 = i.try_into().unwrap(); let holder: Vec = vec![item]; data.push(holder); } test_doc.populate_multiple(mtt.clone(), data); let query = Query::new(test_doc.get_doc_name()); let results = mtt.records(query).unwrap(); assert_eq!(results.len(), count); }