add authorize_typo_test

This commit is contained in:
ad hoc 2022-03-31 09:54:49 +02:00
parent c4653347fd
commit f782fe2062
No known key found for this signature in database
GPG Key ID: 4F00A782990CC643
2 changed files with 53 additions and 3 deletions

View File

@ -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());
}
} }

View File

@ -105,6 +105,12 @@ impl<'a> Search<'a> {
self self
} }
fn is_typo_authorized(&self) -> Result<bool> {
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<SearchResult> { pub fn execute(&self) -> Result<SearchResult> {
// We create the query tree by spliting the query into tokens. // We create the query tree by spliting the query into tokens.
let before = Instant::now(); let before = Instant::now();
@ -113,9 +119,7 @@ impl<'a> Search<'a> {
let mut builder = QueryTreeBuilder::new(self.rtxn, self.index); let mut builder = QueryTreeBuilder::new(self.rtxn, self.index);
builder.optional_words(self.optional_words); builder.optional_words(self.optional_words);
// only authorize typos if both the index and the query allow it. builder.authorize_typos(self.is_typo_authorized()?);
let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?;
builder.authorize_typos(self.authorize_typos && index_authorizes_typos);
builder.words_limit(self.words_limit); builder.words_limit(self.words_limit);
// We make sure that the analyzer is aware of the stop words // 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) 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());
}
}