From f782fe20625755e78d89b630ccf87fbee9bb8b59 Mon Sep 17 00:00:00 2001 From: ad hoc Date: Thu, 31 Mar 2022 09:54:49 +0200 Subject: [PATCH] add authorize_typo_test --- milli/src/index.rs | 14 ++++++++++++++ milli/src/search/mod.rs | 42 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/milli/src/index.rs b/milli/src/index.rs index 4e43e404e..badcac0e5 100644 --- a/milli/src/index.rs +++ b/milli/src/index.rs @@ -1009,4 +1009,18 @@ pub(crate) mod tests { } ); } + + #[test] + fn put_and_retrieve_disable_typo() { + let index = TempIndex::new(); + let mut txn = index.write_txn().unwrap(); + // default value is true + assert!(index.authorize_typos(&txn).unwrap()); + // set to false + index.put_authorize_typos(&mut txn, false).unwrap(); + txn.commit().unwrap(); + + let txn = index.read_txn().unwrap(); + assert!(!index.authorize_typos(&txn).unwrap()); + } } diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index c9eef5a0d..4f753a607 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -105,6 +105,12 @@ impl<'a> Search<'a> { self } + fn is_typo_authorized(&self) -> Result { + let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?; + // only authorize typos if both the index and the query allow it. + Ok(self.authorize_typos && index_authorizes_typos) + } + pub fn execute(&self) -> Result { // We create the query tree by spliting the query into tokens. let before = Instant::now(); @@ -113,9 +119,7 @@ impl<'a> Search<'a> { let mut builder = QueryTreeBuilder::new(self.rtxn, self.index); builder.optional_words(self.optional_words); - // only authorize typos if both the index and the query allow it. - let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?; - builder.authorize_typos(self.authorize_typos && index_authorizes_typos); + builder.authorize_typos(self.is_typo_authorized()?); builder.words_limit(self.words_limit); // We make sure that the analyzer is aware of the stop words @@ -364,3 +368,35 @@ pub fn build_dfa(word: &str, typos: u8, is_prefix: bool) -> DFA { lev.build_dfa(word) } } + +#[cfg(test)] +mod test { + use crate::index::tests::TempIndex; + + use super::*; + + #[test] + fn test_is_authorized_typos() { + let index = TempIndex::new(); + let mut txn = index.write_txn().unwrap(); + + let mut search = Search::new(&txn, &index); + + // default is authorized + assert!(search.is_typo_authorized().unwrap()); + + search.authorize_typos(false); + assert!(!search.is_typo_authorized().unwrap()); + + index.put_authorize_typos(&mut txn, false).unwrap(); + txn.commit().unwrap(); + + let txn = index.read_txn().unwrap(); + let mut search = Search::new(&txn, &index); + + assert!(!search.is_typo_authorized().unwrap()); + + search.authorize_typos(true); + assert!(!search.is_typo_authorized().unwrap()); + } +}