diff --git a/tests/test_bec_figure.py b/tests/test_bec_figure.py index 07aea329..fb56db22 100644 --- a/tests/test_bec_figure.py +++ b/tests/test_bec_figure.py @@ -5,7 +5,8 @@ from unittest.mock import MagicMock import numpy as np import pytest -from bec_widgets.widgets import BECFigure +from bec_widgets.widgets import BECFigure, BECMotorMap, BECWaveform1D +from bec_widgets.widgets.plots import BECImageShow from .client_mocks import mocked_client @@ -66,6 +67,16 @@ def test_bec_figure_add_remove_plot(bec_figure): assert bec_figure._widgets["widget_2"].config.widget_class == "BECWaveform1D" +def test_add_different_types_of_widgets(bec_figure): + plt = bec_figure.plot("samx", "bpm4i") + im = bec_figure.image("eiger") + motor_map = bec_figure.motor_map("samx", "samy") + + assert plt.__class__ == BECWaveform1D + assert im.__class__ == BECImageShow + assert motor_map.__class__ == BECMotorMap + + def test_access_widgets_access_errors(bec_figure): bec_figure.add_plot(row=0, col=0) diff --git a/tests/test_bec_motor_map.py b/tests/test_bec_motor_map.py index 40ed76ad..1545375a 100644 --- a/tests/test_bec_motor_map.py +++ b/tests/test_bec_motor_map.py @@ -37,3 +37,89 @@ def test_motor_map_get_limits(bec_motor_map): for motor_name, expected_limit in expected_limits.items(): actual_limit = bec_motor_map._get_motor_limit(motor_name) assert actual_limit == expected_limit + + +def test_motor_map_get_init_position(bec_motor_map): + bec_motor_map.set_precision(2) + + motor_map_dev = bec_motor_map.client.device_manager.devices + + expected_positions = { + ("samx", "samx"): motor_map_dev["samx"].read()["samx"]["value"], + ("samy", "samy"): motor_map_dev["samy"].read()["samy"]["value"], + ("aptrx", "aptrx"): motor_map_dev["aptrx"].read()["aptrx"]["value"], + ("aptry", "aptry"): motor_map_dev["aptry"].read()["aptry"]["value"], + } + + for (motor_name, entry), expected_position in expected_positions.items(): + actual_position = bec_motor_map._get_motor_init_position(motor_name, entry, 2) + assert actual_position == expected_position + + +def test_motor_movement_updates_position_and_database(bec_motor_map): + motor_map_dev = bec_motor_map.client.device_manager.devices + + init_positions = { + "samx": [motor_map_dev["samx"].read()["samx"]["value"]], + "samy": [motor_map_dev["samy"].read()["samy"]["value"]], + } + + bec_motor_map.change_motors("samx", "samy") + + assert bec_motor_map.database_buffer["x"] == init_positions["samx"] + assert bec_motor_map.database_buffer["y"] == init_positions["samy"] + + # Simulate motor movement for 'samx' only + new_position_samx = 4.0 + bec_motor_map.on_device_readback({"signals": {"samx": {"value": new_position_samx}}}) + + init_positions["samx"].append(new_position_samx) + init_positions["samy"].append(init_positions["samy"][-1]) + # Verify database update for 'samx' + assert bec_motor_map.database_buffer["x"] == init_positions["samx"] + + # Verify 'samy' retains its last known position + assert bec_motor_map.database_buffer["y"] == init_positions["samy"] + + +def test_scatter_plot_rendering(bec_motor_map): + motor_map_dev = bec_motor_map.client.device_manager.devices + + init_positions = { + "samx": [motor_map_dev["samx"].read()["samx"]["value"]], + "samy": [motor_map_dev["samy"].read()["samy"]["value"]], + } + + bec_motor_map.change_motors("samx", "samy") + + # Simulate motor movement for 'samx' only + new_position_samx = 4.0 + bec_motor_map.on_device_readback({"signals": {"samx": {"value": new_position_samx}}}) + bec_motor_map._update_plot() + + # Get the scatter plot item + scatter_plot_item = bec_motor_map.plot_components["scatter"] + + # Check the scatter plot item properties + assert len(scatter_plot_item.data) > 0, "Scatter plot data is empty" + x_data = scatter_plot_item.data["x"] + y_data = scatter_plot_item.data["y"] + assert x_data[-1] == new_position_samx, "Scatter plot X data not updated correctly" + assert ( + y_data[-1] == init_positions["samy"][-1] + ), "Scatter plot Y data should retain last known position" + + +def test_plot_visualization_consistency(bec_motor_map): + bec_motor_map.change_motors("samx", "samy") + # Simulate updating the plot with new data + bec_motor_map.on_device_readback({"signals": {"samx": {"value": 5}}}) + bec_motor_map.on_device_readback({"signals": {"samy": {"value": 9}}}) + bec_motor_map._update_plot() + + scatter_plot_item = bec_motor_map.plot_components["scatter"] + + # Check if the scatter plot reflects the new data correctly + assert ( + scatter_plot_item.data["x"][-1] == 5 and scatter_plot_item.data["y"][-1] == 9 + ), "Plot not updated correctly with new data"