示例#1
0
 def setUp(self):
     self.callback = Mock()
     data = Table("iris")
     values = data.get_column_view(data.domain[0])[0]
     self.min_value = np.min(values)
     self.max_value = np.max(values)
     self.editor = ContinuousVariableEditor(
         self.parent, data.domain[0], self.min_value,
         self.max_value, self.callback
     )
 def test_overflow(self):
     var = ContinuousVariable("var", number_of_decimals=10)
     editor = ContinuousVariableEditor(self.parent, var, -100000, 1,
                                       self.callback)
     self.assertLess(editor._n_decimals, 10)
class TestContinuousVariableEditor(GuiTest):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.parent = QWidget()

    def setUp(self):
        self.callback = Mock()
        data = Table("iris")
        values = data.get_column_view(data.domain[0])[0]
        self.min_value = np.min(values)
        self.max_value = np.max(values)
        self.editor = ContinuousVariableEditor(self.parent, data.domain[0],
                                               self.min_value, self.max_value,
                                               self.callback)

    def test_init(self):
        self.assertEqual(self.editor.value, self.min_value)
        self.assertEqual(self.editor._slider.value(), self.min_value * 10)
        self.assertEqual(self.editor._spin.value(), self.min_value)
        self.callback.assert_not_called()

    def test_edit_slider(self):
        """ Edit slider by user. """
        self.editor._slider.setValue(int(self.max_value * 10))
        self.assertEqual(self.editor.value, self.max_value)
        self.assertEqual(self.editor._slider.value(), self.max_value * 10)
        self.assertEqual(self.editor._spin.value(), self.max_value)
        self.callback.assert_called_once()

        self.callback.reset_mock()
        value = self.min_value + (self.max_value - self.min_value) / 2
        self.editor._slider.setValue(int(value * 10))
        self.assertEqual(self.editor.value, value)
        self.assertEqual(self.editor._slider.value(), value * 10)
        self.assertEqual(self.editor._spin.value(), value)
        self.callback.assert_called_once()

    def test_edit_spin(self):
        """ Edit spin by user. """
        self.editor._spin.setValue(self.max_value)
        self.assertEqual(self.editor.value, self.max_value)
        self.assertEqual(self.editor._slider.value(), self.max_value * 10)
        self.assertEqual(self.editor._spin.value(), self.max_value)
        self.callback.assert_called_once()

        self.callback.reset_mock()
        self.editor._spin.setValue(self.max_value + 1)
        self.assertEqual(self.editor.value, self.max_value + 1)
        self.assertEqual(self.editor._slider.value(), self.max_value * 10)
        self.assertEqual(self.editor._spin.value(), self.max_value + 1)
        self.callback.assert_called_once()

        self.callback.reset_mock()
        value = self.min_value + (self.max_value - self.min_value) / 2
        self.editor._spin.setValue(value)
        self.assertEqual(self.editor.value, value)
        self.assertEqual(self.editor._slider.value(), value * 10)
        self.assertEqual(self.editor._spin.value(), value)
        self.callback.assert_called_once()

    def test_set_value(self):
        """ Programmatically set slider/spin value. """
        self.editor.value = -2
        self.assertEqual(self.editor._slider.value(), self.min_value * 10)
        self.assertEqual(self.editor._spin.value(), -2)
        self.assertEqual(self.editor.value, -2)
        self.callback.assert_called_once()

        self.callback.reset_mock()
        value = self.min_value + (self.max_value - self.min_value) / 4
        self.editor.value = value
        self.assertEqual(self.editor._slider.value(), value * 10)
        self.assertEqual(self.editor._spin.value(), value)
        self.assertEqual(self.editor.value, value)
        self.callback.assert_called_once()

    def test_set_missing_value(self):
        self.editor.value = np.nan
        self.assertEqual(self.editor._slider.value(), self.min_value * 10)
        self.assertFalse(np.isfinite(self.editor._spin.value()))
        self.assertFalse(np.isfinite(self.editor.value))

    def test_missing_values(self):
        var = ContinuousVariable("var")
        self.assertRaises(ValueError, ContinuousVariableEditor, self.parent,
                          var, np.nan, np.nan, Mock())

    def test_overflow(self):
        var = ContinuousVariable("var", number_of_decimals=10)
        editor = ContinuousVariableEditor(self.parent, var, -100000, 1,
                                          self.callback)
        self.assertLess(editor._n_decimals, 10)

    def test_spin_selection_after_init(self):
        edit: QLineEdit = self.editor._spin.lineEdit()
        edit.selectAll()
        self.assertEqual(edit.selectedText(), "")
        self.assertIs(self.editor.focusProxy(), edit.parent())