Skip to content

Commit 0d27c70

Browse files
committed
[test] feat: Add unit tests for MLflow integration
- Add tests for mlflow_logger property in GlobalState - Add tests for _timers_write_to_mlflow function - Add tests for safe_serialize function in log_utils.py - Add tests for LoggerConfig.finalize() method - Update reset_for_restart test to include _mlflow_logger Signed-off-by: yaoyu-33 <[email protected]>
1 parent fa03b2c commit 0d27c70

File tree

3 files changed

+464
-1
lines changed

3 files changed

+464
-1
lines changed

tests/unit_tests/training/test_config.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,3 +2544,84 @@ def build_datasets(
25442544
container.validate()
25452545
finally:
25462546
restore_get_world_size_safe(og_ws, cfg_mod)
2547+
2548+
2549+
@pytest.mark.unit
2550+
class TestLoggerConfigFinalize:
2551+
"""Tests for LoggerConfig.finalize() method."""
2552+
2553+
def test_finalize_no_mlflow_settings(self):
2554+
"""Test finalize succeeds when no MLFlow settings are configured."""
2555+
config = LoggerConfig()
2556+
# Should not raise
2557+
config.finalize()
2558+
2559+
def test_finalize_with_mlflow_experiment_only_raises_error(self):
2560+
"""Test finalize raises error when mlflow_experiment is set but mlflow_run_name is missing."""
2561+
config = LoggerConfig(mlflow_experiment="my_experiment")
2562+
2563+
with pytest.raises(ValueError, match="Set logger.mlflow_run_name"):
2564+
config.finalize()
2565+
2566+
def test_finalize_with_mlflow_experiment_and_empty_run_name_raises_error(self):
2567+
"""Test finalize raises error when mlflow_run_name is empty string."""
2568+
config = LoggerConfig(mlflow_experiment="my_experiment", mlflow_run_name="")
2569+
2570+
with pytest.raises(ValueError, match="Set logger.mlflow_run_name"):
2571+
config.finalize()
2572+
2573+
def test_finalize_with_mlflow_experiment_and_run_name_succeeds(self):
2574+
"""Test finalize succeeds when both mlflow_experiment and mlflow_run_name are set."""
2575+
config = LoggerConfig(mlflow_experiment="my_experiment", mlflow_run_name="my_run")
2576+
# Should not raise (assuming mlflow is importable)
2577+
try:
2578+
config.finalize()
2579+
except ModuleNotFoundError:
2580+
# MLflow might not be installed in test environment
2581+
pass
2582+
2583+
def test_finalize_mlflow_not_installed_raises_module_not_found(self):
2584+
"""Test finalize raises ModuleNotFoundError when mlflow is configured but not installed."""
2585+
config = LoggerConfig(mlflow_experiment="my_experiment", mlflow_run_name="my_run")
2586+
2587+
with patch.dict("sys.modules", {"mlflow": None}):
2588+
with patch("importlib.import_module", side_effect=ModuleNotFoundError("No module named 'mlflow'")):
2589+
with pytest.raises(ModuleNotFoundError, match="mlflow"):
2590+
config.finalize()
2591+
2592+
def test_finalize_with_mlflow_tags_only(self):
2593+
"""Test finalize with only mlflow_tags triggers MLFlow validation."""
2594+
config = LoggerConfig(mlflow_tags={"env": "test"})
2595+
2596+
# mlflow_tags without mlflow_experiment should still try to import mlflow
2597+
# but not require mlflow_run_name since experiment is not set
2598+
try:
2599+
config.finalize()
2600+
except ModuleNotFoundError:
2601+
# MLflow might not be installed in test environment
2602+
pass
2603+
2604+
def test_finalize_with_mlflow_tracking_uri_only(self):
2605+
"""Test finalize with only mlflow_tracking_uri triggers MLFlow validation."""
2606+
config = LoggerConfig(mlflow_tracking_uri="http://localhost:5000")
2607+
2608+
try:
2609+
config.finalize()
2610+
except ModuleNotFoundError:
2611+
# MLflow might not be installed in test environment
2612+
pass
2613+
2614+
def test_finalize_with_all_mlflow_settings(self):
2615+
"""Test finalize with all MLFlow settings configured."""
2616+
config = LoggerConfig(
2617+
mlflow_experiment="my_experiment",
2618+
mlflow_run_name="my_run",
2619+
mlflow_tracking_uri="http://localhost:5000",
2620+
mlflow_tags={"env": "test", "version": "1.0"},
2621+
)
2622+
2623+
try:
2624+
config.finalize()
2625+
except ModuleNotFoundError:
2626+
# MLflow might not be installed in test environment
2627+
pass

0 commit comments

Comments
 (0)