From ae720d0be5cdf21167d1c78253cffbccc6fa6b82 Mon Sep 17 00:00:00 2001 From: janezd Date: Thu, 13 Dec 2018 20:38:30 +0100 Subject: [PATCH] OWScatterPlotBase: Add test for hiding labels --- .../tests/test_owprojectionwidget.py | 10 +++ .../visualize/tests/test_owscatterplotbase.py | 69 ++++++++++++++++--- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/Orange/widgets/visualize/tests/test_owprojectionwidget.py b/Orange/widgets/visualize/tests/test_owprojectionwidget.py index 6b19b0819cc..c425ef720dd 100644 --- a/Orange/widgets/visualize/tests/test_owprojectionwidget.py +++ b/Orange/widgets/visualize/tests/test_owprojectionwidget.py @@ -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()) diff --git a/Orange/widgets/visualize/tests/test_owscatterplotbase.py b/Orange/widgets/visualize/tests/test_owscatterplotbase.py index dd6cd626b41..498ee82b3ef 100644 --- a/Orange/widgets/visualize/tests/test_owscatterplotbase.py +++ b/Orange/widgets/visualize/tests/test_owscatterplotbase.py @@ -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 @@ -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 @@ -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]]) @@ -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