Repository: pola-rs/polars
File: crates/polars-testing/src/asserts/utils.rs (Rust)

Code before:

    if check_column_order && ordered_left_cols != ordered_right_cols {
        return Err(polars_err!(
            assertion_error = "DataFrames",
            "columns are not in the same order",
            format!("{:?}", ordered_left_cols),
            format!("{:?}", ordered_right_cols)
        ));
    }

    if check_dtypes {
        if check_column_order {
            let left_dtypes_ordered = left.dtypes();
            let right_dtypes_ordered = right.dtypes();
            if left_dtypes_ordered != right_dtypes_ordered {
                return Err(polars_err!(
                    assertion_error = "DataFrames",
                    "dtypes do not match",
                    format!("{:?}", left_dtypes_ordered),
                    format!("{:?}", right_dtypes_ordered)
                ));
            }
        } else {
            let left_dtypes: PlHashSet<DataType> = left.dtypes().into_iter().collect();
            let right_dtypes: PlHashSet<DataType> = right.dtypes().into_iter().collect();
            if left_dtypes != right_dtypes {
                return Err(polars_err!(
                    assertion_error = "DataFrames",
                    "dtypes do not match",
                    format!("{:?}", left_dtypes),
                    format!("{:?}", right_dtypes)
                ));
            }
        }
    }

    Ok(())
}

/// Verifies that two DataFrames are equal according to a set of configurable criteria.
///
/// This function serves as the main entry point for comparing DataFrames, first validating
/// schema compatibility and then comparing the actual data values column by column.
///
/// # Arguments
///
/// * `left` - The first DataFrame to compare
/// * `right` - The second DataFrame to compare
/// * `options` - A `DataFrameEqualOptions` struct containing configuration parameters:
///   * `check_row_order` - If true, rows must be in the same order
///   * `check_column_order` - If true, columns must be in the same order

Review comment (suggestion):
When `right_schema.get(name)` is `None`, we need to report that the column names differ rather than reporting that dtypes differ.


Code after:

    if check_column_order && ordered_left_cols != ordered_right_cols {
        return Err(polars_err!(
            assertion_error = "DataFrames",
            "columns are not in the same order",
            format!("{:?}", ordered_left_cols),
            format!("{:?}", ordered_right_cols)
        ));
    }

    if check_dtypes {
        if check_column_order {
            let left_dtypes_ordered = left.dtypes();
            let right_dtypes_ordered = right.dtypes();
            if left_dtypes_ordered != right_dtypes_ordered {
                return Err(polars_err!(
                    assertion_error = "DataFrames",
                    "dtypes do not match",
                    format!("{:?}", left_dtypes_ordered),
                    format!("{:?}", right_dtypes_ordered)
                ));
            }
        } else if left_schema.len() != right_schema.len()
            || left_schema
                .iter()
                .any(|(name, dtype)| right_schema.get(name) != Some(dtype))
        {
            return Err(polars_err!(
                assertion_error = "DataFrames",
                "dtypes do not match",
                format!("{:?}", left_schema),
                format!("{:?}", right_schema)
            ));
        }
    }

    Ok(())
}

/// Verifies that two DataFrames are equal according to a set of configurable criteria.
///
/// This function serves as the main entry point for comparing DataFrames, first validating
/// schema compatibility and then comparing the actual data values column by column.
///
/// # Arguments
///
/// * `left` - The first DataFrame to compare
/// * `right` - The second DataFrame to compare
/// * `options` - A `DataFrameEqualOptions` struct containing configuration parameters:
///   * `check_row_order` - If true, rows must be in the same order
///   * `check_column_order` - If true, columns must be in the same order