Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Extract testable code and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktBurger committed Jul 4, 2024
1 parent 19db743 commit 794a419
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pyleco_extras_CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 17 additions & 13 deletions pyleco_extras/gui/data_logger/data_logger_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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."""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(","):
Expand Down
16 changes: 16 additions & 0 deletions tests/gui/data_logger/test_data_logger_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 794a419

Please sign in to comment.