Fixing the tests

This commit is contained in:
Francesco Allara 2024-02-13 17:50:40 +01:00
parent 206e88449b
commit c9708f7d0c
5 changed files with 52 additions and 42 deletions

View File

@ -452,7 +452,7 @@ impl std::fmt::Display for FaultSource {
#[test]
fn conditionally_lookup_for_error_message() {
let prefix = "Attribute `name` is not sortable.";
let prefix = "Attribute `name` of index `index` is not sortable.";
let messages = vec![
(BTreeSet::new(), "This index does not have configured sortable attributes."),
(BTreeSet::from(["age".to_string()]), "Available sortable attributes are: `age`."),
@ -461,6 +461,7 @@ fn conditionally_lookup_for_error_message() {
for (list, suffix) in messages {
let err = UserError::InvalidSortableAttribute {
field: "name".to_string(),
index: "index".to_string(),
valid_fields: list,
hidden_fields: false,
};

View File

@ -1864,7 +1864,7 @@ pub(crate) mod tests {
// ensure we get the right real searchable fields + user defined searchable fields
let rtxn = index.read_txn().unwrap();
let mut search = index.search(&rtxn);
let mut search = index.search(&rtxn, &index.to_string());
// exact match a document
let search_result = search

View File

@ -70,7 +70,7 @@ impl<'a> Search<'a> {
searchable_attributes: None,
geo_strategy: new::GeoSortStrategy::default(),
terms_matching_strategy: TermsMatchingStrategy::default(),
scoring_strategy: Default::default(),
scori ng_strategy: Default::default(),
exhaustive_number_hits: false,
words_limit: 10,
rtxn,

View File

@ -1033,15 +1033,15 @@ mod tests {
let rtxn = index.read_txn().unwrap();
// Search for a sub object value
let result = index.search(&rtxn).query(r#""value2""#).execute().unwrap();
let result = index.search(&rtxn, &String::from("test_index")).query(r#""value2""#).execute().unwrap();
assert_eq!(result.documents_ids, vec![0]);
// Search for a sub array value
let result = index.search(&rtxn).query(r#""fine""#).execute().unwrap();
let result = index.search(&rtxn, &String::from("test_index")).query(r#""fine""#).execute().unwrap();
assert_eq!(result.documents_ids, vec![1]);
// Search for a sub array sub object key
let result = index.search(&rtxn).query(r#""amazing""#).execute().unwrap();
let result = index.search(&rtxn, &String::from("test_index")).query(r#""amazing""#).execute().unwrap();
assert_eq!(result.documents_ids, vec![2]);
drop(rtxn);
@ -1348,7 +1348,7 @@ mod tests {
assert_eq!(facets, hashset!(S("title"), S("nested.object"), S("nested.machin")));
// testing the simple query search
let mut search = crate::Search::new(&rtxn, &index);
let mut search = crate::Search::new(&rtxn, &index, &String::from("test_index"));
search.query("document");
search.terms_matching_strategy(TermsMatchingStrategy::default());
// all documents should be returned
@ -1389,7 +1389,7 @@ mod tests {
assert!(documents_ids.is_empty()); // nested is not searchable
// testing the filters
let mut search = crate::Search::new(&rtxn, &index);
let mut search = crate::Search::new(&rtxn, &index, &String::from("test_index"));
search.filter(crate::Filter::from_str(r#"title = "The first document""#).unwrap().unwrap());
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
assert_eq!(documents_ids, vec![1]);
@ -1453,7 +1453,7 @@ mod tests {
let rtxn = index.read_txn().unwrap();
// testing the simple query search
let mut search = crate::Search::new(&rtxn, &index);
let mut search = crate::Search::new(&rtxn, &index, &String::from("test_index"));
search.query("document");
search.terms_matching_strategy(TermsMatchingStrategy::default());
// all documents should be returned
@ -1565,7 +1565,7 @@ mod tests {
assert_eq!(hidden, hashset!(S("dog"), S("dog.race"), S("dog.race.bernese mountain")));
for (s, i) in [("zeroth", 0), ("first", 1), ("second", 2), ("third", 3)] {
let mut search = crate::Search::new(&rtxn, &index);
let mut search = crate::Search::new(&rtxn, &index, &String::from("test_index"));
let filter = format!(r#""dog.race.bernese mountain" = {s}"#);
search.filter(crate::Filter::from_str(&filter).unwrap().unwrap());
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
@ -1613,7 +1613,7 @@ mod tests {
assert_eq!(facets, hashset!(S("dog.race"), S("dog.race.bernese mountain")));
let mut search = crate::Search::new(&rtxn, &index);
let mut search = crate::Search::new(&rtxn, &index, &String::from("test_index"));
search.sort_criteria(vec![crate::AscDesc::Asc(crate::Member::Field(S(
"dog.race.bernese mountain",
)))]);
@ -2610,7 +2610,7 @@ mod tests {
.unwrap();
let rtxn = index.read_txn().unwrap();
let res = index.search(&rtxn).vector([0.0, 1.0, 2.0].to_vec()).execute().unwrap();
let res = index.search(&rtxn, &String::from("test_index")).vector([0.0, 1.0, 2.0].to_vec()).execute().unwrap();
assert_eq!(res.documents_ids.len(), 3);
}
@ -2771,7 +2771,7 @@ mod tests {
// Ensuring all the returned IDs actually exists
let rtxn = index.read_txn().unwrap();
let res = index.search(&rtxn).execute().unwrap();
let res = index.search(&rtxn, &String::from("test_index")).execute().unwrap();
index.documents(&rtxn, res.documents_ids).unwrap();
}
@ -2908,7 +2908,7 @@ mod tests {
// Placeholder search with filter
let filter = Filter::from_str("label = sign").unwrap().unwrap();
let results = index.search(&wtxn).filter(filter).execute().unwrap();
let results = index.search(&wtxn, &String::from("test_index")).filter(filter).execute().unwrap();
assert!(results.documents_ids.is_empty());
wtxn.commit().unwrap();
@ -2965,7 +2965,7 @@ mod tests {
let deleted_internal_ids = delete_documents(&mut wtxn, &index, &["1_4"]);
// Placeholder search
let results = index.search(&wtxn).execute().unwrap();
let results = index.search(&wtxn, &String::from("test_index")).execute().unwrap();
assert!(!results.documents_ids.is_empty());
for id in results.documents_ids.iter() {
assert!(
@ -3023,7 +3023,7 @@ mod tests {
let deleted_internal_ids = delete_documents(&mut wtxn, &index, &["1_7", "1_52"]);
// search for abstract
let results = index.search(&wtxn).query("abstract").execute().unwrap();
let results = index.search(&wtxn, &String::from("test_index")).query("abstract").execute().unwrap();
assert!(!results.documents_ids.is_empty());
for id in results.documents_ids.iter() {
assert!(
@ -3075,9 +3075,10 @@ mod tests {
let external_ids_to_delete = ["5", "6", "7", "12", "17", "19"];
let deleted_internal_ids = delete_documents(&mut wtxn, &index, &external_ids_to_delete);
let index_uid = String::from("test_index");
// Placeholder search with geo filter
let filter = Filter::from_str("_geoRadius(50.6924, 3.1763, 20000)").unwrap().unwrap();
let results = index.search(&wtxn).filter(filter).execute().unwrap();
let results = index.search(&wtxn, &index_uid).filter(filter).execute().unwrap();
assert!(!results.documents_ids.is_empty());
for id in results.documents_ids.iter() {
assert!(
@ -3302,7 +3303,8 @@ mod tests {
let words = index.words_fst(&txn).unwrap().into_stream().into_strs().unwrap();
insta::assert_snapshot!(format!("{words:?}"), @r###"["hello"]"###);
let mut s = Search::new(&txn, &index);
let index_uid = String::from("test_index");
let mut s = Search::new(&txn, &index, &index_uid);
s.query("hello");
let crate::SearchResult { documents_ids, .. } = s.execute().unwrap();
insta::assert_snapshot!(format!("{documents_ids:?}"), @"[0]");

View File

@ -1215,6 +1215,7 @@ mod tests {
#[test]
fn set_and_reset_searchable_fields() {
let index = TempIndex::new();
let index_uid = String::from("test_index");
// First we send 3 documents with ids from 1 to 3.
let mut wtxn = index.write_txn().unwrap();
@ -1243,12 +1244,12 @@ mod tests {
let rtxn = index.read_txn().unwrap();
// When we search for something that is not in
// the searchable fields it must not return any document.
let result = index.search(&rtxn).query("23").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("23").execute().unwrap();
assert!(result.documents_ids.is_empty());
// When we search for something that is in the searchable fields
// we must find the appropriate document.
let result = index.search(&rtxn).query(r#""kevin""#).execute().unwrap();
let result = index.search(&rtxn, &index_uid).query(r#""kevin""#).execute().unwrap();
let documents = index.documents(&rtxn, result.documents_ids).unwrap();
assert_eq!(documents.len(), 1);
assert_eq!(documents[0].1.get(0), Some(&br#""kevin""#[..]));
@ -1265,7 +1266,7 @@ mod tests {
let rtxn = index.read_txn().unwrap();
let searchable_fields = index.searchable_fields(&rtxn).unwrap();
assert_eq!(searchable_fields, None);
let result = index.search(&rtxn).query("23").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("23").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1);
let documents = index.documents(&rtxn, result.documents_ids).unwrap();
assert_eq!(documents[0].1.get(0), Some(&br#""kevin""#[..]));
@ -1468,7 +1469,8 @@ mod tests {
// Run an empty query just to ensure that the search results are ordered.
let rtxn = index.read_txn().unwrap();
let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();
let index_uid = String::from("test_index");
let SearchResult { documents_ids, .. } = index.search(&rtxn, &index_uid).execute().unwrap();
let documents = index.documents(&rtxn, documents_ids).unwrap();
// Fetch the documents "age" field in the ordre in which the documents appear.
@ -1511,7 +1513,8 @@ mod tests {
// Run an empty query just to ensure that the search results are ordered.
let rtxn = index.read_txn().unwrap();
let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();
let index_uid = String::from("test_index");
let SearchResult { documents_ids, .. } = index.search(&rtxn, &index_uid).execute().unwrap();
// There must be at least one document with a 34 as the age.
assert_eq!(documents_ids.len(), 3);
@ -1546,7 +1549,8 @@ mod tests {
// Run an empty query just to ensure that the search results are ordered.
let rtxn = index.read_txn().unwrap();
let SearchResult { documents_ids, .. } = index.search(&rtxn).execute().unwrap();
let index_uid = String::from("test_index");
let SearchResult { documents_ids, .. } = index.search(&rtxn, &index_uid).execute().unwrap();
// There must be at least one document with a 34 as the age.
assert_eq!(documents_ids.len(), 3);
@ -1609,18 +1613,19 @@ mod tests {
let expected = fst::Set::from_iter(&set).unwrap();
assert_eq!(stop_words.as_fst().as_bytes(), expected.as_fst().as_bytes());
let index_uid = String::from("test_index");
// when we search for something that is a non prefix stop_words it should be ignored
// thus we should get a placeholder search (all the results = 3)
let result = index.search(&rtxn).query("the ").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("the ").execute().unwrap();
assert_eq!(result.documents_ids.len(), 3);
let result = index.search(&rtxn).query("i ").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("i ").execute().unwrap();
assert_eq!(result.documents_ids.len(), 3);
let result = index.search(&rtxn).query("are ").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("are ").execute().unwrap();
assert_eq!(result.documents_ids.len(), 3);
let result = index.search(&rtxn).query("dog").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("dog").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2); // we have two maxims talking about doggos
let result = index.search(&rtxn).query("benoît").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("benoît").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1); // there is one benoit in our data
// now we'll reset the stop_words and ensure it's None
@ -1635,17 +1640,17 @@ mod tests {
assert!(stop_words.is_none());
// now we can search for the stop words
let result = index.search(&rtxn).query("the").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("the").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2);
let result = index.search(&rtxn).query("i").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("i").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1);
let result = index.search(&rtxn).query("are").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("are").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2);
// the rest of the search is still not impacted
let result = index.search(&rtxn).query("dog").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("dog").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2); // we have two maxims talking about doggos
let result = index.search(&rtxn).query("benoît").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("benoît").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1); // there is one benoit in our data
}
@ -1684,12 +1689,13 @@ mod tests {
let synonyms = index.synonyms(&rtxn).unwrap();
assert!(!synonyms.is_empty()); // at this point the index should return something
let index_uid = String::from("temp");
// Check that we can use synonyms
let result = index.search(&rtxn).query("blini").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("blini").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1);
let result = index.search(&rtxn).query("super like").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("super like").execute().unwrap();
assert_eq!(result.documents_ids.len(), 1);
let result = index.search(&rtxn).query("puppies").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("puppies").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2);
// Reset the synonyms
@ -1705,11 +1711,11 @@ mod tests {
assert!(synonyms.is_empty());
// Check that synonyms are no longer work
let result = index.search(&rtxn).query("blini").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("blini").execute().unwrap();
assert!(result.documents_ids.is_empty());
let result = index.search(&rtxn).query("super like").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("super like").execute().unwrap();
assert!(result.documents_ids.is_empty());
let result = index.search(&rtxn).query("puppies").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("puppies").execute().unwrap();
assert!(result.documents_ids.is_empty());
}
@ -1746,7 +1752,7 @@ mod tests {
assert!(!synonyms.is_empty()); // at this point the index should return something
// Check that we can use synonyms
let result = index.search(&rtxn).query("japanese").execute().unwrap();
let result = index.search(&rtxn, &index_uid).query("japanese").execute().unwrap();
assert_eq!(result.documents_ids.len(), 2);
}
@ -1899,8 +1905,9 @@ mod tests {
}
])).unwrap();
let index_uid = String::from("temp");
let rtxn = index.read_txn().unwrap();
let SearchResult { documents_ids, .. } = index.search(&rtxn).query("S").execute().unwrap();
let SearchResult { documents_ids, .. } = index.search(&rtxn, &index_uid).query("S").execute().unwrap();
let first_id = documents_ids[0];
let documents = index.documents(&rtxn, documents_ids).unwrap();
let (_, content) = documents.iter().find(|(id, _)| *id == first_id).unwrap();