Skip to content

Commit

Permalink
Color gradient selection: Tests for centering
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed May 29, 2020
1 parent 6613114 commit dae3822
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
43 changes: 42 additions & 1 deletion Orange/widgets/utils/tests/test_colorgradientselection.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from unittest.mock import Mock

import numpy as np

from AnyQt.QtTest import QSignalSpy
from AnyQt.QtCore import Qt, QStringListModel
from AnyQt.QtCore import Qt, QStringListModel, QModelIndex

from Orange.widgets.utils import itemmodels
from Orange.widgets.utils.colorgradientselection import ColorGradientSelection
from Orange.widgets.tests.base import GuiTest


class TestColorGradientSelection(GuiTest):
def test_constructor(self):
w = ColorGradientSelection(thresholds=(0.1, 0.9))
Expand Down Expand Up @@ -68,3 +72,40 @@ def test_slider_move(self):
low, high = changed[-1]
self.assertLessEqual(low, high)
self.assertEqual(high, 0.0)

def test_center(self):
w = ColorGradientSelection(center=42)
self.assertEqual(w.center(), 42)
w.setCenter(40)
self.assertEqual(w.center(), 40)

def test_center_visibility(self):
w = ColorGradientSelection(center=0)
w.center_box.setVisible = Mock()
model = itemmodels.ContinuousPalettesModel()
w.setModel(model)
for row in range(model.rowCount(QModelIndex())):
palette = model.data(model.index(row, 0), Qt.UserRole)
if palette:
if palette.flags & palette.Diverging:
diverging = row
else:
nondiverging = row

w.setCurrentIndex(diverging)
w.center_box.setVisible.assert_called_with(True)
w.setCurrentIndex(nondiverging)
w.center_box.setVisible.assert_called_with(False)
w.setCurrentIndex(diverging)
w.center_box.setVisible.assert_called_with(True)

w = ColorGradientSelection()
self.assertIsNone(w.center_box)

def test_center_changed(self):
w = ColorGradientSelection(center=42)
changed = QSignalSpy(w.centerChanged)
w.center_edit.setText("41")
w.center_edit.editingFinished.emit()
self.assertEqual(w.center(), 41)
self.assertEqual(list(changed), [[41]])
17 changes: 16 additions & 1 deletion Orange/widgets/visualize/tests/test_owheatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=missing-docstring, protected-access
import warnings
import unittest
from unittest.mock import patch
from unittest.mock import patch, Mock

import numpy as np
from sklearn.exceptions import ConvergenceWarning
Expand Down Expand Up @@ -245,6 +245,21 @@ def test_palette_centering(self):
colors = image_row_colors(image)
np.testing.assert_almost_equal(colors, desired)

def test_centering_threshold_change(self):
data = np.arange(2).reshape(-1, 1)
table = Table.from_numpy(Domain([ContinuousVariable("y")]), data)
self.send_signal(self.widget.Inputs.data, table)

cmw = self.widget.color_map_widget
palette_index = cmw.findData(
colorpalettes.ContinuousPalettes["diverging_bwr_40_95_c42"],
Qt.UserRole)
cmw.setCurrentIndex(palette_index)

self.widget.update_color_schema = Mock()
cmw.centerChanged.emit(42)
self.widget.update_color_schema.assert_called()

def test_palette_center(self):
widget = self.widget
model = widget.color_map_widget.model()
Expand Down

0 comments on commit dae3822

Please sign in to comment.