|
| 1 | +# |
| 2 | +# Copyright 2026-present ifm electronic, gmbh |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | + |
| 6 | +import pytest |
| 7 | +from ifm3dpy.device import PCICCommand, Parameter, SetTemporaryApplicationParameter |
| 8 | + |
| 9 | + |
| 10 | +def _command_from_payload(payload): |
| 11 | + class Command(PCICCommand): |
| 12 | + def serialize_data(self): |
| 13 | + return payload |
| 14 | + |
| 15 | + return Command() |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "payload", |
| 20 | + [ |
| 21 | + [0x66, 0x30, 0x31], |
| 22 | + (0x66, 0x30, 0x31), |
| 23 | + bytearray(b"f01"), |
| 24 | + ], |
| 25 | +) |
| 26 | +def test_pcic_command_python_override_payload_types(payload): |
| 27 | + command = _command_from_payload(payload) |
| 28 | + assert PCICCommand.serialize_data(command) == [0x66, 0x30, 0x31] |
| 29 | + |
| 30 | + |
| 31 | +def test_pcic_command_override_bytes_payload_rejected(): |
| 32 | + command = _command_from_payload(b"f01") |
| 33 | + with pytest.raises((RuntimeError, TypeError)): |
| 34 | + PCICCommand.serialize_data(command) |
| 35 | + |
| 36 | + |
| 37 | +def test_pcic_command_override_invalid_payload_raises_type_error(): |
| 38 | + command = _command_from_payload("f01") |
| 39 | + with pytest.raises((RuntimeError, TypeError)): |
| 40 | + PCICCommand.serialize_data(command) |
| 41 | + |
| 42 | + |
| 43 | +def test_pcic_command_without_override_raises(): |
| 44 | + class IncompleteCommand(PCICCommand): |
| 45 | + pass |
| 46 | + |
| 47 | + command = IncompleteCommand() |
| 48 | + with pytest.raises(RuntimeError): |
| 49 | + PCICCommand.serialize_data(command) |
| 50 | + |
| 51 | + |
| 52 | +def test_set_temporary_application_parameter_serialization_layout(): |
| 53 | + payload = [0x01, 0x02, 0x03] |
| 54 | + command = SetTemporaryApplicationParameter(Parameter.ODS_MOTION_DATA, |
| 55 | + payload) |
| 56 | + |
| 57 | + serialized = PCICCommand.serialize_data(command) |
| 58 | + |
| 59 | + # Layout: 'f' + 5-digit parameter + '#00000' + payload bytes. |
| 60 | + assert bytes(serialized[:12]).decode("ascii") == "f02103#00000" |
| 61 | + assert serialized[12:] == payload |
| 62 | + |
| 63 | + |
| 64 | +def test_set_temporary_application_parameter_ods_motion_data_prefix_encoding(): |
| 65 | + command = SetTemporaryApplicationParameter(Parameter.ODS_MOTION_DATA, |
| 66 | + [0xAA, 0xBB]) |
| 67 | + serialized = PCICCommand.serialize_data(command) |
| 68 | + |
| 69 | + assert bytes(serialized[:12]).decode("ascii") == "f02103#00000" |
| 70 | + assert serialized[-2:] == [0xAA, 0xBB] |
0 commit comments