312 lines
9.3 KiB
Python
312 lines
9.3 KiB
Python
"""Review protocols: YAML label sets, validation, and PRISMA expansion."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
from focus import protocols
|
|
from lead.review.codes import PROJECTS, reason_choices, reason_codes
|
|
|
|
_KEYS = "1234567890abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
def _write(directory: Path, name: str, body: str) -> None:
|
|
(directory / f"{name}.yaml").write_text(body, encoding="utf-8")
|
|
|
|
|
|
def _base(**overrides: object) -> dict:
|
|
data: dict = {
|
|
"name": "sample",
|
|
"version": 1,
|
|
"title": "Sample",
|
|
"scope": "A test protocol.",
|
|
"labels": [
|
|
{"id": "alpha", "text": "Alpha", "key": "1"},
|
|
{"id": "beta", "text": "Beta", "key": "2", "tag": "custom:beta"},
|
|
],
|
|
}
|
|
data.update(overrides)
|
|
return data
|
|
|
|
|
|
def _dump(directory: Path, name: str, data: dict) -> None:
|
|
_write(directory, name, yaml.safe_dump(data))
|
|
|
|
|
|
def test_available_lists_shipped_names():
|
|
assert protocols.available() == [
|
|
"cms-disposition",
|
|
"code-family",
|
|
"comment-stance",
|
|
"prisma-screen",
|
|
]
|
|
|
|
|
|
def test_comment_stance_labels_and_default_tags():
|
|
proto = protocols.load("comment-stance")
|
|
assert proto.name == "comment-stance"
|
|
assert proto.version == 1
|
|
assert proto.title == "Comment stance"
|
|
assert proto.scope
|
|
assert [(lb.id, lb.key) for lb in proto.labels] == [
|
|
("support", "1"),
|
|
("support-with-modifications", "2"),
|
|
("do-not-support", "3"),
|
|
]
|
|
assert [lb.tag for lb in proto.labels] == [
|
|
"hl:comment-stance:support",
|
|
"hl:comment-stance:support-with-modifications",
|
|
"hl:comment-stance:do-not-support",
|
|
]
|
|
assert proto.label("support") == proto.labels[0]
|
|
assert proto.label("missing") is None
|
|
|
|
|
|
def test_cms_disposition_labels():
|
|
proto = protocols.load("cms-disposition")
|
|
assert [lb.id for lb in proto.labels] == [
|
|
"finalized-as-proposed",
|
|
"finalized-with-modifications",
|
|
"considered-for-future-rulemaking",
|
|
"other",
|
|
]
|
|
assert all(lb.tag == f"hl:cms-disposition:{lb.id}" for lb in proto.labels)
|
|
|
|
|
|
def test_code_family_placeholder_labels():
|
|
"""Element names for a family biography are not a closed label set.
|
|
These five are the plan's placeholder."""
|
|
proto = protocols.load("code-family")
|
|
assert [lb.id for lb in proto.labels] == [
|
|
"code",
|
|
"descriptor-change",
|
|
"payment-change",
|
|
"rationale",
|
|
"other",
|
|
]
|
|
|
|
|
|
def test_prisma_screen_expands_real_reason_codes():
|
|
proto = protocols.load("prisma-screen")
|
|
expected: list[str] = []
|
|
seen: set[str] = set()
|
|
sentences: dict[str, str] = {}
|
|
for project in PROJECTS:
|
|
for code, sentence in reason_choices(project):
|
|
sentences.setdefault(code, sentence)
|
|
for code in reason_codes(project):
|
|
if code not in seen:
|
|
seen.add(code)
|
|
expected.append(code)
|
|
assert [lb.id for lb in proto.labels] == expected
|
|
assert {lb.id for lb in proto.labels} == {
|
|
code for project in PROJECTS for code in reason_codes(project)
|
|
}
|
|
assert [lb.text for lb in proto.labels] == [sentences[i] for i in expected]
|
|
assert [lb.key for lb in proto.labels] == list(_KEYS[: len(expected)])
|
|
assert all(lb.tag == f"hl:prisma-screen:{lb.id}" for lb in proto.labels)
|
|
|
|
|
|
def test_explicit_tag_is_kept(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_dump(tmp_path, "sample", _base())
|
|
proto = protocols.load("sample")
|
|
assert proto.labels[0].tag == "hl:sample:alpha"
|
|
assert proto.labels[1].tag == "custom:beta"
|
|
assert protocols.available() == ["sample"]
|
|
|
|
|
|
def test_missing_protocol_names_the_file():
|
|
with pytest.raises(ValueError, match=r"missing\.yaml"):
|
|
protocols.load("missing")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mutate",
|
|
[
|
|
"duplicate-id",
|
|
"duplicate-key",
|
|
"long-key",
|
|
"empty-key",
|
|
"int-key",
|
|
"bad-id",
|
|
"empty-id",
|
|
"missing-text",
|
|
"label-not-mapping",
|
|
"missing-labels",
|
|
"empty-labels",
|
|
"name-mismatch",
|
|
"version-str",
|
|
"version-bool",
|
|
"missing-title",
|
|
"title-not-str",
|
|
"missing-scope",
|
|
"scope-not-str",
|
|
"not-a-mapping",
|
|
"bad-from",
|
|
"from-and-labels",
|
|
"empty-tag",
|
|
"tag-not-str",
|
|
"missing-name",
|
|
],
|
|
)
|
|
def test_bad_file_raises_with_filename(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mutate: str
|
|
):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
data = _base()
|
|
raw: str | None = None
|
|
if mutate == "duplicate-id":
|
|
data["labels"][1]["id"] = "alpha"
|
|
elif mutate == "duplicate-key":
|
|
data["labels"][1]["key"] = "1"
|
|
elif mutate == "long-key":
|
|
data["labels"][0]["key"] = "ab"
|
|
elif mutate == "empty-key":
|
|
data["labels"][0]["key"] = ""
|
|
elif mutate == "int-key":
|
|
data["labels"][0]["key"] = 1
|
|
elif mutate == "bad-id":
|
|
data["labels"][0]["id"] = "Bad_Id"
|
|
elif mutate == "empty-id":
|
|
data["labels"][0]["id"] = ""
|
|
elif mutate == "missing-text":
|
|
del data["labels"][0]["text"]
|
|
elif mutate == "label-not-mapping":
|
|
data["labels"][0] = "alpha"
|
|
elif mutate == "missing-labels":
|
|
del data["labels"]
|
|
elif mutate == "empty-labels":
|
|
data["labels"] = []
|
|
elif mutate == "name-mismatch":
|
|
data["name"] = "other"
|
|
elif mutate == "version-str":
|
|
data["version"] = "1"
|
|
elif mutate == "version-bool":
|
|
data["version"] = True
|
|
elif mutate == "missing-title":
|
|
del data["title"]
|
|
elif mutate == "title-not-str":
|
|
data["title"] = 1
|
|
elif mutate == "missing-scope":
|
|
del data["scope"]
|
|
elif mutate == "scope-not-str":
|
|
data["scope"] = 1
|
|
elif mutate == "not-a-mapping":
|
|
raw = "- just\n- a\n- list\n"
|
|
elif mutate == "bad-from":
|
|
del data["labels"]
|
|
data["from"] = "somewhere.else"
|
|
elif mutate == "from-and-labels":
|
|
data["from"] = "lead.review.codes"
|
|
elif mutate == "empty-tag":
|
|
data["labels"][0]["tag"] = ""
|
|
elif mutate == "tag-not-str":
|
|
data["labels"][0]["tag"] = 3
|
|
elif mutate == "missing-name":
|
|
del data["name"]
|
|
else:
|
|
raise AssertionError(mutate)
|
|
if raw is None:
|
|
_dump(tmp_path, "sample", data)
|
|
else:
|
|
_write(tmp_path, "sample", raw)
|
|
with pytest.raises(ValueError, match=r"sample\.yaml"):
|
|
protocols.load("sample")
|
|
|
|
|
|
def test_invalid_yaml_names_the_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_write(tmp_path, "sample", "name: [\n")
|
|
with pytest.raises(ValueError, match=r"sample\.yaml"):
|
|
protocols.load("sample")
|
|
|
|
|
|
def test_expansion_rejects_more_labels_than_keys(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_dump(
|
|
tmp_path,
|
|
"wide",
|
|
{
|
|
"name": "wide",
|
|
"version": 1,
|
|
"title": "Wide",
|
|
"scope": "too many",
|
|
"from": "lead.review.codes",
|
|
},
|
|
)
|
|
codes = [f"c{i}" for i in range(len(_KEYS) + 1)]
|
|
monkeypatch.setattr(
|
|
"lead.review.codes.PROJECTS",
|
|
("only",),
|
|
)
|
|
monkeypatch.setattr(
|
|
"lead.review.codes.reason_codes",
|
|
lambda _project: tuple(codes),
|
|
)
|
|
monkeypatch.setattr(
|
|
"lead.review.codes.reason_choices",
|
|
lambda _project: tuple((code, f"{code}.") for code in codes),
|
|
)
|
|
with pytest.raises(ValueError, match=r"wide\.yaml"):
|
|
protocols.load("wide")
|
|
|
|
|
|
def test_expansion_rejects_a_code_with_no_sentence(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_dump(
|
|
tmp_path,
|
|
"wide",
|
|
{
|
|
"name": "wide",
|
|
"version": 1,
|
|
"title": "Wide",
|
|
"scope": "no sentence",
|
|
"from": "lead.review.codes",
|
|
},
|
|
)
|
|
monkeypatch.setattr("lead.review.codes.PROJECTS", ("only",))
|
|
monkeypatch.setattr("lead.review.codes.reason_codes", lambda _p: ("orphan",))
|
|
monkeypatch.setattr("lead.review.codes.reason_choices", lambda _p: ())
|
|
with pytest.raises(ValueError, match=r"wide\.yaml"):
|
|
protocols.load("wide")
|
|
|
|
|
|
def test_expansion_failure_names_the_file(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_dump(
|
|
tmp_path,
|
|
"wide",
|
|
{
|
|
"name": "wide",
|
|
"version": 1,
|
|
"title": "Wide",
|
|
"scope": "boom",
|
|
"from": "lead.review.codes",
|
|
},
|
|
)
|
|
|
|
def _boom(_project: str) -> tuple[str, ...]:
|
|
raise ValueError("codebook mismatch")
|
|
|
|
monkeypatch.setattr("lead.review.codes.reason_codes", _boom)
|
|
with pytest.raises(ValueError, match=r"wide\.yaml"):
|
|
protocols.load("wide")
|
|
|
|
|
|
def test_available_skips_non_yaml(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(protocols, "ROOT", tmp_path)
|
|
_dump(tmp_path, "b", _base(name="b"))
|
|
_dump(tmp_path, "a", _base(name="a"))
|
|
(tmp_path / "notes.txt").write_text("nope", encoding="utf-8")
|
|
assert protocols.available() == ["a", "b"]
|