|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import io |
4 | | -from unittest.mock import patch |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 |
|
| 6 | +import pytest |
6 | 7 | from colorama import Fore |
7 | 8 |
|
8 | 9 | from bayes_opt import BayesianOptimization |
@@ -51,6 +52,44 @@ def test_is_constrained_property(): |
51 | 52 | assert logger.is_constrained |
52 | 53 |
|
53 | 54 |
|
| 55 | +def test_params_config_property(): |
| 56 | + """Test the params_config property getter and setter.""" |
| 57 | + # Test the getter with default initialization (None) |
| 58 | + logger = ScreenLogger() |
| 59 | + assert logger.params_config is None |
| 60 | + |
| 61 | + # Test initialization with a params_config |
| 62 | + mock_config = {"param1": MagicMock(), "param2": MagicMock()} |
| 63 | + logger_with_config = ScreenLogger(params_config=mock_config) |
| 64 | + assert logger_with_config.params_config is mock_config |
| 65 | + |
| 66 | + # Test the setter |
| 67 | + new_config = {"param3": MagicMock(), "param4": MagicMock()} |
| 68 | + logger.params_config = new_config |
| 69 | + assert logger.params_config is new_config |
| 70 | + |
| 71 | + # Test that the logger actually uses the params_config |
| 72 | + optimizer = BayesianOptimization(target_func, PBOUNDS, random_state=1) |
| 73 | + logger.params_config = optimizer._space._params_config |
| 74 | + optimizer.register(params={"p1": 1.5, "p2": 2.5}, target=4.0) |
| 75 | + |
| 76 | + # This should not raise an error now that params_config is set |
| 77 | + step_str = logger._print_step(optimizer._space.res()[-1], optimizer._space.keys) |
| 78 | + assert "|" in step_str |
| 79 | + assert "1" in step_str # iteration |
| 80 | + assert "4.0" in step_str # target value |
| 81 | + |
| 82 | + |
| 83 | +def test_print_step_without_params_config(): |
| 84 | + """Test that _print_step raises an error when params_config is None.""" |
| 85 | + logger = ScreenLogger() |
| 86 | + optimizer = BayesianOptimization(target_func, PBOUNDS, random_state=1) |
| 87 | + optimizer.register(params={"p1": 1.5, "p2": 2.5}, target=4.0) |
| 88 | + |
| 89 | + with pytest.raises(ValueError, match="Parameter configuration is not set"): |
| 90 | + logger._print_step(optimizer._space.res()[-1], optimizer._space.keys) |
| 91 | + |
| 92 | + |
54 | 93 | def test_format_number(): |
55 | 94 | """Test the _format_number method.""" |
56 | 95 | logger = ScreenLogger() |
|
0 commit comments