Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
84 lines
3.4 KiB
Python
84 lines
3.4 KiB
Python
"""Core types of sfm.device_access: errors, series selection, models."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
import micromate.protocol as mm_proto # noqa: E402
|
|
import minimateplus.protocol as mmp_proto # noqa: E402
|
|
from sfm.device_access.errors import ( # noqa: E402
|
|
BadRequest, ConnectFailed, DeviceError, MalformedReply, NoAnswer, translate,
|
|
)
|
|
from sfm.device_access.models import UnitAddress # noqa: E402
|
|
from sfm.device_access.series import normalise_serial, series_for_serial # noqa: E402
|
|
|
|
|
|
# ── series ────────────────────────────────────────────────────────────────────
|
|
|
|
@pytest.mark.parametrize(
|
|
"serial, series",
|
|
[("UM20147", 4), ("BE18193", 3), ("BA4521", 3), (" um20147 ", 4), ("be11529", 3)],
|
|
)
|
|
def test_the_prefix_picks_the_series(serial, series):
|
|
assert series_for_serial(serial) == series
|
|
|
|
|
|
def test_serials_are_normalised():
|
|
assert normalise_serial(" um20147\n") == "UM20147"
|
|
|
|
|
|
@pytest.mark.parametrize("serial", ["", "UM", "XX12345", "12345", "MP1234"])
|
|
def test_an_unknown_or_bare_prefix_is_a_bad_request(serial):
|
|
with pytest.raises(BadRequest, match="UM"):
|
|
series_for_serial(serial)
|
|
|
|
|
|
# ── errors ────────────────────────────────────────────────────────────────────
|
|
|
|
@pytest.mark.parametrize(
|
|
"exc, connecting, cls, status",
|
|
[
|
|
(mm_proto.TimeoutError("no frame"), False, NoAnswer, 504),
|
|
(mmp_proto.TimeoutError("no frame"), False, NoAnswer, 504),
|
|
(mm_proto.ChecksumError("bad"), False, MalformedReply, 502),
|
|
(mmp_proto.ProtocolError("bad"), False, MalformedReply, 502),
|
|
(ConnectionRefusedError("refused"), True, ConnectFailed, 503),
|
|
(OSError("reset"), False, ConnectFailed, 503),
|
|
],
|
|
)
|
|
def test_library_exceptions_translate_to_typed_errors(exc, connecting, cls, status):
|
|
out = translate(exc, connecting=connecting)
|
|
assert isinstance(out, cls)
|
|
assert out.http_status == status
|
|
|
|
|
|
def test_connect_and_mid_session_failures_read_differently():
|
|
assert "could not connect" in str(translate(OSError("x"), connecting=True))
|
|
assert "connection lost" in str(translate(OSError("x"), connecting=False))
|
|
|
|
|
|
def test_a_device_error_passes_through_unchanged():
|
|
e = NoAnswer("quiet")
|
|
assert translate(e, connecting=False) is e
|
|
|
|
|
|
def test_anything_else_becomes_a_plain_device_error():
|
|
out = translate(ValueError("boom"), connecting=False)
|
|
assert type(out) is DeviceError and out.http_status == 500
|
|
|
|
|
|
# ── models ────────────────────────────────────────────────────────────────────
|
|
|
|
def test_an_address_needs_a_host_or_a_port():
|
|
with pytest.raises(BadRequest):
|
|
UnitAddress(host=None, tcp_port=None, port=None, baud=None)
|
|
|
|
|
|
def test_the_lock_key_names_the_physical_link():
|
|
assert UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None).key == "tcp:10.0.0.5:9034"
|
|
assert UnitAddress(host=None, tcp_port=None, port="/dev/ttyACM0", baud=115200).key == "serial:/dev/ttyACM0"
|