Red-Green-Refactor with MLOps examples
Commit at each green. Never commit red.
# Red
def test_model_version_cannot_transition_from_archived_to_production():
v = ModelVersion(name="churn-xgb", version=3, stage=Stage.ARCHIVED)
with pytest.raises(StageTransitionError):
v.transition_to(Stage.PRODUCTION)
# Green — add the guard in ModelVersion.transition_to
# Refactor — extract a transition-matrix constant
# Red
async def test_train_model_registers_when_metrics_pass_threshold():
registry = FakeModelRegistry()
service = TrainModelService(registry=registry, dataset_repo=FakeDatasetRepo(), ...)
await service.execute(TrainRequest(dataset_id="churn-v1", min_accuracy=0.8))
assert registry.latest("churn-xgb").stage == Stage.STAGING
Fakes > mocks. A fake is a working lightweight implementation of the port; a mock asserts on calls. Fakes keep tests robust to refactors.