Skip to content

Commit

Permalink
Fix add query building unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdettai committed Dec 9, 2024
1 parent 3e36524 commit 13f87c8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
59 changes: 54 additions & 5 deletions quickwit/quickwit-doc-mapper/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ mod test {

use quickwit_query::query_ast::{
query_ast_from_user_text, FullTextMode, FullTextParams, PhrasePrefixQuery, QueryAstVisitor,
UserInputQuery,
};
use quickwit_query::{
create_default_quickwit_tokenizer_manager, BooleanOperand, MatchAllOrNone,
};
use quickwit_query::{create_default_quickwit_tokenizer_manager, MatchAllOrNone};
use tantivy::schema::{DateOptions, DateTimePrecision, Schema, FAST, INDEXED, STORED, TEXT};
use tantivy::Term;

Expand Down Expand Up @@ -330,7 +333,7 @@ mod test {
search_fields: Vec<String>,
expected: TestExpectation,
) {
check_build_query(user_query, search_fields, expected, true);
check_build_query(user_query, search_fields, expected, true, false);
}

#[track_caller]
Expand All @@ -339,15 +342,31 @@ mod test {
search_fields: Vec<String>,
expected: TestExpectation,
) {
check_build_query(user_query, search_fields, expected, false);
check_build_query(user_query, search_fields, expected, false, false);
}

#[track_caller]
fn check_build_query_static_lenient_mode(
user_query: &str,
search_fields: Vec<String>,
expected: TestExpectation,
) {
check_build_query(user_query, search_fields, expected, false, true);
}

fn test_build_query(
user_query: &str,
search_fields: Vec<String>,
dynamic_mode: bool,
lenient: bool,
) -> Result<String, String> {
let query_ast = query_ast_from_user_text(user_query, Some(search_fields))
let user_input_query = UserInputQuery {
user_text: user_query.to_string(),
default_fields: Some(search_fields),
default_operator: BooleanOperand::And,
lenient,
};
let query_ast = user_input_query
.parse_user_query(&[])
.map_err(|err| err.to_string())?;
let schema = make_schema(dynamic_mode);
Expand All @@ -369,8 +388,9 @@ mod test {
search_fields: Vec<String>,
expected: TestExpectation,
dynamic_mode: bool,
lenient: bool,
) {
let query_result = test_build_query(user_query, search_fields, dynamic_mode);
let query_result = test_build_query(user_query, search_fields, dynamic_mode, lenient);
match (query_result, expected) {
(Err(query_err_msg), TestExpectation::Err(sub_str)) => {
assert!(
Expand Down Expand Up @@ -432,6 +452,11 @@ mod test {
Vec::new(),
TestExpectation::Err("invalid query: field does not exist: `foo`"),
);
check_build_query_static_lenient_mode(
"foo:bar",
Vec::new(),
TestExpectation::Ok("EmptyQuery"),
);
check_build_query_static_mode(
"title:bar",
Vec::new(),
Expand All @@ -442,6 +467,11 @@ mod test {
vec!["fieldnotinschema".to_string()],
TestExpectation::Err("invalid query: field does not exist: `fieldnotinschema`"),
);
check_build_query_static_lenient_mode(
"bar",
vec!["fieldnotinschema".to_string()],
TestExpectation::Ok("EmptyQuery"),
);
check_build_query_static_mode(
"title:[a TO b]",
Vec::new(),
Expand Down Expand Up @@ -510,6 +540,25 @@ mod test {
);
}

#[test]
fn test_wildcard_query() {
check_build_query_static_mode(
"title:hello*",
Vec::new(),
TestExpectation::Ok("PhrasePrefixQuery"),
);
check_build_query_static_mode(
"foo:bar*",
Vec::new(),
TestExpectation::Err("invalid query: field does not exist: `foo`"),
);
check_build_query_static_mode(
"title:hello*yo",
Vec::new(),
TestExpectation::Err("Wildcard query contains wildcard in non final position"),
);
}

#[test]
fn test_datetime_range_query() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl BuildTantivyAst for PhrasePrefixQuery {
let (_, terms) = match self.get_terms(schema, tokenizer_manager) {
Ok(res) => res,
Err(InvalidQuery::FieldDoesNotExist { .. }) if self.lenient => {
return Ok(crate::MatchAllOrNone::MatchNone.into())
return Ok(TantivyQueryAst::match_none())
}
Err(e) => return Err(e),
};
Expand Down

0 comments on commit 13f87c8

Please sign in to comment.