Skip to content

Commit ef31faf

Browse files
committed
add tests to check params_config implementation
1 parent aa7a350 commit ef31faf

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

tests/test_logger.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

33
import io
4-
from unittest.mock import patch
4+
from unittest.mock import MagicMock, patch
55

6+
import pytest
67
from colorama import Fore
78

89
from bayes_opt import BayesianOptimization
@@ -51,6 +52,44 @@ def test_is_constrained_property():
5152
assert logger.is_constrained
5253

5354

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+
5493
def test_format_number():
5594
"""Test the _format_number method."""
5695
logger = ScreenLogger()

0 commit comments

Comments
 (0)