|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -//! Utils to make testing easier |
| 18 | +//! Utility functions to make testing DataFusion based crates easier |
19 | 19 |
|
20 | 20 | use std::{env, error::Error, path::PathBuf}; |
21 | 21 |
|
| 22 | +/// Compares formatted output of a record batch with an expected |
| 23 | +/// vector of strings, with the result of pretty formatting record |
| 24 | +/// batches. This is a macro so errors appear on the correct line |
| 25 | +/// |
| 26 | +/// Designed so that failure output can be directly copy/pasted |
| 27 | +/// into the test code as expected results. |
| 28 | +/// |
| 29 | +/// Expects to be called about like this: |
| 30 | +/// |
| 31 | +/// `assert_batch_eq!(expected_lines: &[&str], batches: &[RecordBatch])` |
| 32 | +#[macro_export] |
| 33 | +macro_rules! assert_batches_eq { |
| 34 | + ($EXPECTED_LINES: expr, $CHUNKS: expr) => { |
| 35 | + let expected_lines: Vec<String> = |
| 36 | + $EXPECTED_LINES.iter().map(|&s| s.into()).collect(); |
| 37 | + |
| 38 | + let formatted = arrow::util::pretty::pretty_format_batches($CHUNKS).unwrap(); |
| 39 | + |
| 40 | + let actual_lines: Vec<&str> = formatted.trim().lines().collect(); |
| 41 | + |
| 42 | + assert_eq!( |
| 43 | + expected_lines, actual_lines, |
| 44 | + "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n", |
| 45 | + expected_lines, actual_lines |
| 46 | + ); |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +/// Compares formatted output of a record batch with an expected |
| 51 | +/// vector of strings in a way that order does not matter. |
| 52 | +/// This is a macro so errors appear on the correct line |
| 53 | +/// |
| 54 | +/// Designed so that failure output can be directly copy/pasted |
| 55 | +/// into the test code as expected results. |
| 56 | +/// |
| 57 | +/// Expects to be called about like this: |
| 58 | +/// |
| 59 | +/// `assert_batch_sorted_eq!(expected_lines: &[&str], batches: &[RecordBatch])` |
| 60 | +#[macro_export] |
| 61 | +macro_rules! assert_batches_sorted_eq { |
| 62 | + ($EXPECTED_LINES: expr, $CHUNKS: expr) => { |
| 63 | + let mut expected_lines: Vec<String> = |
| 64 | + $EXPECTED_LINES.iter().map(|&s| s.into()).collect(); |
| 65 | + |
| 66 | + // sort except for header + footer |
| 67 | + let num_lines = expected_lines.len(); |
| 68 | + if num_lines > 3 { |
| 69 | + expected_lines.as_mut_slice()[2..num_lines - 1].sort_unstable() |
| 70 | + } |
| 71 | + |
| 72 | + let formatted = arrow::util::pretty::pretty_format_batches($CHUNKS).unwrap(); |
| 73 | + // fix for windows: \r\n --> |
| 74 | + |
| 75 | + let mut actual_lines: Vec<&str> = formatted.trim().lines().collect(); |
| 76 | + |
| 77 | + // sort except for header + footer |
| 78 | + let num_lines = actual_lines.len(); |
| 79 | + if num_lines > 3 { |
| 80 | + actual_lines.as_mut_slice()[2..num_lines - 1].sort_unstable() |
| 81 | + } |
| 82 | + |
| 83 | + assert_eq!( |
| 84 | + expected_lines, actual_lines, |
| 85 | + "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n", |
| 86 | + expected_lines, actual_lines |
| 87 | + ); |
| 88 | + }; |
| 89 | +} |
| 90 | + |
22 | 91 | /// Returns the arrow test data directory, which is by default stored |
23 | 92 | /// in a git submodule rooted at `testing/data`. |
24 | 93 | /// |
|
0 commit comments