Skip to content

Commit

Permalink
Add a few datetime parsing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilload committed Feb 7, 2024
1 parent 9c94f12 commit 74fd99c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions quickwit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bytes = { version = "1", features = ["serde"] }
bytesize = { version = "1.3.0", features = ["serde"] }
bytestring = "1.3.0"
chitchat = { git = "https://github.com/quickwit-oss/chitchat.git", rev = "5d7ff34" }
chrono = { version = "0.4.23", default-features = false, features = [
chrono = { version = "0.4", default-features = false, features = [
"clock",
"std",
] }
Expand Down Expand Up @@ -227,7 +227,7 @@ thiserror = "1"
thousands = "0.2.0"
tikv-jemalloc-ctl = "0.5"
tikv-jemallocator = "0.5"
time = { version = "0.3.17", features = ["std", "formatting", "macros"] }
time = { version = "0.3", features = ["std", "formatting", "macros"] }
tokio = { version = "1.36", features = ["full"] }
tokio-stream = { version = "0.1", features = ["sync"] }
tokio-util = { version = "0.7", features = ["full"] }
Expand Down
18 changes: 9 additions & 9 deletions quickwit/quickwit-datetime/src/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ pub struct StrptimeParser {
impl FromStr for StrptimeParser {
type Err = String;

fn from_str(strptime_format_str: &str) -> Result<Self, Self::Err> {
fn from_str(strptime_format: &str) -> Result<Self, Self::Err> {
StrptimeParser::try_new(
strptime_format_str.to_string(),
strptime_format_str.to_lowercase().contains("%z"),
strptime_format.to_string(),
strptime_format.to_lowercase().contains("%z"),
|strptime_format: &String| {
parse_to_format_item(strptime_format).map_err(|err| {
format!("invalid format specification `{strptime_format}`. error: {err}.")
parse_to_format_item(strptime_format).map_err(|error| {
format!("invalid strptime format `{strptime_format}`: {error}")
})
},
)
Expand All @@ -70,7 +70,7 @@ impl StrptimeParser {
.is_empty()
{
anyhow::bail!(
"datetime string `{}` does not match format `{}`",
"datetime string `{}` does not match strptime format `{}`",
date_time_str,
self.borrow_strptime_format()
);
Expand Down Expand Up @@ -447,10 +447,10 @@ mod tests {
parser.parse_date_time("2021-01-01").unwrap(),
datetime!(2021-01-01 00:00:00 UTC)
);
let error_str = parser.parse_date_time("2021-01-01TABC").unwrap_err();
let error = parser.parse_date_time("2021-01-01TABC").unwrap_err();
assert_eq!(
error_str,
"datetime string `2021-01-01TABC` does not match format `%Y-%m-%d`"
error,
"datetime string `2021-01-01TABC` does not match strptime format `%Y-%m-%d`"
);
}
}
20 changes: 19 additions & 1 deletion quickwit/quickwit-datetime/src/date_time_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,30 @@ mod tests {
"2012-05-21 12:09:14 -03:00",
datetime!(2012-05-21 15:09:14 UTC),
),
(
"%Y-%m-%d %H:%M:%S.%f",
"2024-01-31 18:40:19.950",
datetime!(2024-01-31 18:40:19.950000000 UTC),
),
(
"%Y-%m-%d %H:%M:%S.%f",
"2024-01-31 18:40:19.950188",
datetime!(2024-01-31 18:40:19.950188000 UTC),
),
(
"%Y-%m-%d %H:%M:%S.%f",
"2024-01-31 18:40:19.950188123",
datetime!(2024-01-31 18:40:19.950188123 UTC),
),
];
for (fmt, date_time_str, expected) in test_data {
let parser = StrptimeParser::from_str(fmt).unwrap();
let result = parser.parse_date_time(date_time_str);
if let Err(error) = &result {
panic!("Failed to parse: {date_time_str} {fmt} {error}")
panic!(
"failed to parse `{date_time_str}` using the following strptime format \
`{fmt}`: {error}"
)
}
assert_eq!(result.unwrap(), expected);
}
Expand Down

0 comments on commit 74fd99c

Please sign in to comment.