"""DeviceAccess.change_settings (phase 5): the series-independent entry point -- what it refuses before any I/O, and that it dispatches a change (never retried).""" from __future__ import annotations import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from device_access_fakes import FakeMiniMateClient, FakeMicromateClient # noqa: E402 from sfm.device_access.access import DeviceAccess # noqa: E402 from sfm.device_access.errors import BadRequest, NotSupported # noqa: E402 from sfm.device_access.models import UnitAddress # noqa: E402 ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None) class Audit: def __init__(self): self.rows = {} def begin(self, **f): cid = f"c{len(self.rows) + 1}" self.rows[cid] = dict(f, outcome="pending") return cid def finish(self, cid, **f): self.rows[cid].update(f) def _never(*a, **kw): raise AssertionError("no connection may be opened") def _access(s3=_never, s4=_never, audit="default"): return DeviceAccess(s3_client_factory=s3, s4_client_factory=s4, start_reaper=False, audit=Audit() if audit == "default" else audit, sleep=lambda s: None) def test_live_change_without_revision_is_refused_before_io(): with pytest.raises(BadRequest, match="preview first"): _access().change_settings(ADDR, "UM20147", changes={"job_number": 7}, revision=None) @pytest.mark.parametrize("changes", [{}, None, "job_number=7"]) def test_empty_changes_is_a_bad_request(changes): with pytest.raises(BadRequest): _access().change_settings(ADDR, "BE18193", changes=changes, revision="r", dry_run=True) def test_a_live_change_without_an_audit_log_is_refused(): with pytest.raises(NotSupported, match="audit"): _access(audit=None).change_settings(ADDR, "BE18193", changes={"job_number": 7}, revision="r") def test_change_settings_dispatches_by_series_with_change_true(monkeypatch): access = _access(s3=lambda a, **kw: FakeMiniMateClient(), s4=lambda a, **kw: FakeMicromateClient()) seen = [] real = access._dispatch def spy(address, serial, s3_op, s4_op, *, change=False): seen.append((serial, change)) return real(address, serial, s3_op, s4_op, change=change) monkeypatch.setattr(access, "_dispatch", spy) r3 = access.change_settings(ADDR, "be18193", changes={"job_number": 7}, revision=None, dry_run=True) r4 = access.change_settings(ADDR, "UM20147", changes={"job_number": 7}, revision=None, dry_run=True) assert (r3.series, r4.series) == (3, 4) and r3.outcome == r4.outcome == "dry_run" assert seen == [("BE18193", True), ("UM20147", True)]