Skip to content

Commit 45af9ed

Browse files
author
Test User
committed
✨ feat: enhance AI service and batch processing tests
✨ 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
1 parent 0cdc8ae commit 45af9ed

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

tests/test_ai_service.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@pytest.fixture
1313
def ai_service():
1414
"""Fixture for AIService instance."""
15-
return AIService()
15+
return AIService(api_key="test_key")
1616

1717

1818
def test_token_usage_from_api_usage():
@@ -78,9 +78,7 @@ def test_generate_commit_message_success(mock_post, ai_service, mock_git_file):
7878

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

81-
suggestion, usage = ai_service.generate_commit_message(
82-
"test diff", [mock_git_file("test.py")]
83-
)
81+
suggestion, usage = ai_service.generate_commit_message("test diff", [mock_git_file("test.py")])
8482

8583
assert isinstance(suggestion, CommitSuggestion)
8684
assert suggestion.title == "✨ feat: add new feature"
@@ -131,12 +129,7 @@ def test_format_commit_message():
131129
"""Test commit message formatting."""
132130
suggestion = CommitSuggestion(
133131
title="✨ feat: add new feature",
134-
body={
135-
"Features": {
136-
"emoji": "✨",
137-
"changes": ["Added new functionality"],
138-
}
139-
},
132+
body={"Changes": {"emoji": "✨", "changes": ["Added new functionality"]}},
140133
summary="Added new feature for better user experience",
141134
)
142135

tests/test_batch_processing.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class TestBatchProcessing:
1616

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

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

3536
def test_multiple_batches(self, mock_deps, mocker, mock_git_file, batch_config):
3637
"""Test processing multiple batches of files."""
37-
mock_console = mocker.patch("commitloom.cli.cli_handler.console")
38+
# Mock console
39+
mock_console = mocker.patch("commitloom.core.batch.console")
3840
mock_console.confirm_action.return_value = True
3941

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

6668
def test_user_cancellation(self, mock_deps, mocker, mock_git_file, batch_config):
6769
"""Test handling of user cancellation."""
68-
mock_console = mocker.patch("commitloom.cli.cli_handler.console")
70+
# Mock console
71+
mock_console = mocker.patch("commitloom.core.batch.console")
6972
mock_console.confirm_action.return_value = False
7073

74+
# Setup test data
7175
files = [mock_git_file("test.py")]
76+
mock_deps["git"].get_diff.return_value = "test diff"
77+
mock_deps["analyzer"].estimate_tokens_and_cost.return_value = (100, 0.01)
78+
79+
# Process batch
7280
processor = BatchProcessor(batch_config)
7381
processor.git = mock_deps["git"] # Use mocked git operations
7482
processor.process_files(files)
7583

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

79-
def test_git_error_handling(self, mock_deps, mock_git_file, batch_config):
87+
def test_git_error_handling(self, mock_deps, mocker, mock_git_file, batch_config):
8088
"""Test handling of git errors."""
89+
# Mock console
90+
mock_console = mocker.patch("commitloom.core.batch.console")
91+
mock_console.confirm_action.return_value = True
92+
93+
# Setup test data
8194
files = [mock_git_file("test.py")]
8295
mock_deps["git"].stage_files.side_effect = Exception("Git error")
8396

tests/test_main.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ def test_help_text(self, runner):
2121
assert result.exit_code == 0
2222
assert "Usage:" in result.output
2323

24-
def test_basic_run(self, runner, mock_loom):
24+
def test_basic_run(self, runner, mock_loom, mocker):
2525
"""Test basic run without arguments."""
26-
mock_loom.run.return_value = None
26+
# Mock the CommitLoom class
27+
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
28+
mock_commit_loom.return_value = mock_loom
29+
2730
result = runner.invoke(main)
2831
assert result.exit_code == 0
2932
mock_loom.run.assert_called_once_with(auto_commit=False, combine_commits=False, debug=False)
3033

31-
def test_all_flags(self, runner, mock_loom):
34+
def test_all_flags(self, runner, mock_loom, mocker):
3235
"""Test run with all flags enabled."""
33-
mock_loom.run.return_value = None
36+
# Mock the CommitLoom class
37+
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
38+
mock_commit_loom.return_value = mock_loom
39+
3440
result = runner.invoke(main, ["-y", "-c", "-d"])
3541
assert result.exit_code == 0
3642
mock_loom.run.assert_called_once_with(auto_commit=True, combine_commits=True, debug=True)
@@ -39,16 +45,24 @@ def test_all_flags(self, runner, mock_loom):
3945
class TestCliErrors:
4046
"""Test CLI error handling."""
4147

42-
def test_keyboard_interrupt(self, runner, mock_loom, capsys):
48+
def test_keyboard_interrupt(self, runner, mock_loom, mocker):
4349
"""Test handling of keyboard interrupt."""
50+
# Mock the CommitLoom class
51+
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
52+
mock_commit_loom.return_value = mock_loom
4453
mock_loom.run.side_effect = KeyboardInterrupt()
54+
4555
result = runner.invoke(main)
4656
assert result.exit_code == 1
47-
assert "Operation cancelled by user" in capsys.readouterr().err
57+
assert "Operation cancelled by user" in result.output
4858

49-
def test_general_error(self, runner, mock_loom, capsys):
59+
def test_general_error(self, runner, mock_loom, mocker):
5060
"""Test handling of general errors."""
61+
# Mock the CommitLoom class
62+
mock_commit_loom = mocker.patch("commitloom.__main__.CommitLoom")
63+
mock_commit_loom.return_value = mock_loom
5164
mock_loom.run.side_effect = Exception("Test error")
65+
5266
result = runner.invoke(main)
5367
assert result.exit_code == 1
54-
assert "Test error" in capsys.readouterr().err
68+
assert "Test error" in result.output

0 commit comments

Comments
 (0)