|
1 | 1 | # pylint: disable=missing-docstring |
| 2 | +# pylint: disable=protected-access |
| 3 | +import unittest |
| 4 | + |
| 5 | +import collections |
2 | 6 | import numpy as np |
3 | 7 |
|
4 | | -import unittest |
| 8 | +from AnyQt.QtWidgets import QMenu |
| 9 | +from AnyQt.QtCore import QPoint |
5 | 10 |
|
6 | 11 | from Orange.data import Table, Domain |
7 | 12 | from Orange.classification import MajorityLearner |
@@ -70,6 +75,63 @@ def test_CrossValidationByFeature(self): |
70 | 75 | self.assertEqual(self.widget.resampling, OWTestLearners.KFold) |
71 | 76 | self.assertFalse(self.widget.features_combo.isEnabled()) |
72 | 77 |
|
| 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 | + |
73 | 135 |
|
74 | 136 | class TestHelpers(unittest.TestCase): |
75 | 137 | def test_results_one_vs_rest(self): |
|
0 commit comments