Skip to content

Commit 30594c7

Browse files
timmensclaude
andcommitted
Add tests for tranquilo version check error handling
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8c20da7 commit 30594c7

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
import pytest
3+
4+
from optimagic.exceptions import NotInstalledError
5+
from optimagic.optimizers.tranquilo import Tranquilo, TranquiloLS
6+
7+
8+
@pytest.fixture()
9+
def mock_problem():
10+
"""Create a minimal mock of InternalOptimizationProblem."""
11+
12+
class MockBounds:
13+
lower = np.array([-1.0, -1.0])
14+
upper = np.array([1.0, 1.0])
15+
16+
class MockProblem:
17+
bounds = MockBounds()
18+
19+
def batch_fun(self, xs):
20+
return [np.sum(x**2) for x in xs]
21+
22+
return MockProblem()
23+
24+
25+
def test_tranquilo_raises_if_version_too_old(monkeypatch, mock_problem):
26+
import optimagic.optimizers.tranquilo as tranquilo_mod
27+
28+
monkeypatch.setattr(
29+
tranquilo_mod, "IS_TRANQUILO_VERSION_NEWER_OR_EQUAL_TO_0_1_0", False
30+
)
31+
32+
algo = Tranquilo()
33+
x0 = np.array([0.5, 0.5])
34+
35+
with pytest.raises(NotInstalledError, match="tranquilo"):
36+
algo._solve_internal_problem(mock_problem, x0)
37+
38+
39+
def test_tranquilo_ls_raises_if_version_too_old(monkeypatch, mock_problem):
40+
import optimagic.optimizers.tranquilo as tranquilo_mod
41+
42+
monkeypatch.setattr(
43+
tranquilo_mod, "IS_TRANQUILO_VERSION_NEWER_OR_EQUAL_TO_0_1_0", False
44+
)
45+
46+
algo = TranquiloLS()
47+
x0 = np.array([0.5, 0.5])
48+
49+
with pytest.raises(NotInstalledError, match="tranquilo"):
50+
algo._solve_internal_problem(mock_problem, x0)

0 commit comments

Comments
 (0)