Add an `assert_batches_eq!` macro
- Dominant language
- Rust
- Stars
- 3.6k
- Forks
- 1.3k
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 168
Description
**Is your feature request related to a problem or challenge? Please describe what you are trying to do.**
For unit tests, I want to compare two sets of record batches for equality, and report a readable error message if they aren't. For example:
```rust
let batch = record_batch!(
("column", Int32, [1, 2]),
);
assert_batches_eq!(
&[batch],
[
"+--------+",
"| column |",
"+--------+",
"| 1 |",
"| 2 |",
"+--------+",
];
);
```
Because this diffs the pretty-printed output, the output of a test failure can directly be copied into the source code as the expected result.
**Describe the solution you'd like**
Such a macro [already exists in DataFusion](https://docs.rs/datafusion/latest/datafusion/macro.assert_batches_eq.html). The implementation does not depend on DataFusion internals, so it could be copied into `arrow-rs` with only small adjustments, for example like this:
```rust
#[macro_export]
macro_rules! assert_batches_eq {
($left:expr, [$($right:tt)*] $(,)*) => {
let expected_lines: Vec<&str> = [$($right)*].iter().map(|&s| s).collect();
let formatted = ::arrow::util::pretty::pretty_format_batches($left)
.unwrap()
.to_string();
let actual_lines: Vec<&str> = formatted.trim().lines().collect();
assert!(
expected_lines == actual_lines,
"record batches are not equal:\nleft:\n{:#?}\nright:\n{:#?}",
expected_lines,
actual_lines,
);
};
([$($left:tt)*], $right:expr, $(,)*) => {
assert_batches_eq!($right, [$($left)*])
};
}
```
Here, I've already changed it to accept its arguments in either order, and to not require the syntactic noise of `&` or `vec!` in the call.
**Describe alternatives you've considered**
Copying the macro into my own code.
**Additional context**
Additional ergonomics, for bonus points, could include:
- Accepting two `Vec`es instead of one `Vec` and one `[String]`
- Accepting `IntoIterator` instead of just `Vec`
- Accepting individual `RecordBatch`es instead of only sequences of them
All of these would require a new trait such as `PrettyFormat`, which would have to be public because it's called from within the macro expansion. It would be implemented for `Iterator`, `Vec` and so on.
Contributor guide
Assessment
This issue has not been assessed yet.