polars_core/testing.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
//! Testing utilities.
use crate::prelude::*;
impl Series {
/// Check if series are equal. Note that `None == None` evaluates to `false`
pub fn equals(&self, other: &Series) -> bool {
if self.null_count() > 0 || other.null_count() > 0 {
false
} else {
self.equals_missing(other)
}
}
/// Check if all values in series are equal where `None == None` evaluates to `true`.
pub fn equals_missing(&self, other: &Series) -> bool {
match (self.dtype(), other.dtype()) {
// Two [`Datetime`](DataType::Datetime) series are *not* equal if their timezones
// are different, regardless if they represent the same UTC time or not.
#[cfg(feature = "timezones")]
(DataType::Datetime(_, tz_lhs), DataType::Datetime(_, tz_rhs)) => {
if tz_lhs != tz_rhs {
return false;
}
},
_ => {},
}
// Differs from Partial::eq in that numerical dtype may be different
self.len() == other.len() && self.null_count() == other.null_count() && {
let eq = self.equal_missing(other);
match eq {
Ok(b) => b.all(),
Err(_) => false,
}
}
}
}
impl PartialEq for Series {
fn eq(&self, other: &Self) -> bool {
self.equals_missing(other)
}
}
impl DataFrame {
/// Check if [`DataFrame`]' schemas are equal.
pub fn schema_equal(&self, other: &DataFrame) -> PolarsResult<()> {
for (lhs, rhs) in self.iter().zip(other.iter()) {
polars_ensure!(
lhs.name() == rhs.name(),
SchemaMismatch: "column name mismatch: left-hand = '{}', right-hand = '{}'",
lhs.name(), rhs.name()
);
polars_ensure!(
lhs.dtype() == rhs.dtype(),
SchemaMismatch: "column datatype mismatch: left-hand = '{}', right-hand = '{}'",
lhs.dtype(), rhs.dtype()
);
}
Ok(())
}
/// Check if [`DataFrame`]s are equal. Note that `None == None` evaluates to `false`
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
/// let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
///
/// assert!(!df1.equals(&df2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn equals(&self, other: &DataFrame) -> bool {
if self.shape() != other.shape() {
return false;
}
for (left, right) in self.get_columns().iter().zip(other.get_columns()) {
if left.name() != right.name() || !left.equals(right) {
return false;
}
}
true
}
/// Check if all values in [`DataFrame`]s are equal where `None == None` evaluates to `true`.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
/// let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
///
/// assert!(df1.equals_missing(&df2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn equals_missing(&self, other: &DataFrame) -> bool {
if self.shape() != other.shape() {
return false;
}
for (left, right) in self.get_columns().iter().zip(other.get_columns()) {
if left.name() != right.name() || !left.equals_missing(right) {
return false;
}
}
true
}
}
impl PartialEq for DataFrame {
fn eq(&self, other: &Self) -> bool {
self.shape() == other.shape()
&& self
.columns
.iter()
.zip(other.columns.iter())
.all(|(s1, s2)| s1.equals_missing(s2))
}
}
/// 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::*;
#[test]
fn test_series_equals() {
let a = Series::new("a".into(), &[1_u32, 2, 3]);
let b = Series::new("a".into(), &[1_u32, 2, 3]);
assert!(a.equals(&b));
let s = Series::new("foo".into(), &[None, Some(1i64)]);
assert!(s.equals_missing(&s));
}
#[test]
fn test_series_dtype_not_equal() {
let s_i32 = Series::new("a".into(), &[1_i32, 2_i32]);
let s_i64 = Series::new("a".into(), &[1_i64, 2_i64]);
assert!(s_i32.dtype() != s_i64.dtype());
assert!(s_i32.equals(&s_i64));
}
#[test]
fn test_df_equal() {
let a = Column::new("a".into(), [1, 2, 3].as_ref());
let b = Column::new("b".into(), [1, 2, 3].as_ref());
let df1 = DataFrame::new(vec![a, b]).unwrap();
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],
"b" => &[4, 5, 6])
.unwrap();
let df2 = df!("b" => &[4, 5, 6],
"a" => &[1, 2, 3])
.unwrap();
let df3 = df!("" => &[Some(1), None]).unwrap();
let df4 = df!("" => &[f32::NAN]).unwrap();
assert_eq!(df1, df1);
assert_ne!(df1, df2);
assert_eq!(df2, df2);
assert_ne!(df2, df3);
assert_eq!(df3, df3);
assert_eq!(df4, df4);
}
}