From c9533c1a8b4c736e0d8fe52cfa82dbf0269c21b7 Mon Sep 17 00:00:00 2001 From: tangkong Date: Wed, 15 Nov 2023 12:58:17 -0800 Subject: [PATCH] TST/MNT: fix up tests, other legacy code --- atef/tests/conftest.py | 33 ++++++++++++++++++++------------ atef/tests/test_configuration.py | 15 ++++++++++----- atef/tests/test_page.py | 10 ++++++---- atef/widgets/config/utils.py | 29 +++++++++++++++++++++------- atef/widgets/config/window.py | 8 ++++---- 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/atef/tests/conftest.py b/atef/tests/conftest.py index 1ce4cd26..bd39915c 100644 --- a/atef/tests/conftest.py +++ b/atef/tests/conftest.py @@ -4,7 +4,7 @@ import pathlib import tempfile from functools import partial -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, get_args import happi import ophyd @@ -18,15 +18,15 @@ import atef from atef.cache import get_signal_cache from atef.check import Equals, Greater, GreaterOrEqual, LessOrEqual, NotEquals -from atef.config import (ConfigurationFile, ConfigurationGroup, - DeviceConfiguration, PVConfiguration, - ToolConfiguration) -from atef.procedure import ProcedureFile +from atef.config import (AnyConfiguration, ConfigurationFile, + ConfigurationGroup, DeviceConfiguration, + PVConfiguration, ToolConfiguration) +from atef.procedure import AnyProcedure, ProcedureFile from atef.tools import Ping from atef.type_hints import AnyDataclass from atef.util import ophyd_cleanup -from atef.widgets.config.page import (PAGE_MAP, AtefItem, ComparisonPage, - PageWidget, link_page) +from atef.widgets.config.page import ComparisonPage, PageWidget +from atef.widgets.config.window import DualTree from ..archive_device import ArchivedValue, ArchiverHelper @@ -330,11 +330,20 @@ def configuration_group(): @pytest.fixture def make_page(): def make_page_fn(cfg: AnyDataclass) -> PageWidget: - page_cls = PAGE_MAP[type(cfg)] - cfg_page = page_cls(cfg) - cfg_item = AtefItem(QtWidgets.QTreeWidget(), name='root', - func_name=page_cls.__name__) - link_page(item=cfg_item, widget=cfg_page) + # page_cls = PAGE_MAP[type(cfg)] + # cfg_item = TreeItem(data=cfg) + if isinstance(cfg, get_args(AnyConfiguration)): + file = ConfigurationFile() + file.root.configs.append(cfg) + elif isinstance(cfg, get_args(AnyProcedure)): + file = ProcedureFile() + file.root.steps.append(cfg) + + tree = DualTree(orig_file=file) + tree.select_by_data(cfg) + cfg_page = tree.current_widget + # cfg_page = page_cls(cfg, tree_item=cfg_item, full_tree=tree) + # link_page(item=cfg_item, widget=cfg_page) return cfg_page return make_page_fn diff --git a/atef/tests/test_configuration.py b/atef/tests/test_configuration.py index c9223b76..718df606 100644 --- a/atef/tests/test_configuration.py +++ b/atef/tests/test_configuration.py @@ -38,10 +38,15 @@ def test_gather_pvs( device_configuration: DeviceConfiguration ): # only one pv in pv_configuration, will just grab all PV's - assert len(get_relevant_pvs('MY:PREFIX:hello', pv_configuration)) == 1 - assert len(get_relevant_pvs('shared', pv_configuration)) == 1 + pv_comps = list(pv_configuration.by_pv['MY:PREFIX:hello']) + for comp in pv_comps: + assert len(get_relevant_pvs(comp, pv_configuration)) == 1 + + shared_comp = pv_configuration.shared[0] + assert len(get_relevant_pvs(shared_comp, pv_configuration)) == 1 # 2 devices, 2 attrs - assert len(get_relevant_pvs('fjdslka', device_configuration)) == 0 - assert len(get_relevant_pvs('shared', device_configuration)) == 4 - assert len(get_relevant_pvs('setpoint', device_configuration)) == 2 + setpoint_comp = device_configuration.by_attr['setpoint'][0] + readback_comp = device_configuration.by_attr['readback'][0] + assert len(get_relevant_pvs(setpoint_comp, device_configuration)) == 2 + assert len(get_relevant_pvs(readback_comp, device_configuration)) == 2 diff --git a/atef/tests/test_page.py b/atef/tests/test_page.py index 108796b5..d855840a 100644 --- a/atef/tests/test_page.py +++ b/atef/tests/test_page.py @@ -60,8 +60,9 @@ def test_add_delete_config( monkeypatch.setattr(QtWidgets.QMessageBox, 'question', lambda *args, **kwargs: QtWidgets.QMessageBox.Yes) qtbot.mouseClick(widget.delete_button, QtCore.Qt.LeftButton) - - assert first_config not in configuration_group_page.data.configs + qtbot.wait_until( + lambda: first_config not in configuration_group_page.data.configs + ) @pytest.mark.parametrize( @@ -142,10 +143,11 @@ def test_change_comparison( # get comparison page row_widget = group_page.comparisons_table.cellWidget(0, 0) row_widget.child_button.clicked.emit() - comp_page = group_page.full_tree.currentItem().widget + qtbot.wait_until(lambda: isinstance(group_page.full_tree.current_widget, + ComparisonPage)) + comp_page = group_page.full_tree.current_widget old_comp = comp_page.data - assert isinstance(comp_page, ComparisonPage) new_idxs = get_different_combo_options(comp_page.specific_combo) monkeypatch.setattr(QtWidgets.QMessageBox, 'question', diff --git a/atef/widgets/config/utils.py b/atef/widgets/config/utils.py index 6c085162..efc85dae 100644 --- a/atef/widgets/config/utils.py +++ b/atef/widgets/config/utils.py @@ -809,13 +809,12 @@ def get_relevant_pvs( Parameters ---------- - attr : str - The attribute, pvname or other string identifier to compare to. - This can also be 'shared' - config : Configuration + comp : Comparison + The comparison to gather PVs for + parent : Union[Configuration, ProcedureStep] Typically a DeviceConfiguration, PVConfiguration, or - ToolConfiguration that has the contextual information for - understanding attr. + ToolConfiguration that contains ``comp`` + Returns ------- List[Tuple[str, str]] @@ -1386,7 +1385,23 @@ def replaceChild(self, old_child: TreeItem, new_child: TreeItem) -> None: raise IndexError('old child not found, could not replace') - def takeChildren(self) -> None: + def takeChild(self, idx: int) -> TreeItem: + child = self._children.pop(idx) + # re-assign rows to children + remaining_children = self.takeChildren() + for rchild in remaining_children: + self.addChild(rchild) + + return child + + def insertChild(self, idx: int, child: TreeItem) -> None: + self._children.insert(idx, child) + # re-assign rows to children + remaining_children = self.takeChildren() + for rchild in remaining_children: + self.addChild(rchild) + + def takeChildren(self) -> list[TreeItem]: """ Remove and return this item's children """ diff --git a/atef/widgets/config/window.py b/atef/widgets/config/window.py index 8a8d0e81..2b1db6c9 100644 --- a/atef/widgets/config/window.py +++ b/atef/widgets/config/window.py @@ -334,14 +334,14 @@ def save_as(self, *args, filename: Optional[str] = None, **kwargs): self.set_current_tab_name(filename) current_tree.full_path = filename - def serialize_tree(self, tree: EditTree) -> dict: + def serialize_tree(self, tree: DualTree) -> dict: """ Return the serialized data from a Tree widget. """ try: return serialize( - type(tree.config_file), - tree.config_file, + type(tree.orig_file), + tree.orig_file, ) except Exception: logger.exception('Error serializing file') @@ -352,7 +352,7 @@ def print_dataclass(self, *args, **kwargs): The parameters are open as to accept inputs from any signal. """ - pprint(self.get_current_tree().config_file) + pprint(self.get_current_tree().orig_file) def print_serialized(self, *args, **kwargs): """