Skip to content

Commit

Permalink
✨ feat: enhance AI service and batch processing tests
Browse files Browse the repository at this point in the history
✨ feat: enhance AI service and batch processing tests

✨ Features:
- Introduced API key setup in AIService fixture
- Updated commit message generation test for clarity
- Modified commit message formatting for consistency

✅ Testing:
- Refactored mock console usage in batch processing tests
- Enhanced test coverage for user cancellation and git error handling
- Improved CLI error handling tests with mock setup

Enhanced AI service tests and improved batch processing and CLI error handling
  • Loading branch information
Test User committed Dec 9, 2024
1 parent 0cdc8ae commit 45af9ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
13 changes: 3 additions & 10 deletions tests/test_ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest.fixture
def ai_service():
"""Fixture for AIService instance."""
return AIService()
return AIService(api_key="test_key")


def test_token_usage_from_api_usage():
Expand Down Expand Up @@ -78,9 +78,7 @@ def test_generate_commit_message_success(mock_post, ai_service, mock_git_file):

mock_post.return_value = MagicMock(status_code=200, json=lambda: mock_response)

suggestion, usage = ai_service.generate_commit_message(
"test diff", [mock_git_file("test.py")]
)
suggestion, usage = ai_service.generate_commit_message("test diff", [mock_git_file("test.py")])

assert isinstance(suggestion, CommitSuggestion)
assert suggestion.title == "✨ feat: add new feature"
Expand Down Expand Up @@ -131,12 +129,7 @@ def test_format_commit_message():
"""Test commit message formatting."""
suggestion = CommitSuggestion(
title="✨ feat: add new feature",
body={
"Features": {
"emoji": "✨",
"changes": ["Added new functionality"],
}
},
body={"Changes": {"emoji": "✨", "changes": ["Added new functionality"]}},
summary="Added new feature for better user experience",
)

Expand Down
21 changes: 17 additions & 4 deletions tests/test_batch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class TestBatchProcessing:

def test_single_batch(self, mock_deps, mocker, mock_git_file, batch_config):
"""Test processing a single batch of files."""
mock_console = mocker.patch("commitloom.cli.cli_handler.console")
# Mock console
mock_console = mocker.patch("commitloom.core.batch.console")
mock_console.confirm_action.return_value = True

# Setup test data
Expand All @@ -34,7 +35,8 @@ def test_single_batch(self, mock_deps, mocker, mock_git_file, batch_config):

def test_multiple_batches(self, mock_deps, mocker, mock_git_file, batch_config):
"""Test processing multiple batches of files."""
mock_console = mocker.patch("commitloom.cli.cli_handler.console")
# Mock console
mock_console = mocker.patch("commitloom.core.batch.console")
mock_console.confirm_action.return_value = True

# Setup test data
Expand Down Expand Up @@ -65,19 +67,30 @@ def test_empty_batch(self, mock_deps, batch_config):

def test_user_cancellation(self, mock_deps, mocker, mock_git_file, batch_config):
"""Test handling of user cancellation."""
mock_console = mocker.patch("commitloom.cli.cli_handler.console")
# Mock console
mock_console = mocker.patch("commitloom.core.batch.console")
mock_console.confirm_action.return_value = False

# Setup test data
files = [mock_git_file("test.py")]
mock_deps["git"].get_diff.return_value = "test diff"
mock_deps["analyzer"].estimate_tokens_and_cost.return_value = (100, 0.01)

# Process batch
processor = BatchProcessor(batch_config)
processor.git = mock_deps["git"] # Use mocked git operations
processor.process_files(files)

# Verify no files were staged
mock_deps["git"].stage_files.assert_not_called()

def test_git_error_handling(self, mock_deps, mock_git_file, batch_config):
def test_git_error_handling(self, mock_deps, mocker, mock_git_file, batch_config):
"""Test handling of git errors."""
# Mock console
mock_console = mocker.patch("commitloom.core.batch.console")
mock_console.confirm_action.return_value = True

# Setup test data
files = [mock_git_file("test.py")]
mock_deps["git"].stage_files.side_effect = Exception("Git error")

Expand Down
30 changes: 22 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ def test_help_text(self, runner):
assert result.exit_code == 0
assert "Usage:" in result.output

def test_basic_run(self, runner, mock_loom):
def test_basic_run(self, runner, mock_loom, mocker):
"""Test basic run without arguments."""
mock_loom.run.return_value = None
# Mock the CommitLoom class
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
mock_commit_loom.return_value = mock_loom

result = runner.invoke(main)
assert result.exit_code == 0
mock_loom.run.assert_called_once_with(auto_commit=False, combine_commits=False, debug=False)

def test_all_flags(self, runner, mock_loom):
def test_all_flags(self, runner, mock_loom, mocker):
"""Test run with all flags enabled."""
mock_loom.run.return_value = None
# Mock the CommitLoom class
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
mock_commit_loom.return_value = mock_loom

result = runner.invoke(main, ["-y", "-c", "-d"])
assert result.exit_code == 0
mock_loom.run.assert_called_once_with(auto_commit=True, combine_commits=True, debug=True)
Expand All @@ -39,16 +45,24 @@ def test_all_flags(self, runner, mock_loom):
class TestCliErrors:
"""Test CLI error handling."""

def test_keyboard_interrupt(self, runner, mock_loom, capsys):
def test_keyboard_interrupt(self, runner, mock_loom, mocker):
"""Test handling of keyboard interrupt."""
# Mock the CommitLoom class
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
mock_commit_loom.return_value = mock_loom
mock_loom.run.side_effect = KeyboardInterrupt()

result = runner.invoke(main)
assert result.exit_code == 1
assert "Operation cancelled by user" in capsys.readouterr().err
assert "Operation cancelled by user" in result.output

def test_general_error(self, runner, mock_loom, capsys):
def test_general_error(self, runner, mock_loom, mocker):
"""Test handling of general errors."""
# Mock the CommitLoom class
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
mock_commit_loom.return_value = mock_loom
mock_loom.run.side_effect = Exception("Test error")

result = runner.invoke(main)
assert result.exit_code == 1
assert "Test error" in capsys.readouterr().err
assert "Test error" in result.output

0 comments on commit 45af9ed

Please sign in to comment.