File: test_slidergraph.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (131 lines) | stat: -rw-r--r-- 4,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from unittest.mock import Mock

import numpy as np
from AnyQt.QtCore import Qt

from Orange.widgets import widget
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.utils.slidergraph import SliderGraph


class SimpleWidget(widget.OWWidget):
    name = "Simple widget"

    def __init__(self):
        super().__init__()
        self.plot = SliderGraph(x_axis_label="label1",
                                y_axis_label="label2",
                                callback=lambda x: x)

        self.mainArea.layout().addWidget(self.plot)


class TestSliderGraph(WidgetTest):
    def setUp(self):
        self.data = [np.array([1, 2, 3, 4, 5, 6, 7])]
        self.widget = self.create_widget(SimpleWidget)

    def test_init(self):
        p = self.widget.plot

        # labels set correctly?
        self.assertEqual("label1", p.getAxis("bottom").labelText)
        self.assertEqual("label2", p.getAxis("left").labelText)

        # pylint: disable=protected-access
        self.assertIsNone(p._line)
        self.assertIsNone(p.sequences)
        self.assertIsNone(p.x)
        self.assertIsNone(p.selection_limit)
        self.assertIsNone(p.data_increasing)

        self.assertListEqual([], p.plot_horlabel)
        self.assertListEqual([], p.plot_horline)

    def test_plot(self):
        p = self.widget.plot
        x = np.arange(len(self.data[0]))
        p.update(x, self.data, [Qt.red],
                 cutpoint_x=1)

        # labels set correctly?
        self.assertEqual("label1", p.getAxis("bottom").labelText)
        self.assertEqual("label2", p.getAxis("left").labelText)

        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)
        np.testing.assert_array_equal(self.data, p.sequences)
        np.testing.assert_array_equal(x, p.x)
        self.assertTrue(p.data_increasing)

        self.assertEqual(len(p.plot_horlabel), 1)
        self.assertEqual(len(p.plot_horline), 1)
        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)

    def test_plot_selection_limit(self):
        p = self.widget.plot
        x = np.arange(len(self.data[0]))
        p.update(x, self.data, [Qt.red],
                 cutpoint_x=1, selection_limit=(0, 2))

        # labels set correctly?
        self.assertEqual("label1", p.getAxis("bottom").labelText)
        self.assertEqual("label2", p.getAxis("left").labelText)

        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)
        np.testing.assert_array_equal(self.data, p.sequences)
        np.testing.assert_array_equal(x, p.x)
        self.assertTrue(p.data_increasing)
        self.assertTupleEqual((0, 2), p.selection_limit)
        # pylint: disable=protected-access
        self.assertEqual((0, 2), p._line.maxRange)

        self.assertEqual(len(p.plot_horlabel), 1)
        self.assertEqual(len(p.plot_horline), 1)
        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)

    def test_plot_no_cutpoint(self):
        """
        When no cutpoint provided there must be no cutpoint plotted.
        """
        p = self.widget.plot
        x = np.arange(len(self.data[0]))
        p.update(x, self.data, [Qt.red])

        # pylint: disable=protected-access
        self.assertIsNone(p._line)

        # then it is set
        p.update(x, self.data, [Qt.red], cutpoint_x=1)
        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)

        # and re-ploted without cutpoint again
        p.update(x, self.data, [Qt.red])
        # pylint: disable=protected-access
        self.assertIsNone(p._line)

    def test_hover_event(self):
        p = self.widget.plot
        x = np.arange(len(self.data[0]))
        p.update(x, self.data, [Qt.red], cutpoint_x=1)

        # pylint: disable=protected-access
        self.assertIsNotNone(p._line)

        enter_event = Mock()
        enter_event.isEnter.return_value = True
        enter_event.isExit.return_value = False

        p._line.hoverEvent(enter_event)
        self.assertEqual(p._line.pen, p._line._highlight_pen)

        exit_event = Mock()
        exit_event.isEnter.return_value = False
        exit_event.isExit.return_value = True

        p._line.hoverEvent(exit_event)
        self.assertEqual(p._line.pen, p._line._normal_pen)