feat: complete evidence restructuring worktree
This commit is contained in:
@@ -206,6 +206,154 @@ def _formula_evidence() -> CuratedEvidence:
|
||||
})
|
||||
|
||||
|
||||
def _domain_evidence_v2() -> CuratedEvidence:
|
||||
return CuratedEvidence.model_validate({
|
||||
**COMMON,
|
||||
"schema_version": 2,
|
||||
"title": "Dominio Ablazione",
|
||||
"kind": "domain",
|
||||
"payload": {
|
||||
"rule": (
|
||||
"Il dominio Ablazione rappresenta la procedura transcatetere.\n\n"
|
||||
"La fact centrale è `clinical.fact_ablazione`."
|
||||
),
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
def test_v2_curated_markdown_renders_domain_content_in_the_markdown_body(tmp_path):
|
||||
evidence = _domain_evidence_v2()
|
||||
path = tmp_path / "curated" / "domain" / "dominio-ablazione.md"
|
||||
|
||||
text = dump_curated_markdown(evidence)
|
||||
frontmatter = text.split("---\n", 2)[1]
|
||||
parsed = parse_curated_markdown(text, path=path)
|
||||
|
||||
assert "domain:" not in frontmatter
|
||||
assert "supporting_excerpts:" not in frontmatter
|
||||
assert "review_items:" not in frontmatter
|
||||
assert "# Dominio Ablazione\n" in text
|
||||
assert "## Regola\n\nIl dominio Ablazione" in text
|
||||
assert "## Estratti di supporto\n\n> I pazienti sotto i 18 anni sono pediatrici." in text
|
||||
assert parsed == evidence
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("kind", "payload", "rendered"), [
|
||||
("glossary", {
|
||||
"definition": "Un paziente con età inferiore a 18 anni.",
|
||||
"synonyms": ["minore"],
|
||||
"variants": ["pediatrico"],
|
||||
}, "## Sinonimi\n\n- minore"),
|
||||
("enum", {
|
||||
"column": "clinical.episode.discharge_status",
|
||||
"values": {"D": "dimesso", "T": "trasferito | altra struttura"},
|
||||
}, "| `D` | dimesso |"),
|
||||
("example", {
|
||||
"question": "Come riconosco un paziente pediatrico?",
|
||||
"interpretation": "Applicare la formula della fascia pediatrica.",
|
||||
}, "## Domanda\n\nCome riconosco un paziente pediatrico?"),
|
||||
("mapping", {
|
||||
"concept": "fascia pediatrica",
|
||||
"tables": ["clinical.patient"],
|
||||
"columns": ["clinical.patient.birth_date"],
|
||||
}, "## Tabelle\n\n- `clinical.patient`"),
|
||||
("normalization", {
|
||||
"input": "PEDS",
|
||||
"output": "pediatrico",
|
||||
"rule": "Converte il codice abbreviato nella forma canonica.",
|
||||
}, "## Output\n\npediatrico"),
|
||||
("formula", {
|
||||
"concept": "fascia pediatrica",
|
||||
"columns": ["clinical.patient.birth_date"],
|
||||
"sql": "CASE WHEN age < 18 THEN 'pediatrica' ELSE 'adulta' END",
|
||||
}, "```sql\nCASE WHEN age < 18"),
|
||||
("reference", {
|
||||
"url": "https://example.test/linea-guida",
|
||||
"label": "Linea guida",
|
||||
"description": "Criteri clinici di riferimento.",
|
||||
}, "## URL\n\n<https://example.test/linea-guida>"),
|
||||
])
|
||||
def test_v2_curated_markdown_renders_and_round_trips_every_typed_payload(
|
||||
tmp_path, kind, payload, rendered,
|
||||
):
|
||||
evidence = CuratedEvidence.model_validate({
|
||||
**COMMON,
|
||||
"schema_version": 2,
|
||||
"kind": kind,
|
||||
"payload": payload,
|
||||
})
|
||||
path = tmp_path / "curated" / kind / "fascia-pediatrica.md"
|
||||
|
||||
text = dump_curated_markdown(evidence)
|
||||
|
||||
assert rendered in text
|
||||
assert parse_curated_markdown(text, path=path) == evidence
|
||||
|
||||
|
||||
def test_v2_curated_markdown_renders_review_items_as_readable_blocks(tmp_path):
|
||||
evidence = CuratedEvidence.model_validate({
|
||||
**COMMON,
|
||||
"schema_version": 2,
|
||||
"kind": "domain",
|
||||
"review_items": [{
|
||||
"code": "ambiguous_source_statement",
|
||||
"message": "Il sorgente non chiarisce la data di riferimento.",
|
||||
"field": "domain.rule",
|
||||
}],
|
||||
"payload": {"rule": "L'età è calcolata alla data di ricovero."},
|
||||
})
|
||||
path = tmp_path / "curated" / "domain" / "fascia-pediatrica.md"
|
||||
|
||||
text = dump_curated_markdown(evidence)
|
||||
|
||||
assert "## Elementi da rivedere" in text
|
||||
assert "### `ambiguous_source_statement`" in text
|
||||
assert "Il sorgente non chiarisce la data di riferimento." in text
|
||||
assert "**Campo:** `domain.rule`" in text
|
||||
assert parse_curated_markdown(text, path=path) == evidence
|
||||
|
||||
|
||||
@pytest.mark.parametrize("legacy_field", [
|
||||
"review_items: []\n",
|
||||
"payload:\n rule: should-not-be-ignored\n",
|
||||
])
|
||||
def test_v2_curated_markdown_rejects_body_owned_fields_in_frontmatter(legacy_field):
|
||||
text = dump_curated_markdown(_domain_evidence_v2()).replace(
|
||||
"kind: domain\n",
|
||||
f"kind: domain\n{legacy_field}",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="frontmatter"):
|
||||
parse_curated_markdown(text)
|
||||
|
||||
|
||||
def test_v2_curated_markdown_rejects_unstructured_body_content():
|
||||
text = dump_curated_markdown(_domain_evidence_v2()) + "should-not-be-ignored\n"
|
||||
|
||||
with pytest.raises(ValueError, match="unstructured"):
|
||||
parse_curated_markdown(text)
|
||||
|
||||
|
||||
def test_v2_curated_markdown_round_trips_an_empty_enum_as_an_explicit_empty_state(tmp_path):
|
||||
evidence = CuratedEvidence.model_validate({
|
||||
**COMMON,
|
||||
"schema_version": 2,
|
||||
"kind": "enum",
|
||||
"payload": {
|
||||
"column": "clinical.episode.discharge_status",
|
||||
"values": {},
|
||||
},
|
||||
})
|
||||
|
||||
text = dump_curated_markdown(evidence)
|
||||
|
||||
assert "Nessun elemento" in text
|
||||
assert parse_curated_markdown(
|
||||
text,
|
||||
path=tmp_path / "curated" / "enum" / "discharge-status.md",
|
||||
) == evidence
|
||||
|
||||
|
||||
def test_curated_markdown_round_trip_uses_the_kind_specific_key(tmp_path):
|
||||
evidence = _formula_evidence()
|
||||
path = tmp_path / "curated" / "formula" / "fascia-pediatrica.md"
|
||||
|
||||
Reference in New Issue
Block a user