From 8dc892df0a47ccbdd812555b7c5775a455a23ede Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Fri, 11 Oct 2024 12:17:17 +0200 Subject: [PATCH] tests(plot_base): tests extended --- bec_widgets/widgets/figure/plots/plot_base.py | 1 - tests/unit_tests/test_plot_base.py | 153 ++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) diff --git a/bec_widgets/widgets/figure/plots/plot_base.py b/bec_widgets/widgets/figure/plots/plot_base.py index b226e1a9..c6209147 100644 --- a/bec_widgets/widgets/figure/plots/plot_base.py +++ b/bec_widgets/widgets/figure/plots/plot_base.py @@ -490,7 +490,6 @@ class BECPlotBase(BECConnector, pg.GraphicsLayout): def cleanup_pyqtgraph(self): """Cleanup pyqtgraph items.""" self.unhook_crosshair() - self.unhook_fps_monitor() self.tick_item.cleanup() self.arrow_item.cleanup() item = self.plot_item diff --git a/tests/unit_tests/test_plot_base.py b/tests/unit_tests/test_plot_base.py index ef05dae3..602de510 100644 --- a/tests/unit_tests/test_plot_base.py +++ b/tests/unit_tests/test_plot_base.py @@ -93,3 +93,156 @@ def test_plot_base_axes_added_by_kwargs(qtbot, mocked_client): assert plot_base.config.axis.y_lim == (5, 500) assert plot_base.plot_item.ctrl.logXCheck.isChecked() == True assert plot_base.plot_item.ctrl.logYCheck.isChecked() == True + + +def test_lock_aspect_ratio(qtbot, mocked_client): + """ + Test locking and unlocking the aspect ratio of the plot. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + # Lock the aspect ratio + plot_base.lock_aspect_ratio(True) + assert plot_base.plot_item.vb.state["aspectLocked"] == 1 + + # Unlock the aspect ratio + plot_base.lock_aspect_ratio(False) + assert plot_base.plot_item.vb.state["aspectLocked"] == 0 + + +def test_set_auto_range(qtbot, mocked_client): + """ + Test enabling and disabling auto range for the plot. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + # Enable auto range for both axes + plot_base.set_auto_range(True, axis="xy") + assert plot_base.plot_item.vb.state["autoRange"] == [True, True] + + # Disable auto range for x-axis + plot_base.set_auto_range(False, axis="x") + assert plot_base.plot_item.vb.state["autoRange"] == [False, True] + + # Disable auto range for y-axis + plot_base.set_auto_range(False, axis="y") + assert plot_base.plot_item.vb.state["autoRange"] == [False, False] + + +def test_set_outer_axes(qtbot, mocked_client): + """ + Test showing and hiding the outer axes of the plot. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + # Show outer axes + plot_base.set_outer_axes(True) + assert plot_base.plot_item.getAxis("top").isVisible() + assert plot_base.plot_item.getAxis("right").isVisible() + assert plot_base.config.axis.outer_axes is True + + # Hide outer axes + plot_base.set_outer_axes(False) + assert not plot_base.plot_item.getAxis("top").isVisible() + assert not plot_base.plot_item.getAxis("right").isVisible() + assert plot_base.config.axis.outer_axes is False + + +def test_toggle_crosshair(qtbot, mocked_client): + """ + Test toggling the crosshair on and off. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + # Toggle crosshair on + plot_base.toggle_crosshair() + assert plot_base.crosshair is not None + + # Toggle crosshair off + plot_base.toggle_crosshair() + assert plot_base.crosshair is None + + +def test_invalid_scale_input(qtbot, mocked_client): + """ + Test setting an invalid scale for x and y axes. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + with pytest.raises(ValueError): + plot_base.set_x_scale("invalid_scale") + + with pytest.raises(ValueError): + plot_base.set_y_scale("invalid_scale") + + +def test_set_x_lim_invalid_arguments(qtbot, mocked_client): + """ + Test passing invalid arguments to set_x_lim. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + with pytest.raises(ValueError): + plot_base.set_x_lim(1) + + with pytest.raises(ValueError): + plot_base.set_x_lim((1, 2, 3)) + + +def test_set_y_lim_invalid_arguments(qtbot, mocked_client): + """ + Test passing invalid arguments to set_y_lim. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + with pytest.raises(ValueError): + plot_base.set_y_lim(1) + + with pytest.raises(ValueError): + plot_base.set_y_lim((1, 2, 3)) + + +def test_remove_plot(qtbot, mocked_client): + """ + Test removing the plot widget from the figure. + """ + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + with mock.patch.object(bec_figure, "remove") as mock_remove: + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + plot_base.remove() + mock_remove.assert_called_once_with(widget_id=plot_base.gui_id) + + +def test_add_fps_monitor(qtbot, mocked_client): + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + plot_base.enable_fps_monitor(True) + + assert plot_base.fps_monitor is not None + assert plot_base.fps_monitor.view_box is plot_base.plot_item.getViewBox() + assert plot_base.fps_monitor.timer.isActive() == True + assert plot_base.fps_monitor.timer.interval() == 1000 + assert plot_base.fps_monitor.sigFpsUpdate is not None + assert plot_base.fps_monitor.sigFpsUpdate.connect is not None + + +def test_hook_unhook_fps_monitor(qtbot, mocked_client): + bec_figure = create_widget(qtbot, BECFigure, client=mocked_client) + plot_base = bec_figure.add_widget(widget_type="BECPlotBase", widget_id="test_plot") + + plot_base.enable_fps_monitor(True) + assert plot_base.fps_monitor is not None + + plot_base.enable_fps_monitor(False) + assert plot_base.fps_monitor is None + + plot_base.enable_fps_monitor(True) + assert plot_base.fps_monitor is not None