Skip to content

Commit

Permalink
TST/MNT: fix up tests, other legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed Nov 15, 2023
1 parent 852b578 commit c9533c1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
33 changes: 21 additions & 12 deletions atef/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions atef/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions atef/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand Down
29 changes: 22 additions & 7 deletions atef/widgets/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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
"""
Expand Down
8 changes: 4 additions & 4 deletions atef/widgets/config/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
"""
Expand Down

0 comments on commit c9533c1

Please sign in to comment.