|
| 1 | +import pickle |
| 2 | +import pytest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | + |
| 5 | +from spotPython.utils.file import load_and_run_spot_python_experiment |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mock_experiment_data(): |
| 10 | + return ( |
| 11 | + MagicMock(), # mock spot_tuner |
| 12 | + {"TENSORBOARD_CLEAN": True, "tensorboard_start": False}, # mock fun_control |
| 13 | + MagicMock(), # mock design_control |
| 14 | + MagicMock(), # mock surrogate_control |
| 15 | + MagicMock() # mock optimizer_control |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +@patch("spotPython.utils.file.load_experiment") |
| 20 | +@patch("spotPython.utils.file.pprint.pprint") |
| 21 | +@patch("spotPython.utils.file.gen_design_table") |
| 22 | +@patch("spotPython.utils.file.setup_paths") |
| 23 | +@patch("spotPython.utils.file.start_tensorboard") |
| 24 | +@patch("spotPython.utils.file.stop_tensorboard") |
| 25 | +def test_load_and_run_spot_python_experiment( |
| 26 | + mock_stop_tensorboard, |
| 27 | + mock_start_tensorboard, |
| 28 | + mock_setup_paths, |
| 29 | + mock_gen_design_table, |
| 30 | + mock_pprint, |
| 31 | + mock_load_experiment, |
| 32 | + mock_experiment_data |
| 33 | + ): |
| 34 | + # Set up the mocks |
| 35 | + mock_load_experiment.return_value = mock_experiment_data |
| 36 | + mock_start_tensorboard.return_value = None |
| 37 | + |
| 38 | + # Call the function under test |
| 39 | + result = load_and_run_spot_python_experiment("spot_test_experiment.pkl") |
| 40 | + |
| 41 | + # Verify behaviors for load_experiment and return values |
| 42 | + mock_load_experiment.assert_called_once_with("spot_test_experiment.pkl") |
| 43 | + mock_gen_design_table.assert_called_once_with(mock_experiment_data[1]) |
| 44 | + mock_setup_paths.assert_called_once_with(mock_experiment_data[1]["TENSORBOARD_CLEAN"]) |
| 45 | + mock_experiment_data[0].init_spot_writer.assert_called_once() |
| 46 | + mock_experiment_data[0].run.assert_called_once() |
| 47 | + mock_experiment_data[0].save_experiment.assert_called_once() |
| 48 | + mock_stop_tensorboard.assert_called_once_with(None) |
| 49 | + |
| 50 | + # Assert the returned tuple |
| 51 | + assert result == (*mock_experiment_data, None) |
0 commit comments