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

test(test_plot_base): BECPlotBase tests added

This commit is contained in:
wyzula-jan
2024-02-23 13:37:25 +01:00
parent f668eb8b9b
commit 826a5e9874
2 changed files with 102 additions and 13 deletions

View File

@ -157,23 +157,51 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.setLogMode(y=(scale == "log"))
self.config.axis.y_scale = scale
def set_x_lim(self, x_lim: tuple) -> None:
def set_x_lim(self, *args) -> None:
"""
Set the limits of the x-axis.
Args:
x_lim(tuple): Limits of the x-axis.
"""
self.setXRange(x_lim[0], x_lim[1])
self.config.axis.x_lim = x_lim
Set the limits of the x-axis. This method can accept either two separate arguments
for the minimum and maximum x-axis values, or a single tuple containing both limits.
Usage:
set_x_lim(x_min, x_max)
set_x_lim((x_min, x_max))
def set_y_lim(self, y_lim: tuple) -> None:
"""
Set the limits of the y-axis.
Args:
y_lim(tuple): Limits of the y-axis.
*args: A variable number of arguments. Can be two integers (x_min and x_max)
or a single tuple with two integers.
"""
self.setYRange(y_lim[0], y_lim[1])
self.config.axis.y_lim = y_lim
if len(args) == 1 and isinstance(args[0], tuple):
x_min, x_max = args[0]
elif len(args) == 2:
x_min, x_max = args
else:
raise ValueError("set_x_lim expects either two separate arguments or a single tuple")
self.setXRange(x_min, x_max)
self.config.axis.x_lim = (x_min, x_max)
def set_y_lim(self, *args) -> None:
"""
Set the limits of the y-axis. This method can accept either two separate arguments
for the minimum and maximum y-axis values, or a single tuple containing both limits.
Usage:
set_y_lim(y_min, y_max)
set_y_lim((y_min, y_max))
Args:
*args: A variable number of arguments. Can be two integers (y_min and y_max)
or a single tuple with two integers.
"""
if len(args) == 1 and isinstance(args[0], tuple):
y_min, y_max = args[0]
elif len(args) == 2:
y_min, y_max = args
else:
raise ValueError("set_y_lim expects either two separate arguments or a single tuple")
self.setYRange(y_min, y_max)
self.config.axis.y_lim = (y_min, y_max)
def set_grid(self, x: bool = False, y: bool = False):
"""

61
tests/test_plot_base.py Normal file
View File

@ -0,0 +1,61 @@
import pytest
from .client_mocks import mocked_client
from .test_bec_figure import bec_figure
def test_init_plot_base(bec_figure):
plot_base = bec_figure.add_widget(widget_type="PlotBase", widget_id="test_plot")
assert plot_base is not None
assert plot_base.config.widget_class == "BECPlotBase"
assert plot_base.config.gui_id == "test_plot"
def test_plot_base_axes_by_separate_methods(bec_figure):
plot_base = bec_figure.add_widget(widget_type="PlotBase", widget_id="test_plot")
plot_base.set_title("Test Title")
plot_base.set_x_label("Test x Label")
plot_base.set_y_label("Test y Label")
plot_base.set_x_lim(1, 100)
plot_base.set_y_lim(5, 500)
plot_base.set_grid(True, True)
plot_base.set_x_scale("log")
plot_base.set_y_scale("log")
assert plot_base.titleLabel.text == "Test Title"
assert plot_base.config.axis.title == "Test Title"
assert plot_base.getAxis("bottom").labelText == "Test x Label"
assert plot_base.config.axis.x_label == "Test x Label"
assert plot_base.getAxis("left").labelText == "Test y Label"
assert plot_base.config.axis.y_label == "Test y Label"
assert plot_base.config.axis.x_lim == (1, 100)
assert plot_base.config.axis.y_lim == (5, 500)
assert plot_base.ctrl.xGridCheck.isChecked() == True
assert plot_base.ctrl.yGridCheck.isChecked() == True
assert plot_base.ctrl.logXCheck.isChecked() == True
assert plot_base.ctrl.logYCheck.isChecked() == True
def test_plot_base_axes_added_by_kwargs(bec_figure):
plot_base = bec_figure.add_widget(widget_type="PlotBase", widget_id="test_plot")
plot_base.set(
title="Test Title",
x_label="Test x Label",
y_label="Test y Label",
x_lim=(1, 100),
y_lim=(5, 500),
x_scale="log",
y_scale="log",
)
assert plot_base.titleLabel.text == "Test Title"
assert plot_base.config.axis.title == "Test Title"
assert plot_base.getAxis("bottom").labelText == "Test x Label"
assert plot_base.config.axis.x_label == "Test x Label"
assert plot_base.getAxis("left").labelText == "Test y Label"
assert plot_base.config.axis.y_label == "Test y Label"
assert plot_base.config.axis.x_lim == (1, 100)
assert plot_base.config.axis.y_lim == (5, 500)
assert plot_base.ctrl.logXCheck.isChecked() == True
assert plot_base.ctrl.logYCheck.isChecked() == True