feat(sfm): device_commands audit table -- one row per change attempt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
7ff527df07
commit
fb9b48f259
4 files changed
+160
No files matched your search
@@ -0,0 +1,41 @@
|
||||
"""The device_commands audit table: one row per change attempt, pending -> result."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from sfm.audit import DbAudit # noqa: E402
|
||||
from sfm.database import SeismoDb # noqa: E402
|
||||
|
||||
ROW = dict(serial="UM20147", series=4, address="serial:/dev/ttyACM0",
|
||||
action="start monitoring", tier="change",
|
||||
request={"plan": {"steps": [{"sub": "0x96"}]}}, caller="test")
|
||||
|
||||
|
||||
def test_a_command_is_pending_until_finished(tmp_path):
|
||||
db = SeismoDb(tmp_path / "s.db")
|
||||
cid = db.begin_device_command(**ROW)
|
||||
row = db.query_device_commands(serial="UM20147")[0]
|
||||
assert (row["id"], row["outcome"], row["verified"]) == (cid, "pending", None)
|
||||
assert row["request"]["plan"]["steps"][0]["sub"] == "0x96"
|
||||
db.finish_device_command(cid, outcome="sent", verified="ok",
|
||||
ack={"subs": ["0x69"]}, error=None)
|
||||
row = db.query_device_commands(serial="UM20147")[0]
|
||||
assert (row["outcome"], row["verified"], row["ack"]) == ("sent", "ok", {"subs": ["0x69"]})
|
||||
assert row["finished_ts"]
|
||||
|
||||
|
||||
def test_the_table_survives_reopening(tmp_path):
|
||||
SeismoDb(tmp_path / "s.db").begin_device_command(**ROW)
|
||||
assert len(SeismoDb(tmp_path / "s.db").query_device_commands()) == 1
|
||||
|
||||
|
||||
def test_db_audit_is_a_thin_sink(tmp_path):
|
||||
db = SeismoDb(tmp_path / "s.db")
|
||||
audit = DbAudit(lambda: db)
|
||||
cid = audit.begin(**ROW)
|
||||
audit.finish(cid, outcome="failed", verified="mismatch", ack=None, error="unit reads idle")
|
||||
row = db.query_device_commands()[0]
|
||||
assert (row["outcome"], row["verified"], row["error"]) == ("failed", "mismatch", "unit reads idle")
|
||||
Reference in new issue
Block a user