96 lines
3.9 KiB
Python
96 lines
3.9 KiB
Python
import pytest
|
|
|
|
from core.governance.enums import ArtifactKind, Environment
|
|
from models.io.registry import registry
|
|
from mlflow import MlflowException
|
|
|
|
from models.io.storage import ReadOnlyRepository
|
|
from models.io.tracking import 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):
|
|
notes = tmp_path / "notas.json"
|
|
notes.write_text("{}")
|
|
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
run.log_artifact(ArtifactKind.OTRO, notes, metadata={"origen": "prueba"})
|
|
version = registry.register_run(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.OTRO.value)]
|
|
assert any(path.endswith("notas.json") 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_registering_only_tolerates_an_existing_registered_model(mlflow_tracking, monkeypatch):
|
|
from mlflow import MlflowClient
|
|
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert registry.register_run(run.finish()) == "1"
|
|
|
|
second = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
assert registry.register_run(second.finish()) == "2"
|
|
|
|
third = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV)
|
|
|
|
def unreachable(self, name):
|
|
raise MlflowException("servidor no disponible")
|
|
|
|
monkeypatch.setattr(MlflowClient, "create_registered_model", unreachable)
|
|
with pytest.raises(MlflowException, match="no disponible"):
|
|
registry.register_run(third.finish())
|
|
|
|
|
|
def test_unknown_domain_is_rejected():
|
|
with pytest.raises(ValueError, match="dominio desconocido"):
|
|
TrainingRun(domain="x", scenario="tamizaje", environment=Environment.DEV)
|
|
|
|
|
|
def test_finishing_a_run_registers_nothing(mlflow_tracking):
|
|
"""Pins down: closing a run logs its metrics and ends it, and only the registry turns it into a version, so a run
|
|
can be kept without becoming a model. Fails if `finish` starts registering again."""
|
|
run = TrainingRun(domain="cacu", scenario="tamizaje", environment=Environment.DEV).finish(metrics={"auc": 0.5})
|
|
|
|
assert registry.client.get_run(run.run_id).info.status == "FINISHED"
|
|
assert registry.client.search_model_versions("name='cacu-tamizaje'") == []
|