"""A model artifact URI (gs://...) lets the API score without an MLflow server running.""" # Imported eagerly: mlflow loads .pyfunc lazily, so patching it by name can hit the proxy. import mlflow.pyfunc import pytest from app.config import settings from app.services import scoring @pytest.fixture(autouse=True) def clear_cache() -> None: scoring._models.clear() def no_registry(**kwargs: object) -> None: pytest.fail("the registry must not be contacted when MODEL_URI is set") def test_model_uri_skips_the_registry(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "model_uri", "gs://bucket/models/pathogenicity/3") monkeypatch.setattr(scoring, "MlflowClient", no_registry) monkeypatch.setattr(mlflow.pyfunc, "load_model", lambda uri: f"model@{uri}") model, version = scoring.load_model() assert model == "model@gs://bucket/models/pathogenicity/3" assert version == "3" def test_model_uri_without_a_version_segment_still_labels_the_prediction( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(settings, "model_uri", "gs://bucket/models/pathogenicity/") monkeypatch.setattr(scoring, "MlflowClient", no_registry) monkeypatch.setattr(mlflow.pyfunc, "load_model", lambda uri: "model") assert scoring.load_model()[1] == "pathogenicity" def test_model_uri_is_loaded_once(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "model_uri", "gs://bucket/models/pathogenicity/3") monkeypatch.setattr(scoring, "MlflowClient", no_registry) calls: list[str] = [] monkeypatch.setattr(mlflow.pyfunc, "load_model", lambda uri: calls.append(uri) or "model") scoring.load_model() scoring.load_model() assert len(calls) == 1