|
| 1 | +"""Tests for PacketGeneralGetDeviceIdAck""" |
| 2 | +# pylint: disable=missing-function-docstring |
| 3 | +# test names are self-explanatory, docstrings would only restate them |
| 4 | + |
| 5 | +from science_mode_4.general.general_device_id import PacketGeneralGetDeviceIdAck |
| 6 | +from science_mode_4.protocol.types import ResultAndError |
| 7 | + |
| 8 | + |
| 9 | +def _build_data(result_error: int, device_id: str) -> bytes: |
| 10 | + return bytes([result_error]) + device_id.encode().ljust(10, b"\x00") |
| 11 | + |
| 12 | + |
| 13 | +def test_device_id_strips_null_padding(): |
| 14 | + # regression test: data[1:11].decode() used to include the null padding bytes |
| 15 | + # verbatim, so a device id shorter than 10 chars came back with a trailing "\x00" |
| 16 | + ack = PacketGeneralGetDeviceIdAck(_build_data(0, "220824001")) |
| 17 | + assert ack.device_id == "220824001" |
| 18 | + assert "\x00" not in ack.device_id |
| 19 | + |
| 20 | + |
| 21 | +def test_device_id_uses_full_field_when_not_null_terminated(): |
| 22 | + ack = PacketGeneralGetDeviceIdAck(_build_data(0, "1234567890")) |
| 23 | + assert ack.device_id == "1234567890" |
| 24 | + |
| 25 | + |
| 26 | +def test_result_error_is_parsed(): |
| 27 | + ack = PacketGeneralGetDeviceIdAck(_build_data(1, "220824001")) |
| 28 | + assert ack.result_error == ResultAndError.TRANSFER_ERROR |
| 29 | + |
| 30 | + |
| 31 | +def test_device_id_defaults_to_empty_string_without_data(): |
| 32 | + ack = PacketGeneralGetDeviceIdAck(None) |
| 33 | + assert ack.device_id == "" |
| 34 | + assert ack.result_error == ResultAndError.NO_ERROR |
0 commit comments