84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
import pytest
|
|
|
|
from core.governance.enums import ArtifactKind, Environment
|
|
from models.registry import registry
|
|
from mlflow import MlflowException
|
|
|
|
from models.training import ReadOnlyRepository, TrainingRun
|
|
|
|
|
|
def test_prod_run_requires_a_cohort_build():
|
|
with pytest.raises(ValueError, match="cohorte_armado_id"):
|
|
TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.PROD)
|
|
|
|
|
|
def test_dev_run_does_not_require_a_cohort_build(mlflow_tracking):
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert run.cohorte_armado_id is None
|
|
|
|
|
|
def test_finish_registers_model_version_and_artifacts(mlflow_tracking, tmp_path):
|
|
checkpoint = tmp_path / "model.onnx"
|
|
checkpoint.write_bytes(b"fake-onnx-bytes")
|
|
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
run.log_artifact(ArtifactKind.ONNX_CHECKPOINT, checkpoint, metadata={"opset": 17})
|
|
version = run.finish(metrics={"auc": 0.81})
|
|
|
|
mv = registry.get("cacu", "tamizaje", version)
|
|
assert mv.tags["environment"] == Environment.DEV.value
|
|
metrics = registry.client.get_run(mv.run_id).data.metrics
|
|
assert metrics["auc"] == 0.81
|
|
|
|
artifacts = [a.path for a in registry.client.list_artifacts(mv.run_id, path=ArtifactKind.ONNX_CHECKPOINT.value)]
|
|
assert any(path.endswith("model.onnx") for path in artifacts)
|
|
|
|
|
|
def test_read_only_repository_blocks_writes(cacu_db):
|
|
from persistance.cacu import paciente_repository
|
|
|
|
read_only = ReadOnlyRepository(paciente_repository)
|
|
assert read_only.list() == []
|
|
with pytest.raises(PermissionError, match="ML-04"):
|
|
read_only.create(object())
|
|
with pytest.raises(PermissionError, match="ML-04"):
|
|
read_only.open_session()
|
|
|
|
|
|
def test_training_run_repository_is_read_only(cacu_db, mlflow_tracking):
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
repo = run.repository("paciente")
|
|
assert isinstance(repo, ReadOnlyRepository)
|
|
with pytest.raises(PermissionError):
|
|
repo.create(object())
|
|
|
|
|
|
def test_experiments_keep_artifacts_under_the_configured_root(mlflow_tracking, tmp_path, monkeypatch):
|
|
root = tmp_path / "artefactos"
|
|
monkeypatch.setenv("MLFLOW_ARTIFACT_ROOT", str(root))
|
|
run = TrainingRun(domain="cama", scenario="riesgo", environment=Environment.DEV)
|
|
experiment = run.client.get_experiment_by_name("cama-riesgo")
|
|
assert experiment.artifact_location.endswith(str(root / "cama-riesgo"))
|
|
|
|
|
|
def test_finish_only_tolerates_an_existing_registered_model(mlflow_tracking, monkeypatch):
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert run.finish() == "1"
|
|
|
|
second = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert second.finish() == "2"
|
|
|
|
third = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
|
|
def unreachable(name):
|
|
raise MlflowException("servidor no disponible")
|
|
|
|
monkeypatch.setattr(third.client, "create_registered_model", unreachable)
|
|
with pytest.raises(MlflowException, match="no disponible"):
|
|
third.finish()
|
|
|
|
|
|
def test_unknown_domain_is_rejected():
|
|
with pytest.raises(ValueError, match="dominio desconocido"):
|
|
TrainingRun(domain="x", scenario="tamizaje", environment=Environment.DEV)
|