Skip to content

Commit

Permalink
OWScatterPlotBase: Add test for hiding labels
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Dec 17, 2018
1 parent 7d07d37 commit ae720d0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Orange/widgets/visualize/tests/test_owprojectionwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,13 @@ def test_saved_selection(self):
self.send_signal(self.widget.Inputs.data, self.data, widget=w)
self.assertEqual(np.sum(w.graph.selection), 15)
np.testing.assert_equal(self.widget.graph.selection, w.graph.selection)

def test_too_many_labels(self):
w = self.widget.Warning.too_many_labels
self.assertFalse(w.is_shown())

self.widget.graph.too_many_labels.emit(True)
self.assertTrue(w.is_shown())

self.widget.graph.too_many_labels.emit(False)
self.assertFalse(w.is_shown())
69 changes: 61 additions & 8 deletions Orange/widgets/visualize/tests/test_owscatterplotbase.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
# pylint: disable=missing-docstring,too-many-lines,too-many-public-methods
from unittest.mock import patch, Mock
import numpy as np

from AnyQt.QtCore import QRectF, Qt
from AnyQt.QtGui import QColor
from AnyQt.QtTest import QSignalSpy

from pyqtgraph import mkPen

Expand Down Expand Up @@ -104,18 +104,21 @@ def test_update_coordinates(self):

def test_update_coordinates_and_labels(self):
graph = self.graph
xy = self.xy = (np.array([1, 2]), np.array([3, 4]))
self.master.get_label_data = lambda: ["a", "b"]
xy = self.xy = (np.array([1., 2]), np.array([3, 4]))
self.master.get_label_data = lambda: np.array(["a", "b"])
graph.reset_graph()
self.assertEqual(graph.labels[0].pos().x(), 1)
xy[0][0] = 0
xy[0][0] = 1.5
graph.update_coordinates()
self.assertEqual(graph.labels[0].pos().x(), 0)
self.assertEqual(graph.labels[0].pos().x(), 1.5)
xy[0][0] = 0 # This label goes out of the range
graph.update_coordinates()
self.assertEqual(graph.labels[0].pos().x(), 2)

def test_update_coordinates_and_density(self):
graph = self.graph
xy = self.xy = (np.array([1, 2]), np.array([3, 4]))
self.master.get_label_data = lambda: ["a", "b"]
self.master.get_label_data = lambda: np.array(["a", "b"])
graph.reset_graph()
self.assertEqual(graph.labels[0].pos().x(), 1)
xy[0][0] = 0
Expand All @@ -127,7 +130,7 @@ def test_update_coordinates_reset_view(self):
graph = self.graph
graph.view_box.setRange = self.setRange
xy = self.xy = (np.array([2, 1]), np.array([3, 10]))
self.master.get_label_data = lambda: ["a", "b"]
self.master.get_label_data = lambda: np.array(["a", "b"])
graph.reset_graph()
self.assertEqual(self.last_setRange, [[1, 2], [3, 10]])

Expand Down Expand Up @@ -969,6 +972,56 @@ def test_unselect_all(self):
graph.update_labels.assert_not_called()
self.master.selection_changed.assert_not_called()

def test_hiding_too_many_labels(self):
spy = QSignalSpy(self.graph.too_many_labels)
self.graph.MAX_VISIBLE_LABELS = 5

graph = self.graph
coords = np.array(
[(x, 0) for x in range(10)], dtype=float).T
self.master.get_coordinates_data = lambda: coords
graph.reset_graph()

self.assertFalse(spy and spy[-1][0])

self.master.get_label_data = lambda: \
np.array([str(x) for x in range(10)], dtype=object)
graph.update_labels()
self.assertTrue(spy[-1][0])
self.assertFalse(bool(self.graph.labels))

graph.view_box.setRange(QRectF(1, -1, 4, 4))
graph.view_box.sigRangeChangedManually.emit(((1, 5), (-1, 3)))
self.assertFalse(spy[-1][0])
self.assertTrue(bool(self.graph.labels))

graph.view_box.setRange(QRectF(1, -1, 8, 8))
graph.view_box.sigRangeChangedManually.emit(((1, 9), (-1, 7)))
self.assertTrue(spy[-1][0])
self.assertFalse(bool(self.graph.labels))

graph.label_only_selected = True
graph.update_labels()
self.assertFalse(spy[-1][0])
self.assertFalse(bool(self.graph.labels))

graph.selection_select([1, 2, 3, 4, 5, 6])
self.assertTrue(spy[-1][0])
self.assertFalse(bool(self.graph.labels))

graph.selection_select([1, 2, 3])
self.assertFalse(spy[-1][0])
self.assertTrue(bool(self.graph.labels))

graph.label_only_selected = False
graph.update_labels()
self.assertTrue(spy[-1][0])
self.assertFalse(bool(self.graph.labels))

graph.clear()
self.assertFalse(spy[-1][0])
self.assertFalse(bool(self.graph.labels))


if __name__ == "__main__":
import unittest
Expand Down

0 comments on commit ae720d0

Please sign in to comment.