|
1 | 1 | # Test methods with long descriptive names can omit docstrings |
2 | 2 | # pylint: disable=missing-docstring |
| 3 | +from Orange.data import Table, Domain, DiscreteVariable |
| 4 | +from Orange.classification.tree import TreeLearner as ClassificationTreeLearner |
3 | 5 | from Orange.base import Model |
4 | 6 | from Orange.widgets.classify.owclassificationtree import OWTreeLearner |
5 | 7 | from Orange.widgets.tests.base import (WidgetTest, DefaultParameterMapping, |
6 | 8 | ParameterMapping, WidgetLearnerTestMixin) |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class TestOWClassificationTree(WidgetTest, WidgetLearnerTestMixin): |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + super().setUpClass() |
| 15 | + cls.iris = Table("iris") |
| 16 | + |
10 | 17 | def setUp(self): |
11 | 18 | self.widget = self.create_widget( |
12 | 19 | OWTreeLearner, stored_settings={"auto_apply": False}) |
@@ -34,3 +41,50 @@ def test_parameters_unchecked(self): |
34 | 41 | for par, val in zip(self.parameters, (None, 2, 1))] |
35 | 42 | self.test_parameters() |
36 | 43 |
|
| 44 | + def test_cannot_binarize(self): |
| 45 | + widget = self.widget |
| 46 | + error_shown = widget.Error.cannot_binarize.is_shown |
| 47 | + self.assertFalse(error_shown()) |
| 48 | + self.send_signal("Data", self.iris) |
| 49 | + |
| 50 | + # The widget outputs ClassificationTreeLearner. |
| 51 | + # If not, below tests may not make sense |
| 52 | + learner = self.get_output("Learner") |
| 53 | + dlearner = learner.get_learner(learner.CLASSIFICATION) |
| 54 | + self.assertTrue(dlearner, ClassificationTreeLearner) |
| 55 | + |
| 56 | + # No error on Iris |
| 57 | + max_binarization = dlearner.MAX_BINARIZATION |
| 58 | + self.assertFalse(error_shown()) |
| 59 | + |
| 60 | + # Error when too many values |
| 61 | + domain = Domain([ |
| 62 | + DiscreteVariable( |
| 63 | + values=[str(x) for x in range(max_binarization + 1)])], |
| 64 | + DiscreteVariable(values="01")) |
| 65 | + self.send_signal("Data", Table(domain, [[0, 0], [1, 1]])) |
| 66 | + self.assertTrue(error_shown()) |
| 67 | + # No more error on Iris |
| 68 | + self.send_signal("Data", self.iris) |
| 69 | + self.assertFalse(error_shown()) |
| 70 | + |
| 71 | + # Checking and unchecking binarization works |
| 72 | + widget.controls.binary_trees.click() |
| 73 | + self.assertFalse(widget.binary_trees) |
| 74 | + widget.unconditional_apply() |
| 75 | + self.send_signal("Data", Table(domain, [[0, 0], [1, 1]])) |
| 76 | + self.assertFalse(error_shown()) |
| 77 | + widget.controls.binary_trees.click() |
| 78 | + widget.unconditional_apply() |
| 79 | + self.assertTrue(error_shown()) |
| 80 | + widget.controls.binary_trees.click() |
| 81 | + widget.unconditional_apply() |
| 82 | + self.assertFalse(error_shown()) |
| 83 | + |
| 84 | + # If something is wrong with the data, no error appears |
| 85 | + domain = Domain([ |
| 86 | + DiscreteVariable( |
| 87 | + values=[str(x) for x in range(max_binarization + 1)])], |
| 88 | + DiscreteVariable(values="01")) |
| 89 | + self.send_signal("Data", Table(domain)) |
| 90 | + self.assertFalse(error_shown()) |
0 commit comments