Skip to content

Commit

Permalink
test(helper): add column name in assert messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Caceresenzo committed Jun 7, 2024
1 parent a7fe391 commit 89c17c1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

def assertDataFramesEqual(self: unittest.TestCase, first: pandas.DataFrame, second: pandas.DataFrame) -> bool:
for index, column in enumerate(first.columns):
self.assertEqual(first.dtypes.iloc[index], second.dtypes.iloc[index], "type mismatch between dataframes")
self.assertEqual(first.dtypes.iloc[index], second.dtypes.iloc[index], f"{column}: type mismatch between dataframes")

self.assertTrue((first[column].isna() == second[column].isna()).all(), "not all NaN are equal")
self.assertTrue((first[column].isna() == second[column].isna()).all(), f"{column}: not all NaN are equal")

if (
pandas.api.types.is_integer_dtype(first[column])
or pandas.api.types.is_string_dtype(first[column])
or pandas.api.types.is_bool_dtype(first[column])
):
self.assertTrue((first[~first[column].isna()][column] == second[~second[column].isna()][column]).all(), "not all non-NaN are equal in comparing int/string/bool")
self.assertTrue((first[~first[column].isna()][column] == second[~second[column].isna()][column]).all(), f"{column}: not all non-NaN are equal in comparing int/string/bool")

elif pandas.api.types.is_float_dtype(first[column]):
self.assertTrue(numpy.allclose(first[~first[column].isna()][column], second[~second[column].isna()][column]), "not all non-NaN are equal in comparing float")
self.assertTrue(numpy.allclose(
first[~first[column].isna()][column],
second[~second[column].isna()][column],
equal_nan=True
), f"{column}: not all non-NaN are equal in comparing float")

else:
self.fail(f"Column {column} is of an unknown type: {first[column].dtype}")
self.fail(f"{column}: unknown type: {first[column].dtype}")

0 comments on commit 89c17c1

Please sign in to comment.