mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
test: add tests
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import
|
# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from qtpy.QtGui import QFontInfo
|
||||||
|
|
||||||
from .client_mocks import mocked_client
|
from .client_mocks import mocked_client
|
||||||
from .test_bec_figure import bec_figure
|
from .test_bec_figure import bec_figure
|
||||||
@ -37,6 +40,30 @@ def test_plot_base_axes_by_separate_methods(bec_figure):
|
|||||||
assert plot_base.plot_item.ctrl.logXCheck.isChecked() == True
|
assert plot_base.plot_item.ctrl.logXCheck.isChecked() == True
|
||||||
assert plot_base.plot_item.ctrl.logYCheck.isChecked() == True
|
assert plot_base.plot_item.ctrl.logYCheck.isChecked() == True
|
||||||
|
|
||||||
|
# Check the font size by mocking the set functions
|
||||||
|
# I struggled retrieving it from the QFont object directly
|
||||||
|
# thus I mocked the set functions to check internally the functionality
|
||||||
|
with (
|
||||||
|
mock.patch.object(plot_base.plot_item, "setLabel") as mock_set_label,
|
||||||
|
mock.patch.object(plot_base.plot_item, "setTitle") as mock_set_title,
|
||||||
|
):
|
||||||
|
plot_base.set_x_label("Test x Label", 20)
|
||||||
|
plot_base.set_y_label("Test y Label", 16)
|
||||||
|
assert mock_set_label.call_count == 2
|
||||||
|
assert plot_base.config.axis.x_label_size == 20
|
||||||
|
assert plot_base.config.axis.y_label_size == 16
|
||||||
|
col = plot_base.get_text_color()
|
||||||
|
calls = []
|
||||||
|
style = {"color": col, "font-size": "20pt"}
|
||||||
|
calls.append(mock.call("bottom", "Test x Label", **style))
|
||||||
|
style = {"color": col, "font-size": "16pt"}
|
||||||
|
calls.append(mock.call("left", "Test y Label", **style))
|
||||||
|
assert mock_set_label.call_args_list == calls
|
||||||
|
plot_base.set_title("Test Title", 16)
|
||||||
|
style = {"color": col, "size": "16pt"}
|
||||||
|
call = mock.call("Test Title", **style)
|
||||||
|
assert mock_set_title.call_args == call
|
||||||
|
|
||||||
|
|
||||||
def test_plot_base_axes_added_by_kwargs(bec_figure):
|
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 = bec_figure.add_widget(widget_type="PlotBase", widget_id="test_plot")
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import
|
# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import
|
||||||
from unittest.mock import MagicMock
|
from unittest import mock
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pytest
|
import pytest
|
||||||
@ -56,8 +56,12 @@ def test_create_waveform1D_by_config(bec_figure):
|
|||||||
"col": 0,
|
"col": 0,
|
||||||
"axis": {
|
"axis": {
|
||||||
"title": "Widget 1",
|
"title": "Widget 1",
|
||||||
|
"title_size": None,
|
||||||
"x_label": None,
|
"x_label": None,
|
||||||
|
"x_label_size": None,
|
||||||
"y_label": None,
|
"y_label": None,
|
||||||
|
"y_label_size": None,
|
||||||
|
"legend_label_size": None,
|
||||||
"x_scale": "linear",
|
"x_scale": "linear",
|
||||||
"y_scale": "linear",
|
"y_scale": "linear",
|
||||||
"x_lim": (1, 10),
|
"x_lim": (1, 10),
|
||||||
@ -193,6 +197,19 @@ def test_add_curve(bec_figure):
|
|||||||
assert c1.config.source == "scan_segment"
|
assert c1.config.source == "scan_segment"
|
||||||
|
|
||||||
|
|
||||||
|
def test_change_legend_font_size(bec_figure):
|
||||||
|
plot = bec_figure.add_plot()
|
||||||
|
|
||||||
|
w1 = plot.add_curve_scan(x_name="samx", y_name="bpm4i")
|
||||||
|
my_func = plot.plot_item.legend.items[0][1]
|
||||||
|
with mock.patch.object(my_func, "setText") as mock_set_text:
|
||||||
|
plot.set_legend_label_size(16)
|
||||||
|
assert plot.config.axis.legend_label_size == 16
|
||||||
|
assert mock_set_text.call_count == 1
|
||||||
|
style = {"color": plot.get_text_color(), "size": "16pt"}
|
||||||
|
assert mock_set_text.call_args == mock.call(my_func.text, **style)
|
||||||
|
|
||||||
|
|
||||||
def test_remove_curve(bec_figure):
|
def test_remove_curve(bec_figure):
|
||||||
w1 = bec_figure.add_plot()
|
w1 = bec_figure.add_plot()
|
||||||
|
|
||||||
@ -406,10 +423,10 @@ def test_scan_update(bec_figure, qtbot):
|
|||||||
"scan_id": 1,
|
"scan_id": 1,
|
||||||
}
|
}
|
||||||
# Mock scan_storage.find_scan_by_ID
|
# Mock scan_storage.find_scan_by_ID
|
||||||
mock_scan_data_waveform = MagicMock()
|
mock_scan_data_waveform = mock.MagicMock()
|
||||||
mock_scan_data_waveform.data = {
|
mock_scan_data_waveform.data = {
|
||||||
device_name: {
|
device_name: {
|
||||||
entry: MagicMock(val=[msg_waveform["data"][device_name][entry]["value"]])
|
entry: mock.MagicMock(val=[msg_waveform["data"][device_name][entry]["value"]])
|
||||||
for entry in msg_waveform["data"][device_name]
|
for entry in msg_waveform["data"][device_name]
|
||||||
}
|
}
|
||||||
for device_name in msg_waveform["data"]
|
for device_name in msg_waveform["data"]
|
||||||
@ -430,12 +447,12 @@ def test_scan_history_with_val_access(bec_figure, qtbot):
|
|||||||
c1 = w1.add_curve_scan(x_name="samx", y_name="bpm4i")
|
c1 = w1.add_curve_scan(x_name="samx", y_name="bpm4i")
|
||||||
|
|
||||||
mock_scan_data = {
|
mock_scan_data = {
|
||||||
"samx": {"samx": MagicMock(val=np.array([1, 2, 3]))}, # Use MagicMock for .val
|
"samx": {"samx": mock.MagicMock(val=np.array([1, 2, 3]))}, # Use mock.MagicMock for .val
|
||||||
"bpm4i": {"bpm4i": MagicMock(val=np.array([4, 5, 6]))}, # Use MagicMock for .val
|
"bpm4i": {"bpm4i": mock.MagicMock(val=np.array([4, 5, 6]))}, # Use mock.MagicMock for .val
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_scan_storage = MagicMock()
|
mock_scan_storage = mock.MagicMock()
|
||||||
mock_scan_storage.find_scan_by_ID.return_value = MagicMock(data=mock_scan_data)
|
mock_scan_storage.find_scan_by_ID.return_value = mock.MagicMock(data=mock_scan_data)
|
||||||
w1.queue.scan_storage = mock_scan_storage
|
w1.queue.scan_storage = mock_scan_storage
|
||||||
|
|
||||||
fake_scan_id = "fake_scan_id"
|
fake_scan_id = "fake_scan_id"
|
||||||
@ -464,10 +481,10 @@ def test_scatter_2d_update(bec_figure, qtbot):
|
|||||||
}
|
}
|
||||||
msg_metadata = {"scan_name": "line_scan"}
|
msg_metadata = {"scan_name": "line_scan"}
|
||||||
|
|
||||||
mock_scan_data = MagicMock()
|
mock_scan_data = mock.MagicMock()
|
||||||
mock_scan_data.data = {
|
mock_scan_data.data = {
|
||||||
device_name: {
|
device_name: {
|
||||||
entry: MagicMock(val=msg["data"][device_name][entry]["value"])
|
entry: mock.MagicMock(val=msg["data"][device_name][entry]["value"])
|
||||||
for entry in msg["data"][device_name]
|
for entry in msg["data"][device_name]
|
||||||
}
|
}
|
||||||
for device_name in msg["data"]
|
for device_name in msg["data"]
|
||||||
|
Reference in New Issue
Block a user