From 46e774374be8c8d63dcc1b5ae40e04230d0a1ff3 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:33:58 +0100 Subject: [PATCH] chore(test): test_shared.py turn into pytest (#826) --- test/unittests/utils/test_shared.py | 131 ++++++++++++++-------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/test/unittests/utils/test_shared.py b/test/unittests/utils/test_shared.py index 8eacb9c6b..0018e4fb9 100644 --- a/test/unittests/utils/test_shared.py +++ b/test/unittests/utils/test_shared.py @@ -1,82 +1,81 @@ -import unittest from typing import Union import numpy as np import pandas as pd import pytest +from pandas.testing import assert_frame_equal from dsp_tools.commands.excel2xml.propertyelement import PropertyElement from dsp_tools.utils import shared -# ruff: noqa: PT009 (pytest-unittest-assertion) (remove this line when pytest is used instead of unittest) -# ruff: noqa: PT027 (pytest-unittest-raises-assertion) (remove this line when pytest is used instead of unittest) +def test_prepare_dataframe() -> None: + original_df = pd.DataFrame( + { + " TitLE of Column 1 ": ["1", " 0-1 ", "1-n ", pd.NA, " ", " ", "", " 0-n ", pd.NA], + " Title of Column 2 ": [None, "1", 1, "text", "text", "text", "text", "text", "text"], + "Title of Column 3": ["", pd.NA, None, "text", "text", "text", "text", pd.NA, "text"], + } + ) + expected_df = pd.DataFrame( + { + "title of column 1": ["0-1", "1-n", "0-n"], + "title of column 2": ["1", "1", "text"], + "title of column 3": ["", "", ""], + }, + index=[1, 2, 7], + ) + returned_df = shared.prepare_dataframe( + df=original_df, required_columns=[" TitLE of Column 1 ", " Title of Column 2 "], location_of_sheet="" + ) + assert_frame_equal(returned_df, expected_df) -class TestShared(unittest.TestCase): - def test_prepare_dataframe(self) -> None: - original_df = pd.DataFrame( - { - " TitLE of Column 1 ": ["1", " 0-1 ", "1-n ", pd.NA, " ", " ", "", " 0-n ", pd.NA], - " Title of Column 2 ": [None, "1", 1, "text", "text", "text", "text", "text", "text"], - "Title of Column 3": ["", pd.NA, None, "text", "text", "text", "text", pd.NA, "text"], - } - ) - expected_df = pd.DataFrame( - { - "title of column 1": ["0-1", "1-n", "0-n"], - "title of column 2": ["1", "1", "text"], - "title of column 3": ["", "", ""], - } - ) - returned_df = shared.prepare_dataframe( - df=original_df, required_columns=[" TitLE of Column 1 ", " Title of Column 2 "], location_of_sheet="" - ) - for (i, expected_row), (_, returned_row) in zip(expected_df.iterrows(), returned_df.iterrows()): - self.assertListEqual(list(expected_row), list(returned_row), msg=f"Failed in row {i}") - def test_check_notna(self) -> None: - na_values = [ - None, - pd.NA, - pd.NA, - "", - " ", - "-", - ",", - ".", - "*", - " ⳰", - " ῀ ", # noqa: RUF001 (ambiguous-unicode-character-string) - " ῾ ", # noqa: RUF001 (ambiguous-unicode-character-string) - " \n\t ", - "N/A", - "n/a", - "", - "None", - ["a", "b"], - pd.array(["a", "b"]), - np.array([0, 1]), - ] - for na_value in na_values: - self.assertFalse(shared.check_notna(na_value), msg=f"Failed na_value: {na_value}") +def test_check_notna_good() -> None: + na_values = [ + None, + pd.NA, + pd.NA, + "", + " ", + "-", + ",", + ".", + "*", + " ⳰", + " ῀ ", # noqa: RUF001 (ambiguous-unicode-character-string) + " ῾ ", # noqa: RUF001 (ambiguous-unicode-character-string) + " \n\t ", + "N/A", + "n/a", + "", + "None", + ["a", "b"], + pd.array(["a", "b"]), + np.array([0, 1]), + ] + for na_value in na_values: + assert not shared.check_notna(na_value), f"Failed na_value: {na_value}" - notna_values_orig: list[Union[str, int, float, bool]] = [ - 1, - 0.1, - True, - False, - "True", - "False", - r" \n\t ", - "0", - "_", - "Ὅμηρος", - "!", - "?", - ] - notna_values_as_propelem = [PropertyElement(x) for x in notna_values_orig] - for notna_value in notna_values_orig + notna_values_as_propelem: - self.assertTrue(shared.check_notna(notna_value), msg=f"Failed notna_value: {notna_value}") + +def test_check_notna_problem() -> None: + notna_values_orig: list[Union[str, int, float, bool]] = [ + 1, + 0.1, + True, + False, + "True", + "False", + r" \n\t ", + "0", + "_", + "Ὅμηρος", + "!", + "?", + ] + notna_values_as_propelem = [PropertyElement(x) for x in notna_values_orig] + for notna_value in notna_values_orig + notna_values_as_propelem: + assert shared.check_notna(notna_value), f"Failed notna_value: {notna_value}" if __name__ == "__main__":