Skip to content

Commit

Permalink
Refactor assert_batches_sorted_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
OussamaSaoudi-db committed Dec 10, 2024
1 parent 019a321 commit dacfc34
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions kernel/tests/cdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,36 @@ fn read_cdf_for_table(
}

fn assert_batches_sorted_eq(expected_lines: &[impl ToString], batches: &[RecordBatch]) {
let mut expected_lines: Vec<String> = expected_lines.iter().map(ToString::to_string).collect();
let sort_rows = |lines: &mut Vec<String>| {
let num_lines = lines.len();
if num_lines > 3 {
// sort except for header + footer
lines.as_mut_slice()[2..num_lines - 1].sort_unstable()
}
};

// sort except for header + footer
let num_lines = expected_lines.len();
if num_lines > 3 {
expected_lines.as_mut_slice()[2..num_lines - 1].sort_unstable()
}
let mut expected_lines: Vec<String> = expected_lines.iter().map(ToString::to_string).collect();
sort_rows(&mut expected_lines);

let formatted = arrow::util::pretty::pretty_format_batches(batches)
.unwrap()
.to_string();

let mut actual_lines: Vec<&str> = formatted.trim().lines().collect();

// sort except for header + footer
let num_lines = actual_lines.len();
if num_lines > 3 {
actual_lines.as_mut_slice()[2..num_lines - 1].sort_unstable()
}
let mut actual_lines: Vec<String> = formatted.trim().lines().map(ToString::to_string).collect();
sort_rows(&mut actual_lines);

let expected_table_str = expected_lines.join("\n");
let actual_table_str = actual_lines.join("\n");

assert_eq!(
expected_lines.len(),
actual_lines.len(),
"Incorrect number of lines. Expected {} lines:\n{}\nbut got {} lines:\n{} ",
expected_lines.len(),
"Incorrect number of lines. Expected:\n{}\nbut got:\n{} ",
expected_table_str,
actual_lines.len(),
actual_table_str
);
for (expected, actual) in expected_lines.iter().zip(actual_lines) {
for (expected, actual) in expected_lines.iter().zip(&actual_lines) {
assert_eq!(
expected, actual,
"Expected:\n{}\nbut got:\n{}",
Expand Down

0 comments on commit dacfc34

Please sign in to comment.