0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-13 19:21:50 +02:00

tests(plot_base): tests extended

This commit is contained in:
2024-10-11 12:17:17 +02:00
parent 8c5ef26843
commit 8dc892df0a
2 changed files with 153 additions and 1 deletions

View File

@ -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

View File

@ -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