Skip to content

Commit

Permalink
Fix tests for changes, and changes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Feb 12, 2023
1 parent 1116c26 commit 3834f8e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
19 changes: 11 additions & 8 deletions Orange/widgets/data/owcontinuize.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class MethodDesc(NamedTuple):


class ContDomainModel(DomainModel):
FilterRole = Qt.UserRole + 2
HintRole = next(gui.OrangeUserRole)
FilterRole = next(gui.OrangeUserRole)
"""Domain model that adds description of chosen methods"""
def __init__(self, valid_type):
super().__init__(
Expand All @@ -132,13 +133,15 @@ def data(self, index, role=Qt.DisplayRole):
name = super().data(index, Qt.DisplayRole)
if not isinstance(name, str):
return None
hint = index.data(Qt.UserRole)
hint = index.data(self.HintRole)
if hint is None:
return name
return f"{name} {hint[0]}"
value = super().data(index, role)
if role == Qt.DisplayRole and not isinstance(value, LabelledSeparator):
return value, *index.data(Qt.UserRole)
if role == Qt.DisplayRole:
if isinstance(value, LabelledSeparator):
return None
return value, *(index.data(self.HintRole) or ("", False))
return value


Expand Down Expand Up @@ -198,14 +201,14 @@ def __init__(self, *args, **kwargs):

def initStyleOption(self, option, index):
super().initStyleOption(option, index)
hint = index.data(Qt.UserRole)
hint = index.data(ContDomainModel.HintRole)
option.font.setBold(hint is not None and hint[1])

def set_default_hints(self, show):
self._default_hints = show

def displayText(self, value, _):
if isinstance(value, LabelledSeparator):
if value is None:
return None
name, hint, nondefault = value
if self._default_hints or nondefault:
Expand Down Expand Up @@ -454,7 +457,7 @@ def _on_radio_clicked(self, method_id: int):
desc = methods[method_id].short_desc
for index, var in zip(indexes, selvars):
show = method_id != self.default_for_var(var)
model.setData(index, (desc, show), Qt.UserRole)
model.setData(index, (desc, show), model.HintRole)
self.commit.deferred()

@Inputs.data
Expand Down Expand Up @@ -493,7 +496,7 @@ def _set_hints(self):
model.setData(
model.index(i, 0),
(options[method_id].short_desc, nondefault),
Qt.UserRole)
model.HintRole)
hints.clear()
hints.update(filtered)

Expand Down
80 changes: 61 additions & 19 deletions Orange/widgets/data/tests/test_owcontinuize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from Orange.data import Table, DiscreteVariable, ContinuousVariable, Domain
from Orange.widgets.data.owcontinuize import OWContinuize, DefaultKey, \
ContinuousOptions, Normalize, Continuize, DiscreteOptions, ContDomainModel, \
DefaultContModel
DefaultContModel, ListViewSearch
from Orange.widgets.tests.base import WidgetTest
from orangewidget.utils.itemmodels import SeparatedListDelegate


class TestOWContinuize(WidgetTest):
Expand Down Expand Up @@ -254,11 +255,11 @@ def test_set_hints_on_new_data(self):
self.send_signal(w.Inputs.data, data)

model = w.cont_view.model()
self.assertIsNone(model.index(0, 0).data(Qt.UserRole))
self.assertEqual(model.index(1, 0).data(Qt.UserRole),
ContinuousOptions[Normalize.Leave].short_desc)
self.assertEqual(model.index(5, 0).data(Qt.UserRole),
ContinuousOptions[Normalize.Normalize11].short_desc)
self.assertEqual(model.index(0, 0).data(model.HintRole), ("preset", False))
self.assertEqual(model.index(1, 0).data(model.HintRole),
(ContinuousOptions[Normalize.Leave].short_desc, True))
self.assertEqual(model.index(5, 0).data(model.HintRole),
(ContinuousOptions[Normalize.Normalize11].short_desc, True))
self.assertNotIn("x", w.cont_var_hints)

def test_change_hints_disc(self):
Expand All @@ -274,8 +275,8 @@ def test_change_hints_disc(self):

self.send_signal(w.Inputs.data, Table("heart_disease"))
self.assertEqual(
dmod.index(3, 0).data(Qt.UserRole),
DiscreteOptions[Continuize.Remove].short_desc)
dmod.index(3, 0).data(dmod.HintRole),
(DiscreteOptions[Continuize.Remove].short_desc, True))

dselmod.select(dmod.index(1, 0), QItemSelectionModel.ClearAndSelect) # chest pain
dselmod.select(dmod.index(4, 0), QItemSelectionModel.Select) # exerc ind ang
Expand All @@ -295,10 +296,10 @@ def test_change_hints_disc(self):
self.assertFalse("gender" in w.disc_var_hints)
self.assertEqual(w.disc_var_hints["rest ECG"], Continuize.Remove)

