75 lines
3.6 KiB
Python
75 lines
3.6 KiB
Python
import pytest
|
|
from core.governance.enums import ArtifactKind, Environment
|
|
from models.registry import PromotionBlocked, registry
|
|
|
|
|
|
def test_dev_version_cannot_be_promoted_and_says_why(governance_db, mlflow_tracking):
|
|
version = registry.register(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert registry.status("cacu", "tamizaje", version) == "desarrollo"
|
|
with pytest.raises(PromotionBlocked) as blocked:
|
|
registry.promote("cacu", "tamizaje", version)
|
|
failed = {check.code for check in blocked.value.checks if not check.passed}
|
|
assert failed == {"ML-02", "ML-03", "MLD-01", "MLD-01b", "MLD-03", "ML-01b"}
|
|
|
|
|
|
def test_promotion_retires_the_previous_version_of_the_scenario(production_ready):
|
|
domain, scenario, first = production_ready("v1")
|
|
_, _, second = production_ready("v2", metrics={"auc": 0.84})
|
|
assert registry.status(domain, scenario, first) == "candidata"
|
|
registry.promote(domain, scenario, first)
|
|
registry.promote(domain, scenario, second)
|
|
assert registry.status(domain, scenario, first) == "retirada"
|
|
assert registry.status(domain, scenario, second) == "produccion"
|
|
other_domain, other_scenario, other_version = production_ready("v1-dx", scenario="diagnostico")
|
|
registry.promote(other_domain, other_scenario, other_version)
|
|
assert registry.status(domain, scenario, second) == "produccion"
|
|
|
|
|
|
def test_reports_can_be_attached_after_training(production_ready):
|
|
domain, scenario, version = production_ready("v1")
|
|
registry.attach_reports(domain, scenario, version, clinical_validation_report_uri=None)
|
|
assert not next(c for c in registry.checks(domain, scenario, version) if c.code == "MLD-01b").passed
|
|
registry.attach_reports(domain, scenario, version, clinical_validation_report_uri="docs/clinica-v2.pdf")
|
|
assert all(c.passed for c in registry.checks(domain, scenario, version))
|
|
with pytest.raises(ValueError):
|
|
registry.attach_reports(domain, scenario, version, metrics="{}")
|
|
|
|
|
|
def test_artifacts_are_logged_to_the_run(production_ready):
|
|
domain, scenario, version = production_ready("v1")
|
|
lineage = registry.lineage(domain, scenario, version)
|
|
assert any(path.startswith(ArtifactKind.ONNX_CHECKPOINT.value) for path in lineage["artifacts"])
|
|
|
|
|
|
def test_retire_and_lineage(production_ready):
|
|
domain, scenario, first = production_ready("v1", metrics={"auc": 0.8, "brier": 0.2})
|
|
_, _, second = production_ready("v2", metrics={"auc": 0.85})
|
|
registry.promote(domain, scenario, second)
|
|
lineage = registry.lineage(domain, scenario, second)
|
|
assert lineage["cohorte_armado"]["huella"] == "huella-v2"
|
|
assert [p["paso"] for p in lineage["cohorte_exclusiones"]] == ["candidatas", "sin evento previo"]
|
|
assert lineage["model_version"]["status"] == "produccion"
|
|
registry.retire(domain, scenario, second)
|
|
assert registry.status(domain, scenario, second) == "retirada"
|
|
with pytest.raises(ValueError):
|
|
registry.retire(domain, scenario, second)
|
|
|
|
|
|
def test_invalid_onnx_checkpoint_is_rejected(production_ready):
|
|
from models.export import inspect_onnx_metadata
|
|
|
|
with pytest.raises(ValueError, match="ONNX"):
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "x.onnx"
|
|
path.write_bytes(b"weights")
|
|
inspect_onnx_metadata(path)
|
|
|
|
|
|
@pytest.mark.parametrize("domain, scenario", [("cacu' OR '1'='1", None), ("cacu", "x' OR name LIKE '%")])
|
|
def test_version_filters_reject_anything_but_known_names(mlflow_tracking, domain, scenario):
|
|
with pytest.raises(ValueError):
|
|
registry.versions(domain, scenario)
|