From 794a419ac9a457b51554e61e602ab2f6a3a38e9d Mon Sep 17 00:00:00 2001 From: Benedikt Burger <67148916+BenediktBurger@users.noreply.github.com> Date: Thu, 4 Jul 2024 11:53:58 +0200 Subject: [PATCH] Extract testable code and add test. --- .github/workflows/pyleco_extras_CI.yml | 1 + .../gui/data_logger/data_logger_base.py | 30 +++++++++++-------- .../gui/data_logger/test_data_logger_base.py | 16 ++++++++++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.github/workflows/pyleco_extras_CI.yml b/.github/workflows/pyleco_extras_CI.yml index 8181b8e..95b29bc 100644 --- a/.github/workflows/pyleco_extras_CI.yml +++ b/.github/workflows/pyleco_extras_CI.yml @@ -29,6 +29,7 @@ jobs: - name: Lint with ruff uses: chartboost/ruff-action@v1 with: + version: 0.4.10 # ruff-action@v1 is broken in regard to ruff 0.5.0 args: --extend-select=E9,F63,F7,F82 --show-source # - uses: ammaraskar/sphinx-problem-matcher@master # - name: Generate docs diff --git a/pyleco_extras/gui/data_logger/data_logger_base.py b/pyleco_extras/gui/data_logger/data_logger_base.py index c4bfb57..94177f1 100644 --- a/pyleco_extras/gui/data_logger/data_logger_base.py +++ b/pyleco_extras/gui/data_logger/data_logger_base.py @@ -277,9 +277,10 @@ def get_configuration(self) -> dict[str, Any]: config.update(self.get_gui_configuration()) return config - def _read_variables_and_units(self) -> None: - var_text = self.teVariables.toPlainText().replace(": ", ":").replace(",", " ") - raw_vars = var_text.split() + @staticmethod + def _interpret_variables_and_units_text(var_text: str) -> tuple[list[str], dict[str, str]]: + sanitized_text = var_text.replace(": ", ":").replace(",", " ") + raw_vars = sanitized_text.split() last_name = "" variables = [] units = {} @@ -297,8 +298,11 @@ def _read_variables_and_units(self) -> None: if u is not None: units[v] = u variables.append(v) - self._variables = variables - self._units = units + return variables, units + + def _read_variables_and_units(self) -> None: + var_text = self.teVariables.toPlainText() + self._variables, self._units = self._interpret_variables_and_units_text(var_text=var_text) def _update_variables_and_units(self) -> None: """Update the line with variables and units.""" @@ -342,13 +346,12 @@ def trigger_type(self) -> TriggerTypes: @trigger_type.setter def trigger_type(self, value: TriggerTypes | str) -> None: - match value: - case TriggerTypes.NONE: - self.actionPause.setChecked(True) - case TriggerTypes.TIMER: - self.cbTimer.setChecked(True) - case TriggerTypes.VARIABLE: - self.cbTrigger.setChecked(True) + if value == TriggerTypes.NONE: + self.actionPause.setChecked(True) + elif value == TriggerTypes.TIMER: + self.cbTimer.setChecked(True) + elif value == TriggerTypes.VARIABLE: + self.cbTrigger.setChecked(True) @property def trigger_timeout(self) -> float: @@ -397,7 +400,8 @@ def set_configuration(self, configuration: dict[str, Any]) -> None: """Set measurement configuration according to the dict `configuration`.""" self._set_config(**configuration) - def read_legacy_units(self, text: str) -> dict[str, str]: + @staticmethod + def read_legacy_units(text: str) -> dict[str, str]: """Interpreting the old units text and returning a corresponding dict.""" units = {} for element in text.split(","): diff --git a/tests/gui/data_logger/test_data_logger_base.py b/tests/gui/data_logger/test_data_logger_base.py index 30aac5f..9e04eea 100644 --- a/tests/gui/data_logger/test_data_logger_base.py +++ b/tests/gui/data_logger/test_data_logger_base.py @@ -37,3 +37,19 @@ def test_get_y(self, data_logger: DataLoggerBase): def test_get_data_keys(data_logger: DataLoggerBase): assert list(data_logger.get_data_keys()) == ["x", "y"] + + +@pytest.mark.parametrize( + "text, vars, units", + ( + ("time:s, random", ["time", "random"], {"time": "s"}), + ( + "time:s,\nSERVER.pub.var, .var2: W", + ["time", "SERVER.pub.var", "SERVER.pub.var2"], + {"time": "s", "SERVER.pub.var2": "W"}, + ), + ("abc, .def", ["abc", ".def"], {}), + ), +) +def test_interpret_variables_text(text, vars, units): + assert DataLoggerBase._interpret_variables_and_units_text(text) == (vars, units)