Skip to content

Commit 539c830

Browse files
committed
Refactor mock OpenAI client in tests and enhance CLI output verification
1 parent 147c9b1 commit 539c830

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

tests/test_main.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2+
import os
23
from click.testing import CliRunner
3-
from unittest.mock import patch
4+
from unittest.mock import patch, Mock
45
from aireview.main import main
56

67
@pytest.fixture
@@ -22,18 +23,16 @@ def mock_git_with_changes():
2223
@pytest.fixture
2324
def mock_openai():
2425
"""Mock OpenAI client"""
26+
mock_completion = Mock()
27+
mock_completion.choices = [Mock(message=Mock(content="Test review content"))]
28+
29+
mock_chat = Mock()
30+
mock_chat.completions.create.return_value = mock_completion
31+
32+
mock_client = Mock()
33+
mock_client.chat = mock_chat
34+
2535
with patch('aireview.ai_reviewer.OpenAI') as mock:
26-
mock_client = type('MockClient', (), {
27-
'chat': type('MockChat', (), {
28-
'completions': type('MockCompletions', (), {
29-
'create': lambda **kwargs: type('MockResponse', (), {
30-
'choices': [type('MockChoice', (), {
31-
'message': type('MockMessage', (), {'content': 'Test review'})
32-
})()]
33-
})()
34-
})()
35-
})()
36-
})()
3736
mock.return_value = mock_client
3837
yield mock
3938

@@ -46,10 +45,29 @@ def test_main_cli_no_changes(temp_config_file, mock_git_no_changes):
4645

4746
def test_main_cli_with_changes(temp_config_file, mock_git_with_changes, mock_openai):
4847
"""Test CLI with git changes."""
48+
# Get the output file path from the config
49+
import configparser
50+
config = configparser.ConfigParser()
51+
config.read(temp_config_file)
52+
output_file = config.get("review", "output", fallback="review.md")
53+
4954
runner = CliRunner()
5055
result = runner.invoke(main, ['--config', temp_config_file])
56+
57+
# Check CLI output
5158
assert result.exit_code == 0
52-
assert "Getting review for test.py" in result.output
59+
assert "Generating review for test.py" in result.output
60+
assert f"AI review written to {output_file}" in result.output
61+
62+
# Check review file content
63+
assert os.path.exists(output_file)
64+
with open(output_file, 'r') as f:
65+
content = f.read()
66+
assert "Test review content" in content
67+
68+
# Cleanup
69+
if os.path.exists(output_file):
70+
os.remove(output_file)
5371

5472
def test_main_cli_invalid_config():
5573
"""Test CLI with invalid config file."""

0 commit comments

Comments
 (0)