From a6994b7166b29c00c74c4c5d562123dd19c20f4c Mon Sep 17 00:00:00 2001 From: wakonig_k Date: Thu, 16 Jan 2025 17:39:05 +0100 Subject: [PATCH] tests(scan_assembler): added test for splitting scan args into request inputs --- .../tests_scan_server/test_scan_assembler.py | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 bec_server/tests/tests_scan_server/test_scan_assembler.py diff --git a/bec_server/tests/tests_scan_server/test_scan_assembler.py b/bec_server/tests/tests_scan_server/test_scan_assembler.py new file mode 100644 index 00000000..30e53f63 --- /dev/null +++ b/bec_server/tests/tests_scan_server/test_scan_assembler.py @@ -0,0 +1,121 @@ +from unittest import mock + +import pytest + +from bec_lib import messages +from bec_server.scan_server.scan_assembler import ScanAssembler +from bec_server.scan_server.scans import FermatSpiralScan, LineScan + + +@pytest.fixture +def scan_assembler(): + return ScanAssembler(parent=mock.MagicMock()) + + +@pytest.mark.parametrize( + "msg, request_inputs_expected", + [ + ( + # Fermat scan with args and kwargs, matching the FermatSpiralScan signature + messages.ScanQueueMessage( + scan_type="fermat_scan", + parameter={"args": {"samx": (-5, 5), "samy": (-5, 5)}, "kwargs": {"steps": 3}}, + queue="primary", + ), + { + "arg_bundle": [], + "inputs": { + "motor1": "samx", + "start_motor1": -5, + "stop_motor1": 5, + "motor2": "samy", + "start_motor2": -5, + "stop_motor2": 5, + }, + "kwargs": {"steps": 3}, + }, + ), + ( + # Fermat scan with no args; everything is in kwargs + messages.ScanQueueMessage( + scan_type="fermat_scan", + parameter={ + "args": [], + "kwargs": { + "motor1": "samx", + "start_motor1": -5, + "stop_motor1": 5, + "motor2": "samy", + "start_motor2": -5, + "stop_motor2": 5, + "steps": 3, + }, + }, + queue="primary", + ), + { + "arg_bundle": [], + "inputs": { + "motor1": "samx", + "start_motor1": -5, + "stop_motor1": 5, + "motor2": "samy", + "start_motor2": -5, + "stop_motor2": 5, + }, + "kwargs": {"steps": 3}, + }, + ), + ( + # Fermat scan with mixed args and kwargs + messages.ScanQueueMessage( + scan_type="fermat_scan", + parameter={ + "args": ["samx"], + "kwargs": { + "start_motor1": -5, + "stop_motor1": 5, + "motor2": "samy", + "start_motor2": -5, + "stop_motor2": 5, + "steps": 3, + }, + }, + queue="primary", + ), + { + "arg_bundle": [], + "inputs": { + "motor1": "samx", + "start_motor1": -5, + "stop_motor1": 5, + "motor2": "samy", + "start_motor2": -5, + "stop_motor2": 5, + }, + "kwargs": {"steps": 3}, + }, + ), + ( + # Line scan with arg bundle + messages.ScanQueueMessage( + scan_type="line_scan", + parameter={"args": {"samx": (-5, 5), "samy": (-5, 5)}, "kwargs": {"steps": 3}}, + queue="primary", + ), + {"arg_bundle": ["samx", -5, 5, "samy", -5, 5], "inputs": {}, "kwargs": {"steps": 3}}, + ), + ], +) +def test_scan_assembler_request_inputs(msg, request_inputs_expected, scan_assembler): + + class MockScanManager: + available_scans = { + "fermat_scan": {"class": "FermatSpiralScan"}, + "line_scan": {"class": "LineScan"}, + } + scan_dict = {"FermatSpiralScan": FermatSpiralScan, "LineScan": LineScan} + + with mock.patch.object(scan_assembler, "scan_manager", MockScanManager()): + request = scan_assembler.assemble_device_instructions(msg, "scan_id") + assert request.request_inputs == request_inputs_expected