Skip to content

Commit

Permalink
Fixed up test of error messages for LazyFrame with error plan
Browse files Browse the repository at this point in the history
  • Loading branch information
rben01 committed Oct 18, 2023
1 parent 938259b commit e4740a6
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions crates/polars-lazy/src/tests/err_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,78 @@ use super::*;

#[test]
fn error_messages() {
fn test_it(df: &LazyFrame, n: usize, base_err_msg: &str) {
fn assert_errors_eq(e1: &PolarsError, e2: &PolarsError) {
use PolarsError::*;
match (e1, e2) {
(ColumnNotFound(s1), ColumnNotFound(s2)) => {
assert_eq!(s1.as_ref(), s2.as_ref());
},
(ComputeError(s1), ComputeError(s2)) => {
assert_eq!(s1.as_ref(), s2.as_ref());
},
_ => panic!("{e1:?} != {e2:?}"),
}
}

fn test_it(df: LazyFrame, n: usize) {
let base_err_msg = format!(
"xyz\n\nError originated just after this \
operation:\n{INITIAL_PROJECTION_STR}"
);
let col_not_found_err_msg = format!("not found: {base_err_msg}");

let plan_err_str;
let collect_err;

if n == 0 {
plan_err_str = format!(
"ErrorState(NotYetEncountered { \
err: ColumnNotFound(ErrString(\"{base_err_msg}\")) })"
"ErrorState(NotYetEncountered {{ \
err: ColumnNotFound(ErrString({base_err_msg:?})) }})"
);
collect_err = PolarsError::ColumnNotFound(ErrString::from(&base_err_msg));
collect_err = PolarsError::ColumnNotFound(ErrString::from(base_err_msg.to_owned()));
} else {
plan_err_str = format!(
"ErrorState(AlreadyEncountered { \
n_times: {n}, prev_err_msg: \"not found: {base_err_msg}\" })"
"ErrorState(AlreadyEncountered {{ n_times: {n}, \
prev_err_msg: {col_not_found_err_msg:?} }})",
);
collect_err = PolarsError::ComputeError(ErrString::from(format!(
"LogicalPlan already failed (depth: {n}) \
with error: 'not found: {base_err_msg}"
with error: '{col_not_found_err_msg}'"
)))
};

assert_eq!(df.describe_plan(), plan_err_str);
assert_eq!(df.collect().unwrap_err(), collect_err);
assert_errors_eq(&df.collect().unwrap_err(), &collect_err);
}

let initial_projection: &str = r#"DF ["c1"]; PROJECT */1 COLUMNS; SELECTION: "None""#;
let base_err_msg =
format!("xyz\n\nError originated just after this operation:\n{INITIAL_PROJECTION}");
const INITIAL_PROJECTION_STR: &str = r#"DF ["c1"]; PROJECT */1 COLUMNS; SELECTION: "None""#;

let df = df! [
"c1" => [0, 1],
]
.unwrap()
.lazy();

assert_eq!(df.describe_plan(), initial_projection);
assert_eq!(df.describe_plan(), INITIAL_PROJECTION_STR);

let df0 = df.clone().select([col("xyz")]);
test_it(&df0, 0, &base_err_msg);
test_it(df0, 0);

let df1 = df.clone().select([col("xyz")]).select([col("c1")]);
test_it(&df1, 1, &base_err_msg);
test_it(df1, 1);

let df2 = df
.clone()
.select([col("xyz")])
.select([col("c1")])
.select([col("c2")]);
test_it(&df2, 2, &base_err_msg);
test_it(df2, 2);

let df3 = df
.clone()
.select([col("xyz")])
.select([col("c1")])
.select([col("c2")])
.select([col("c3")]);
test_it(&df3, 3, &base_err_msg);
test_it(df3, 3);
}

0 comments on commit e4740a6

Please sign in to comment.