From 76f1db263fe6121e5e76b8a40ef978163224b266 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:33:29 +0200 Subject: [PATCH] fix: no-op test that doesn't test anything (DEV-2655) (#517) * Fix * Update test_properties.py * Refomatted error message --- src/dsp_tools/utils/excel2json/properties.py | 4 +- .../test_excel2json/test_properties.py | 59 ++++++++++--------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/dsp_tools/utils/excel2json/properties.py b/src/dsp_tools/utils/excel2json/properties.py index 0bdd5b5b1..f2f7c4d9a 100644 --- a/src/dsp_tools/utils/excel2json/properties.py +++ b/src/dsp_tools/utils/excel2json/properties.py @@ -260,7 +260,7 @@ def _check_compliance_gui_attributes(df: pd.DataFrame) -> dict[str, pd.Series] | else: final_series = no_attribute_check # The boolean series is returned - return {"wrong gui_attributes": final_series} + return {"gui_attributes": final_series} def _check_missing_values_in_row_raise_error(df: pd.DataFrame, excelfile: str) -> None: @@ -293,7 +293,7 @@ def _check_missing_values_in_row_raise_error(df: pd.DataFrame, excelfile: str) - if missing_dict: # Get the row numbers from the boolean series missing_dict = utl.get_wrong_row_numbers(wrong_row_dict=missing_dict, true_remains=True) - error_str = "\n".join([f" - Column Name: {k} Row Number: {v}" for k, v in missing_dict.items()]) + error_str = "\n".join([f"- Column '{k}' Row Number(s): {v}" for k, v in missing_dict.items()]) raise UserError(f"The file '{excelfile}' is missing values in the following rows:\n" f"{error_str}") diff --git a/test/unittests/test_excel2json/test_properties.py b/test/unittests/test_excel2json/test_properties.py index 44b48d871..2499643bf 100644 --- a/test/unittests/test_excel2json/test_properties.py +++ b/test/unittests/test_excel2json/test_properties.py @@ -13,7 +13,7 @@ import pytest from pandas.testing import assert_frame_equal -from dsp_tools.models.exceptions import BaseError, UserError +from dsp_tools.models.exceptions import UserError from dsp_tools.utils.excel2json import properties as e2j @@ -440,13 +440,17 @@ def test__do_property_excel_compliance(self) -> None: "gui_attributes": ["size: 32, maxlength: 128", pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA], } ) - with self.assertRaises(BaseError) as context: + with self.assertRaisesRegex( + UserError, + r"The file 'Test' is missing values in the following rows\:\n" + r"\- Column 'name' Row Number\(s\)\: \[9\]\n" + r"\- Column 'super' Row Number\(s\)\: \[2, 4, 9\]\n" + r"\- Column 'object' Row Number\(s\)\: \[5, 9\]\n" + r"\- Column 'gui_element' Row Number\(s\)\: \[6, 8, 9\]\n" + r"\- Column 'label' Row Number\(s\)\: \[3, 8\]\n" + r"\- Column 'gui_attributes' Row Number\(s\)\: \[7\]", + ): e2j._do_property_excel_compliance(df=original_df, excelfile="Test") - self.assertEqual( - context, - "The file '{excel_filename}' is missing values in some rows. See below for more information:\n" - "{error_str}", - ) @pytest.mark.filterwarnings("ignore::UserWarning") def test__rename_deprecated_hlist(self) -> None: @@ -480,27 +484,28 @@ def test__get_gui_attribute(self) -> None: {"gui_attributes": [pd.NA, "max:1.4 / min:1.2", "hlist:", "234345", "hlist: languages,"]} ) self.assertIsNone(e2j._get_gui_attribute(df_row=original_df.loc[0, :], row_num=2, excelfile="Test")) - with self.assertRaises(UserError) as context: + + with self.assertRaisesRegex( + UserError, + r"Row 3 of Excel file Test contains invalid data in column 'gui_attributes'\.\n" + r"The expected format is '\[attribute\: value, attribute\: value\]'\.", + ): e2j._get_gui_attribute(df_row=original_df.loc[1, :], row_num=3, excelfile="Test") - self.assertEqual( - "Row {row_num} of Excel file {excel_filename} contains invalid data in column 'gui_attributes'. " - "The expected format is '[attribute: value, attribute: value]'.", - context, - ) - with self.assertRaises(UserError) as context: + + with self.assertRaisesRegex( + UserError, + r"Row 4 of Excel file Test contains invalid data in column 'gui_attributes'\.\n" + r"The expected format is '\[attribute\: value, attribute\: value\]'\.", + ): e2j._get_gui_attribute(df_row=original_df.loc[2, :], row_num=4, excelfile="Test") - self.assertEqual( - "Row {row_num} of Excel file {excel_filename} contains invalid data in column 'gui_attributes'. " - "The expected format is '[attribute: value, attribute: value]'.", - context, - ) - with self.assertRaises(UserError) as context: + + with self.assertRaisesRegex( + UserError, + r"Row 5 of Excel file Test contains invalid data in column 'gui_attributes'\.\n" + r"The expected format is '\[attribute\: value, attribute\: value\]'\.", + ): e2j._get_gui_attribute(df_row=original_df.loc[3, :], row_num=5, excelfile="Test") - self.assertEqual( - "Row {row_num} of Excel file {excel_filename} contains invalid data in column 'gui_attributes'. " - "The expected format is '[attribute: value, attribute: value]'.", - context, - ) + expected_dict = {"hlist": "languages"} returned_dict = e2j._get_gui_attribute(df_row=original_df.loc[4, :], row_num=6, excelfile="Test") self.assertDictEqual(expected_dict, cast(dict[str, str], returned_dict)) @@ -520,10 +525,10 @@ def test__check_compliance_gui_attributes(self) -> None: "gui_attributes": ["Spinbox_attr", pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, "TimeStamp_attr"], } ) - expected_dict = {"wrong gui_attributes": [False, True, False, False, False, False, True]} + expected_dict = {"gui_attributes": [False, True, False, False, False, False, True]} returned_dict = e2j._check_compliance_gui_attributes(df=original_df) returned_dict = cast(dict[str, list[pd.Series]], returned_dict) - casted_dict: dict[str, Any] = {"wrong gui_attributes": list(returned_dict["wrong gui_attributes"])} + casted_dict: dict[str, Any] = {"gui_attributes": list(returned_dict["gui_attributes"])} self.assertDictEqual(expected_dict, casted_dict) def test__row2prop(self) -> None: