from unittest.mock import Mock, patch from pr_review.config import PRAnalyzerFactory, terraform_infra_comment_filter class TestPRAnalyzerFactory: @patch('pr_review.config.BedrockClient') @patch('pr_review.config.GitHubClient') def test_create_analyzer_terraform_infra( self, mock_github_client_class, mock_bedrock_client_class ): """Test creating analyzer for terraform-infra repo.""" mock_github_client = Mock() mock_bedrock_client = Mock() mock_github_client_class.return_value = mock_github_client mock_bedrock_client_class.return_value = mock_bedrock_client analyzer = PRAnalyzerFactory.create_analyzer( 'terraform-infra', 'token', 'owner' ) # Verify clients were created correctly mock_github_client_class.assert_called_once_with( 'token', 'owner', 'terraform-infra' ) mock_bedrock_client_class.assert_called_once() # Verify analyzer type from pr_review.terraform_infra import TerraformInfraPRAnalyzer assert isinstance(analyzer, TerraformInfraPRAnalyzer) @patch('pr_review.config.BedrockClient') @patch('pr_review.config.GitHubClient') def test_create_analyzer_python_general( self, mock_github_client_class, mock_bedrock_client_class ): """Test creating analyzer for pull-request-review-tools repo.""" mock_github_client = Mock() mock_bedrock_client = Mock() mock_github_client_class.return_value = mock_github_client mock_bedrock_client_class.return_value = mock_bedrock_client analyzer = PRAnalyzerFactory.create_analyzer( 'pull-request-review-tools', 'token', 'owner' ) # Verify clients were created correctly mock_github_client_class.assert_called_once_with( 'token', 'owner', 'pull-request-review-tools' ) mock_bedrock_client_class.assert_called_once() # Verify analyzer type from pr_review.python_general import PythonGeneralPRAnalyzer assert isinstance(analyzer, PythonGeneralPRAnalyzer) @patch('pr_review.config.BedrockClient') @patch('pr_review.config.GitHubClient') def test_create_analyzer_database( self, mock_github_client_class, mock_bedrock_client_class ): """Test creating analyzer for database repo.""" mock_github_client = Mock() mock_bedrock_client = Mock() mock_github_client_class.return_value = mock_github_client mock_bedrock_client_class.return_value = mock_bedrock_client analyzer = PRAnalyzerFactory.create_analyzer('database', 'token', 'owner') # Verify clients were created correctly mock_github_client_class.assert_called_once_with('token', 'owner', 'database') mock_bedrock_client_class.assert_called_once() # Verify analyzer type from pr_review.database import DatabasePRAnalyzer assert isinstance(analyzer, DatabasePRAnalyzer) def test_create_analyzer_unknown_repo(self): """Test error for unknown repository.""" try: PRAnalyzerFactory.create_analyzer('unknown-repo', 'token', 'owner') assert False, 'Should have raised ValueError' except ValueError as e: assert 'Unknown repository: unknown-repo' in str(e) class TestCommentFilters: def test_terraform_infra_comment_filter(self): """Test Terraform plan comment filtering.""" comments = [ {'body': 'Ran Plan for test', 'created_at': '2024-01-02T00:00:00Z'}, {'body': 'Regular comment', 'created_at': '2024-01-02T00:00:00Z'}, { 'body': 'Continued plan output from previous comment.', 'created_at': '2024-01-02T00:00:00Z', }, { 'body': 'Ran Plan for test', 'created_at': '2023-12-31T00:00:00Z', }, # Before last commit ] last_commit_date = '2024-01-01T00:00:00Z' result = terraform_infra_comment_filter(comments, last_commit_date) assert len(result) == 2 assert 'Ran Plan for test' in result assert 'Continued plan output from previous comment.' in result