From dcce50f151d5c333c45872b9c06e6e8bfb863555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20BR=C3=89ZOT?= Date: Mon, 7 Aug 2023 15:19:50 +0200 Subject: [PATCH] fix: add `test_keyword_presence()` test --- refactored/tests/test_in_memory.rs | 78 ++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/refactored/tests/test_in_memory.rs b/refactored/tests/test_in_memory.rs index 1a3f52ed..e48ef0e5 100644 --- a/refactored/tests/test_in_memory.rs +++ b/refactored/tests/test_in_memory.rs @@ -818,3 +818,81 @@ async fn test_graph_compacting() { check_search_result(&res, &rob_keyword, &robert_doe_location); } } + +#[actix_rt::test] +async fn test_keyword_presence() -> Result<(), Error> { + let mut indexed_value_to_keywords = HashMap::new(); + + // direct location robert doe + let robert_doe_location = Location::from("robert doe DB location"); + indexed_value_to_keywords.insert( + IndexedValue::Data(robert_doe_location.clone()), + hashset_keywords(&["robert", "doe"]), + ); + + // direct location john doe + let john_doe_location = Location::from("john doe DB location"); + indexed_value_to_keywords.insert( + IndexedValue::Data(john_doe_location.clone()), + hashset_keywords(&["john", "doe"]), + ); + + let mut findex = Findex::new( + EntryTable::setup(InMemoryEdx::setup()), + ChainTable::setup(InMemoryEdx::setup()), + ); + + let master_key = findex.keygen(); + let label = Label::from("First label."); + + let new_keywords = findex + .add(&master_key, &label, indexed_value_to_keywords) + .await?; + + // the 3 keywords should not be present in the database + assert_eq!(new_keywords.len(), 3); + assert!(new_keywords.contains(&Keyword::from("robert"))); + assert!(new_keywords.contains(&Keyword::from("doe"))); + assert!(new_keywords.contains(&Keyword::from("john"))); + + // Now insert a Robert Smith + let mut indexed_value_to_keywords = HashMap::new(); + let robert_smith_location = Location::from("robert smith DB location"); + indexed_value_to_keywords.insert( + IndexedValue::Data(robert_smith_location), + hashset_keywords(&["robert", "smith"]), + ); + let new_keywords = findex + .add(&master_key, &label, indexed_value_to_keywords) + .await?; + // robert should be present, but not smith + assert_eq!(new_keywords.len(), 1); + assert!(!new_keywords.contains(&Keyword::from("robert"))); + assert!(new_keywords.contains(&Keyword::from("smith"))); + + // Delete Robert Smith and the junior keyword + let robert_smith_location = Location::from("robert smith DB location"); + let mut indexed_value_to_keywords = HashMap::new(); + indexed_value_to_keywords.insert( + IndexedValue::Data(robert_smith_location.clone()), + hashset_keywords(&["robert", "smith", "junior"]), + ); + let new_keywords = findex + .delete(&master_key, &label, indexed_value_to_keywords.clone()) + .await?; + // robert and smith should be present, but not junior + assert_eq!(new_keywords.len(), 1); + assert!(!new_keywords.contains(&Keyword::from("robert"))); + assert!(!new_keywords.contains(&Keyword::from("smith"))); + assert!(new_keywords.contains(&Keyword::from("junior"))); + + // however, the first delete create an entry for "junior, + // therefore deleting again will find it + let new_keywords = findex + .delete(&master_key, &label, indexed_value_to_keywords.clone()) + .await?; + // all should be present + assert_eq!(new_keywords.len(), 0); + + Ok(()) +}