Skip to content

Commit d36aae5

Browse files
committed
test(cli): patch anthropic via sys.modules since _get_client imports lazily
1 parent 0175c55 commit d36aae5

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/praisonai/tests/unit/cli/test_managed_cli_destructive.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -272,51 +272,58 @@ class TestGetClient:
272272
"""Test the _get_client helper function."""
273273

274274
@patch.dict('os.environ', {'ANTHROPIC_API_KEY': 'test-key'})
275-
@patch('praisonai.cli.commands.managed.anthropic')
276-
def test_get_client_with_anthropic_api_key(self, mock_anthropic):
275+
def test_get_client_with_anthropic_api_key(self):
277276
"""Test client creation with ANTHROPIC_API_KEY."""
277+
import sys
278+
mock_anthropic = MagicMock()
278279
mock_client = Mock()
279280
mock_anthropic.Anthropic.return_value = mock_client
280-
281-
# Reset the patched _get_client to use the real function
282-
from praisonai.cli.commands.managed import _get_client
283-
284-
client = _get_client()
285-
281+
with patch.dict(sys.modules, {'anthropic': mock_anthropic}):
282+
from praisonai.cli.commands.managed import _get_client
283+
client = _get_client()
286284
mock_anthropic.Anthropic.assert_called_once_with(api_key='test-key')
287285
assert client == mock_client
288286

289-
@patch.dict('os.environ', {'CLAUDE_API_KEY': 'test-key-2'})
290-
@patch('praisonai.cli.commands.managed.anthropic')
291-
def test_get_client_with_claude_api_key(self, mock_anthropic):
287+
@patch.dict('os.environ', {'CLAUDE_API_KEY': 'test-key-2'}, clear=True)
288+
def test_get_client_with_claude_api_key(self):
292289
"""Test client creation with CLAUDE_API_KEY."""
290+
import sys
291+
mock_anthropic = MagicMock()
293292
mock_client = Mock()
294293
mock_anthropic.Anthropic.return_value = mock_client
295-
296-
from praisonai.cli.commands.managed import _get_client
297-
298-
client = _get_client()
299-
294+
with patch.dict(sys.modules, {'anthropic': mock_anthropic}):
295+
from praisonai.cli.commands.managed import _get_client
296+
client = _get_client()
300297
mock_anthropic.Anthropic.assert_called_once_with(api_key='test-key-2')
301298
assert client == mock_client
302299

303300
@patch.dict('os.environ', {}, clear=True)
304-
@patch('praisonai.cli.commands.managed.anthropic')
305-
def test_get_client_no_api_key(self, mock_anthropic):
301+
def test_get_client_no_api_key(self):
306302
"""Test client creation with no API key."""
307-
from praisonai.cli.commands.managed import _get_client
308-
309-
with pytest.raises(typer.Exit) as exc_info:
310-
_get_client()
311-
303+
import sys
304+
mock_anthropic = MagicMock()
305+
with patch.dict(sys.modules, {'anthropic': mock_anthropic}):
306+
from praisonai.cli.commands.managed import _get_client
307+
with pytest.raises(typer.Exit) as exc_info:
308+
_get_client()
312309
assert exc_info.value.exit_code == 1
313310

314-
@patch('praisonai.cli.commands.managed.anthropic', None)
315311
def test_get_client_no_anthropic_package(self):
316312
"""Test client creation when anthropic package is not installed."""
317-
from praisonai.cli.commands.managed import _get_client
318-
319-
with pytest.raises(typer.Exit) as exc_info:
320-
_get_client()
321-
322-
assert exc_info.value.exit_code == 1
313+
import sys
314+
import builtins
315+
real_import = builtins.__import__
316+
def fake_import(name, *args, **kwargs):
317+
if name == 'anthropic':
318+
raise ImportError("No module named 'anthropic'")
319+
return real_import(name, *args, **kwargs)
320+
saved = sys.modules.pop('anthropic', None)
321+
try:
322+
with patch('builtins.__import__', side_effect=fake_import):
323+
from praisonai.cli.commands.managed import _get_client
324+
with pytest.raises(typer.Exit) as exc_info:
325+
_get_client()
326+
assert exc_info.value.exit_code == 1
327+
finally:
328+
if saved is not None:
329+
sys.modules['anthropic'] = saved

0 commit comments

Comments
 (0)