Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhroom committed Nov 7, 2024
1 parent 9348eb1 commit d65e6b1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
4 changes: 2 additions & 2 deletions rascal2/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rascal2.widgets.controls import ControlsWidget
from rascal2.widgets.inputs import AdaptiveDoubleSpinBox, get_validated_input
from rascal2.widgets.inputs import AdaptiveDoubleSpinBox, get_validated_input, MultiSelectComboBox
from rascal2.widgets.terminal import TerminalWidget

__all__ = ["ControlsWidget", "AdaptiveDoubleSpinBox", "get_validated_input", "TerminalWidget"]
__all__ = ["ControlsWidget", "AdaptiveDoubleSpinBox", "get_validated_input", "TerminalWidget", "MulitSelectComboBox"]
6 changes: 3 additions & 3 deletions rascal2/widgets/project/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ def edit(self):
self.table.setIndexWidget(self.model.index(i, 0), None)


class ContrastsModel(ClassListModel):
"""Classlist model for Contrasts."""
class DomainsModel(ClassListModel):
"""Classlist model for domain contrasts."""

def flags(self, index):
flags = super().flags(index)
Expand All @@ -311,7 +311,7 @@ def flags(self, index):
class DomainContrastWidget(ProjectFieldWidget):
"""Subclass of field widgets for domain contrasts."""

classlist_model = ContrastsModel
classlist_model = DomainsModel

def __init__(self, field, parent):
super().__init__(field, parent)
Expand Down
31 changes: 30 additions & 1 deletion tests/widgets/project/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
ParameterFieldWidget,
ParametersModel,
ProjectFieldWidget,
DomainsModel,
DomainContrastWidget
)


Expand All @@ -32,7 +34,7 @@ class DataModel(pydantic.BaseModel, validate_assignment=True):
@pytest.fixture
def classlist():
"""A test ClassList."""
return RATapi.ClassList([DataModel(name="A", value=1), DataModel(name="B", value=6), DataModel(name="C", value=18)])
return RATapi.ClassList([DataModel(name="A", value=1), DataModel(name="B", value=6), DataModel(name="C", value=18),])


@pytest.fixture
Expand All @@ -54,6 +56,15 @@ def _classlist(protected_indices):
return _classlist


@pytest.fixture
def domains_classlist():
return RATapi.ClassList([
RATapi.models.DomainContrast(name="A", model=["LA"]),
RATapi.models.DomainContrast(name="B", model=["LB", "LB2", "LB3"]),
RATapi.models.DomainContrast(name="C", model=["LC", "LC2"]),
])


@pytest.fixture
def param_model(param_classlist):
def _param_model(protected_indices):
Expand Down Expand Up @@ -260,3 +271,21 @@ def test_hidden_bayesian_columns(param_classlist):
for item in bayesian_columns:
index = widget.model.headers.index(item)
assert not widget.table.isColumnHidden(index + 1)


@pytest.mark.parametrize("edit_mode", [True, False])
def test_domains_model_flags(edit_mode, domains_classlist):
"""Test that the DomainsModel flags are set correctly."""
model = DomainsModel(domains_classlist, parent)
model.edit_mode = edit_mode
for row in [0, 1, 2]:
for column in [1, 2]:
assert bool(model.flags(model.index(row, column)) & QtCore.Qt.ItemFlag.ItemIsEditable) == edit_mode


def test_domains_widget_item_delegates(domains_classlist):
"""Test that the domains widget has the expected item delegates."""
widget = DomainContrastWidget("Test", parent)
widget.update_model(domains_classlist)
assert isinstance(widget.table.itemDelegateForColumn(1), delegates.ValidatedInputDelegate)
assert isinstance(widget.table.itemDelegateForColumn(2), delegates.MultiSelectLayerDelegate)
20 changes: 19 additions & 1 deletion tests/widgets/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic.fields import FieldInfo
from PyQt6 import QtWidgets

from rascal2.widgets import AdaptiveDoubleSpinBox, get_validated_input
from rascal2.widgets import AdaptiveDoubleSpinBox, get_validated_input, MultiSelectComboBox


class MyEnum(StrEnum):
Expand Down Expand Up @@ -39,3 +39,21 @@ def test_adaptive_spinbox(value, decimals):
spinbox = AdaptiveDoubleSpinBox()
spinbox.validate(value, 0)
assert spinbox.decimals() == decimals


@pytest.mark.parametrize("selected", ([], [1], [0, 2]))
def test_multi_select_update(selected):
"""Test that the selected data updates correctly."""
combobox = MultiSelectComboBox()
assert combobox.lineEdit().text() == ""
assert combobox.selected_items() == []
items = ["A", "B", "C"]
combobox.addItems(items)

combobox.select_indices(selected)
expected_items = [items[i] for i in selected]
assert combobox.selected_items() == expected_items
assert combobox.lineEdit().text() == ", ".join(expected_items)



0 comments on commit d65e6b1

Please sign in to comment.