Skip to content

Commit

Permalink
Move assert_df_eq to polars_core
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvangelderen committed Mar 6, 2024
1 parent 60af176 commit a1f6199
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
25 changes: 25 additions & 0 deletions crates/polars-core/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ impl PartialEq for DataFrame {
}
}

/// Asserts that two expressions of type [`DataFrame`] are equal according to [`DataFrame::equals`]
/// at runtime. If the expression are not equal, the program will panic with a message that displays
/// both dataframes.
#[macro_export]
macro_rules! assert_df_eq {
($a:expr, $b:expr $(,)?) => {
let a: &$crate::frame::DataFrame = &$a;
let b: &$crate::frame::DataFrame = &$b;
assert!(a.equals(b), "expected {:?}\nto equal {:?}", a, b);
};
}

#[cfg(test)]
mod test {
use crate::prelude::*;
Expand Down Expand Up @@ -194,6 +206,19 @@ mod test {
assert!(df1.equals(&df1))
}

#[test]
fn assert_df_eq_passes() {
let df = df!("a" => [1], "b" => [2]).unwrap();
assert_df_eq!(df, df);
drop(df); // Ensure `assert_df_eq!` does not consume its arguments.
}

#[test]
#[should_panic(expected = "to equal")]
fn assert_df_eq_panics() {
assert_df_eq!(df!("a" => [1]).unwrap(), df!("a" => [2]).unwrap(),);
}

#[test]
fn test_df_partialeq() {
let df1 = df!("a" => &[1, 2, 3],
Expand Down
10 changes: 1 addition & 9 deletions crates/polars/tests/it/io/ipc_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
mod test {
use std::io::Cursor;

use polars_core::df;
use polars_core::prelude::*;
use polars_core::{assert_df_eq, df};
use polars_io::ipc::*;
use polars_io::{SerReader, SerWriter};

Expand All @@ -21,14 +21,6 @@ mod test {
buf
}

macro_rules! assert_df_eq {
($a:expr, $b:expr) => {
let a: &polars_core::frame::DataFrame = &$a;
let b: &polars_core::frame::DataFrame = &$b;
assert!(a.equals(b), "expected {:?}\nto equal {:?}", a, b);
};
}

#[test]
fn write_and_read_ipc_stream() {
let df = create_df();
Expand Down

0 comments on commit a1f6199

Please sign in to comment.