Repository navigation
Expand file tree
/
Copy pathtest_evidence.py
More file actions
71 lines (56 loc) · 2.66 KB
/
Copy pathtest_evidence.py
File metadata and controls
71 lines (56 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""RiskControl evidence export validates against traceability-matrix-dhf's contract."""
import json
import sys
from pathlib import Path
import jsonschema
import pytest
import foremode
from test_golden import SCENARIOS
SCHEMA = json.loads((Path(__file__).parent / "schemas" / "risk-control.schema.json")
.read_text(encoding="utf-8"))
DHF = Path(__file__).resolve().parent.parent / "traceability-matrix-dhf"
def _payload(sid):
sc, _ = foremode.load_scenario(sid)
return sc, foremode.to_risk_records(sc, sid)
@pytest.mark.parametrize("sid", SCENARIOS)
def test_records_validate_against_schema(sid):
sc, p = _payload(sid)
assert p["source"] == "foremode" and p["scenario_id"] == sid
assert len(p["risk_controls"]) == len(sc["failures"])
for r in p["risk_controls"]:
jsonschema.validate(r, SCHEMA)
def test_scale_mapping_and_mitigation():
sc, p = _payload("cnc_femoral_stem")
label, s, o, _ = sc["ratings"][0]
r = p["risk_controls"][0]
assert (r["severity"], r["probability"]) == ((s + 1) // 2, (o + 1) // 2)
assert r["harm"] == sc["failures"][0]["effect"]
assert sc["failures"][0]["prevention"] in r["control_measure"]
assert r["residual_risk_acceptable"] is None
planned = {a["mode"] for a in sc["actions"]}
for (lbl, *_), rec in zip(sc["ratings"], p["risk_controls"]):
assert ("Planned action:" in rec["control_measure"]) == (lbl in planned)
def test_mismatched_rows_refused():
sc, _ = foremode.load_scenario("cnc_femoral_stem")
sc["ratings"] = sc["ratings"][:-1]
with pytest.raises(ValueError):
foremode.to_risk_records(sc, "x")
def test_cli_evidence_flag(tmp_path):
foremode.main(["generate", "sterile_packaging", "--evidence", "-o", str(tmp_path / "pkg")])
p = json.loads((tmp_path / "pkg.risk.json").read_text(encoding="utf-8"))
assert p["source"] == "foremode" and p["risk_controls"]
@pytest.mark.skipif(not DHF.exists() or sys.version_info < (3, 11),
reason="sibling traceability-matrix-dhf checkout not present")
@pytest.mark.parametrize("sid", SCENARIOS)
def test_records_ingest_into_live_dhf_model(sid):
"""Drift guard: vendored schema == DHF's model, and records load into DhfProject."""
sys.path.insert(0, str(DHF))
try:
models = pytest.importorskip("schemas.models")
finally:
sys.path.remove(str(DHF))
assert models.RiskControl.model_json_schema(mode="validation") == SCHEMA
_, p = _payload(sid)
proj = models.DhfProject(project_id=sid, name=p["process_name"], iec_62304_class="C",
risk_controls=p["risk_controls"])
assert len(proj.risk_controls) == len(p["risk_controls"])