Skip to content

Commit 3212e3a

Browse files
committed
Test and Score: Add tests
1 parent 87d7037 commit 3212e3a

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Orange/widgets/evaluate/owtestlearners.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ def _invalidate(self, which=None):
679679
def show_column_chooser(self, pos):
680680
# pylint doesn't know that self.shown_scores is a set, not a Setting
681681
# pylint: disable=unsupported-membership-test
682-
def update(col_name, state):
683-
if state:
682+
def update(col_name, checked):
683+
if checked:
684684
self.shown_scores.add(col_name)
685685
else:
686686
self.shown_scores.remove(col_name)

Orange/widgets/evaluate/tests/test_owtestlearners.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# pylint: disable=missing-docstring
2+
# pylint: disable=protected-access
3+
import unittest
4+
5+
import collections
26
import numpy as np
37

4-
import unittest
8+
from AnyQt.QtWidgets import QMenu
9+
from AnyQt.QtCore import QPoint
510

611
from Orange.data import Table, Domain
712
from Orange.classification import MajorityLearner
@@ -70,6 +75,63 @@ def test_CrossValidationByFeature(self):
7075
self.assertEqual(self.widget.resampling, OWTestLearners.KFold)
7176
self.assertFalse(self.widget.features_combo.isEnabled())
7277

78+
def test_update_shown_columns(self):
79+
w = self.widget #: OWTestLearners
80+
all, shown = "MABDEFG", "ABDF"
81+
header = w.view.horizontalHeader()
82+
w.shown_scores = set(shown)
83+
w.result_model.setHorizontalHeaderLabels(list(all))
84+
w._update_shown_columns()
85+
for i, name in enumerate(all):
86+
self.assertEqual(name == "M" or name in shown,
87+
not header.isSectionHidden(i),
88+
msg="error in section {}({})".format(i, name))
89+
90+
w.shown_scores = set()
91+
w._update_shown_columns()
92+
for i, name in enumerate(all):
93+
self.assertEqual(i == 0,
94+
not header.isSectionHidden(i),
95+
msg="error in section {}({})".format(i, name))
96+
97+
def test_show_column_chooser(self):
98+
w = self.widget #: OWTestLearners
99+
all, shown = "MABDEFG", "ABDF"
100+
header = w.view.horizontalHeader()
101+
w.shown_scores = set(shown)
102+
w.result_model.setHorizontalHeaderLabels(list(all))
103+
w._update_shown_columns()
104+
105+
actions = collections.OrderedDict()
106+
menu_add_action = QMenu.addAction
107+
108+
def addAction(menu, a):
109+
action = menu_add_action(menu, a)
110+
actions[a] = action
111+
return action
112+
113+
def execmenu(*_):
114+
self.assertEqual(list(actions), list(all)[1:])
115+
for name, action in actions.items():
116+
self.assertEqual(action.isChecked(), name in shown)
117+
actions["E"].triggered.emit(True)
118+
self.assertEqual(w.shown_scores, set("ABDEF"))
119+
actions["B"].triggered.emit(False)
120+
self.assertEqual(w.shown_scores, set("ADEF"))
121+
for i, name in enumerate(all):
122+
self.assertEqual(name == "M" or name in "ADEF",
123+
not header.isSectionHidden(i),
124+
msg="error in section {}({})".format(i, name))
125+
126+
# We must patch `QMenu.exec` because the Qt would otherwise (invisibly)
127+
# show the popup and wait for the user.
128+
# Assertions are made within `menuexec` since they check the
129+
# instances of `QAction`, which are invalid (destroyed by Qt?) after
130+
# `menuexec` finishes.
131+
with unittest.mock.patch("AnyQt.QtWidgets.QMenu.addAction", addAction),\
132+
unittest.mock.patch("AnyQt.QtWidgets.QMenu.exec", execmenu):
133+
w.show_column_chooser(QPoint(0, 0))
134+
73135

74136
class TestHelpers(unittest.TestCase):
75137
def test_results_one_vs_rest(self):

0 commit comments

Comments
 (0)