Switch to snapshot tests for search/criteria/typo.rs

This commit is contained in:
Loïc Lecrenier 2022-08-04 11:18:32 +02:00
parent a9c7d82693
commit d2e01528a6
2 changed files with 59 additions and 152 deletions

View File

@ -349,22 +349,33 @@ mod test {
use super::super::test::TestContext; use super::super::test::TestContext;
use super::*; use super::*;
fn display_criteria(mut criteria: Typo, mut parameters: CriterionParameters) -> String {
let mut result = String::new();
while let Some(criterion) = criteria.next(&mut parameters).unwrap() {
result.push_str(&format!("{criterion:?}\n\n"));
}
result
}
#[test] #[test]
fn initial_placeholder_no_facets() { fn initial_placeholder_no_facets() {
let context = TestContext::default(); let context = TestContext::default();
let query_tree = None; let query_tree = None;
let facet_candidates = None; let facet_candidates = None;
let mut criterion_parameters = CriterionParameters { let criterion_parameters = CriterionParameters {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(query_tree, facet_candidates); let parent = Initial::new(query_tree, facet_candidates);
let mut criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let result = display_criteria(criteria, criterion_parameters);
insta::assert_snapshot!(result, @r###"
CriterionResult { query_tree: None, candidates: None, filtered_candidates: None, bucket_candidates: None }
assert!(criteria.next(&mut criterion_parameters).unwrap().unwrap().candidates.is_none()); "###);
assert!(criteria.next(&mut criterion_parameters).unwrap().is_none());
} }
#[test] #[test]
@ -390,78 +401,32 @@ mod test {
let facet_candidates = None; let facet_candidates = None;
let mut criterion_parameters = CriterionParameters { let criterion_parameters = CriterionParameters {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(Some(query_tree), facet_candidates); let parent = Initial::new(Some(query_tree), facet_candidates);
let mut criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let candidates_1 = context.word_docids("split").unwrap().unwrap() let result = display_criteria(criteria, criterion_parameters);
& context.word_docids("this").unwrap().unwrap() insta::assert_snapshot!(result, @r###"
& context.word_docids("world").unwrap().unwrap(); CriterionResult { query_tree: Some(OR
let expected_1 = CriterionResult { AND
query_tree: Some(Operation::Or( Exact { word: "split" }
false, Exact { word: "this" }
vec![Operation::And(vec![ Exact { word: "world" }
Operation::Query(Query { ), candidates: Some(RoaringBitmap<[]>), filtered_candidates: None, bucket_candidates: Some(RoaringBitmap<[]>) }
prefix: false,
kind: QueryKind::exact("split".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("this".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("world".to_string()),
}),
])],
)),
candidates: Some(candidates_1.clone()),
bucket_candidates: Some(candidates_1),
filtered_candidates: None,
};
assert_eq!(criteria.next(&mut criterion_parameters).unwrap(), Some(expected_1)); CriterionResult { query_tree: Some(OR
AND
Exact { word: "split" }
Exact { word: "this" }
OR
Exact { word: "word" }
Exact { word: "world" }
), candidates: Some(RoaringBitmap<[]>), filtered_candidates: None, bucket_candidates: Some(RoaringBitmap<[]>) }
let candidates_2 = (context.word_docids("split").unwrap().unwrap() "###);
& context.word_docids("this").unwrap().unwrap()
& context.word_docids("word").unwrap().unwrap())
- context.word_docids("world").unwrap().unwrap();
let expected_2 = CriterionResult {
query_tree: Some(Operation::Or(
false,
vec![Operation::And(vec![
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("split".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("this".to_string()),
}),
Operation::Or(
false,
vec![
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact_with_typo(1, "word".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("world".to_string()),
}),
],
),
])],
)),
candidates: Some(candidates_2.clone()),
bucket_candidates: Some(candidates_2),
filtered_candidates: None,
};
assert_eq!(criteria.next(&mut criterion_parameters).unwrap(), Some(expected_2));
} }
#[test] #[test]
@ -470,25 +435,18 @@ mod test {
let query_tree = None; let query_tree = None;
let facet_candidates = context.word_docids("earth").unwrap().unwrap(); let facet_candidates = context.word_docids("earth").unwrap().unwrap();
let mut criterion_parameters = CriterionParameters { let criterion_parameters = CriterionParameters {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(query_tree, Some(facet_candidates.clone())); let parent = Initial::new(query_tree, Some(facet_candidates.clone()));
let mut criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let expected = CriterionResult { let result = display_criteria(criteria, criterion_parameters);
query_tree: None, insta::assert_snapshot!(result, @r###"
candidates: None, CriterionResult { query_tree: None, candidates: None, filtered_candidates: Some(RoaringBitmap<8000 values between 986424 and 4294786076>), bucket_candidates: None }
bucket_candidates: None,
filtered_candidates: Some(facet_candidates.clone()),
};
// first iteration, returns the facet candidates "###);
assert_eq!(criteria.next(&mut criterion_parameters).unwrap(), Some(expected));
// second iteration, returns None because there is no more things to do
assert!(criteria.next(&mut criterion_parameters).unwrap().is_none());
} }
#[test] #[test]
@ -514,77 +472,31 @@ mod test {
let facet_candidates = context.word_docids("earth").unwrap().unwrap(); let facet_candidates = context.word_docids("earth").unwrap().unwrap();
let mut criterion_parameters = CriterionParameters { let criterion_parameters = CriterionParameters {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(Some(query_tree), Some(facet_candidates.clone())); let parent = Initial::new(Some(query_tree), Some(facet_candidates.clone()));
let mut criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let candidates_1 = context.word_docids("split").unwrap().unwrap() let result = display_criteria(criteria, criterion_parameters);
& context.word_docids("this").unwrap().unwrap() insta::assert_snapshot!(result, @r###"
& context.word_docids("world").unwrap().unwrap(); CriterionResult { query_tree: Some(OR
let expected_1 = CriterionResult { AND
query_tree: Some(Operation::Or( Exact { word: "split" }
false, Exact { word: "this" }
vec![Operation::And(vec![ Exact { word: "world" }
Operation::Query(Query { ), candidates: Some(RoaringBitmap<[]>), filtered_candidates: None, bucket_candidates: Some(RoaringBitmap<[]>) }
prefix: false,
kind: QueryKind::exact("split".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("this".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("world".to_string()),
}),
])],
)),
candidates: Some(&candidates_1 & &facet_candidates),
bucket_candidates: Some(&candidates_1 & &facet_candidates),
filtered_candidates: None,
};
assert_eq!(criteria.next(&mut criterion_parameters).unwrap(), Some(expected_1)); CriterionResult { query_tree: Some(OR
AND
Exact { word: "split" }
Exact { word: "this" }
OR
Exact { word: "word" }
Exact { word: "world" }
), candidates: Some(RoaringBitmap<[]>), filtered_candidates: None, bucket_candidates: Some(RoaringBitmap<[]>) }
let candidates_2 = (context.word_docids("split").unwrap().unwrap() "###);
& context.word_docids("this").unwrap().unwrap()
& context.word_docids("word").unwrap().unwrap())
- context.word_docids("world").unwrap().unwrap();
let expected_2 = CriterionResult {
query_tree: Some(Operation::Or(
false,
vec![Operation::And(vec![
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("split".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("this".to_string()),
}),
Operation::Or(
false,
vec![
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact_with_typo(1, "word".to_string()),
}),
Operation::Query(Query {
prefix: false,
kind: QueryKind::exact("world".to_string()),
}),
],
),
])],
)),
candidates: Some(&candidates_2 & &facet_candidates),
bucket_candidates: Some(&candidates_2 & &facet_candidates),
filtered_candidates: None,
};
assert_eq!(criteria.next(&mut criterion_parameters).unwrap(), Some(expected_2));
} }
} }

View File

@ -99,11 +99,6 @@ impl QueryKind {
QueryKind::Exact { original_typo: 0, word } QueryKind::Exact { original_typo: 0, word }
} }
#[cfg(test)]
pub fn exact_with_typo(original_typo: u8, word: String) -> Self {
QueryKind::Exact { original_typo, word }
}
pub fn tolerant(typo: u8, word: String) -> Self { pub fn tolerant(typo: u8, word: String) -> Self {
QueryKind::Tolerant { typo, word } QueryKind::Tolerant { typo, word }
} }