diff --git a/quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs b/quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs index 846d4a428cf..ad51e3d5d52 100644 --- a/quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs +++ b/quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs @@ -107,3 +107,40 @@ fn parse_and_convert(literal: JsonLiteral, parser: &StrptimeParser) -> anyhow::R Ok(literal) } } + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use quickwit_datetime::StrptimeParser; + + use crate::elastic_query_dsl::range_query::parse_and_convert; + use crate::JsonLiteral; + + #[test] + fn test_parse_and_convert() { + let parser = StrptimeParser::from_str("%Y-%m-%d %H:%M:%S").unwrap(); + + // valid datetime + let input = JsonLiteral::String("2022-12-30 05:45:00".to_string()); + let result = parse_and_convert(input, &parser)?; + assert_eq!( + result, + JsonLiteral::String("2022-12-30 5:45:00.0 +00:00:00".to_string()) + ); + + // invalid datetime + let input = JsonLiteral::String("invalid datetime".to_string()); + let result = parse_and_convert(input, &parser); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Failed to parse date time")); + + // non_string(number) input + let input = JsonLiteral::Number(27.into()); + let result = parse_and_convert(input.clone(), &parser)?; + assert_eq!(result, input); + } +}