self.assertIsNone(dmod.index(0, 0).data(Qt.UserRole))
self.assertEqual(dmod.index(0, 0).data(dmod.HintRole), ("preset", False))
self.assertEqual(
dmod.index(3, 0).data(Qt.UserRole),
DiscreteOptions[Continuize.Remove].short_desc)
dmod.index(3, 0).data(dmod.HintRole),
(DiscreteOptions[Continuize.Remove].short_desc, True))

dview.select_default()
dgroup.button(Continuize.AsOrdinal).setChecked(True)
Expand Down Expand Up @@ -334,10 +335,10 @@ def test_change_hints_cont(self):
self.assertFalse("cholesterol" in w.cont_var_hints)
self.assertEqual(w.cont_var_hints["max HR"], Normalize.Normalize11)

self.assertIsNone(cmod.index(0, 0).data(Qt.UserRole))
self.assertEqual(cmod.index(0, 0).data(cmod.HintRole), ("preset", False))
self.assertEqual(
cmod.index(3, 0).data(Qt.UserRole),
ContinuousOptions[Normalize.Normalize11].short_desc)
cmod.index(3, 0).data(cmod.HintRole),
(ContinuousOptions[Normalize.Normalize11].short_desc, True))

def test_transformations(self):
domain = Domain([DiscreteVariable(c, values="abc")
Expand Down Expand Up @@ -449,24 +450,65 @@ def test_migrate_settings_to_v3(self):

class TestModelsAndViews(GuiTest):
def test_contmodel(self):
domain = Domain([ContinuousVariable(c) for c in "abc"])
domain = Domain([ContinuousVariable(c) for c in "abc"],
ContinuousVariable("y"))
model = ContDomainModel(ContinuousVariable)
model.set_domain(domain)

ind = model.index(0, 0)
self.assertEqual(ind.data()[0], "a")
self.assertEqual(ind.data(model.FilterRole)[0], "a")
self.assertIsNone(ind.data(Qt.ToolTipRole))

ind = model.index(1, 0)
model.setData(ind, "mega encoding", Qt.UserRole)
self.assertEqual(model.index(0, 0).data(), "a")
self.assertEqual(ind.data(), "b: mega encoding")
model.setData(ind, ("mega encoding", True), model.HintRole)
self.assertEqual(ind.data(), ("b", "mega encoding", True))
self.assertEqual(ind.data(model.HintRole), ("mega encoding", True))
self.assertIn("b", ind.data(model.FilterRole))
self.assertIn("mega encoding", ind.data(model.FilterRole))
self.assertNotIn("bmega encoding", ind.data(model.FilterRole))
self.assertIsNone(ind.data(Qt.ToolTipRole))

ind = model.index(3, 0) # separator
self.assertIsNone(ind.data())
self.assertIsNone(ind.data(model.HintRole))
self.assertIsNone(ind.data(model.FilterRole))

def test_defaultcontmodel(self):
model = DefaultContModel()
self.assertEqual(1, model.rowCount(QModelIndex()))
self.assertEqual(1, model.columnCount(QModelIndex()))
ind = model.index(0, 0)
model.setMethod("mega encoding")
self.assertEqual(ind.data(), "Default: mega encoding")
self.assertEqual(ind.data(), "Preset: mega encoding")
self.assertIsNotNone(ind.data(Qt.DecorationRole))
self.assertIsNotNone(ind.data(Qt.ToolTipRole))


class TestListViewDelegate(unittest.TestCase):
def test_displaytext(self):
delegate = ListViewSearch.Delegate()
self.assertEqual(delegate.displayText(("a", "foo", False), Mock()), "a")
self.assertEqual(delegate.displayText(("a", "foo", True), Mock()), "a: foo")
delegate.set_default_hints(True)
self.assertEqual(delegate.displayText(("a", "foo", False), Mock()), "a: foo")
delegate.set_default_hints(False)
self.assertEqual(delegate.displayText(("a", "foo", False), Mock()), "a")

@patch.object(SeparatedListDelegate, "initStyleOption")
def test_bold(self, _):
delegate = ListViewSearch.Delegate()
option = Mock()
index = Mock()
index.data = lambda role: ("foo", True) if role == ContDomainModel.HintRole else None
delegate.initStyleOption(option, index)
option.font.setBold.assert_called_with(True)
index.data = lambda role: ("foo", False) if role == ContDomainModel.HintRole else None
delegate.initStyleOption(option, index)
option.font.setBold.assert_called_with(False)
index.data = lambda role: None if role == ContDomainModel.HintRole else None
delegate.initStyleOption(option, index)
option.font.setBold.assert_called_with(False)

if __name__ == "__main__":
unittest.main()

0 comments on commit 3834f8e

Please sign in to comment.