From 45af9ed80f231a0bbfa6da055d9a097a5d7c24ad Mon Sep 17 00:00:00 2001 From: Test User Date: Mon, 9 Dec 2024 16:38:58 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20enhance=20AI=20service=20an?= =?UTF-8?q?d=20batch=20processing=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ 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 --- tests/test_ai_service.py | 13 +++---------- tests/test_batch_processing.py | 21 +++++++++++++++++---- tests/test_main.py | 30 ++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/tests/test_ai_service.py b/tests/test_ai_service.py index 68f7b76..c906d49 100644 --- a/tests/test_ai_service.py +++ b/tests/test_ai_service.py @@ -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(): @@ -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" @@ -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", ) diff --git a/tests/test_batch_processing.py b/tests/test_batch_processing.py index fa6d0ae..0fbace8 100644 --- a/tests/test_batch_processing.py +++ b/tests/test_batch_processing.py @@ -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 @@ -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 @@ -65,10 +67,16 @@ 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) @@ -76,8 +84,13 @@ def test_user_cancellation(self, mock_deps, mocker, mock_git_file, batch_config) # 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") diff --git a/tests/test_main.py b/tests/test_main.py index 9487a53..2041fc2 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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) @@ -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