import pytest from datetime import datetime from unittest.mock import patch from src.models import Commit class TestCommitPatternAnalyzer: @pytest.fixture def mock_commit(self): return Commit( sha="abc123", message="Test commit", author_name="John Doe", author_email="john@test.com", committed_datetime=datetime(2024, 1, 15, 10, 30, 0), author_datetime=datetime(2024, 1, 15, 10, 30, 0), additions=50, deletions=10, files_changed=2, ) def test_analyze_empty_repo(self): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[]): from src.analyzers import CommitPatternAnalyzer analyzer = CommitPatternAnalyzer("/fake/path") result = analyzer.analyze(days=30) assert result.total_commits == 0 assert result.unique_authors == 0 def test_analyze_with_commits(self, mock_commit): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[mock_commit]): from src.analyzers import CommitPatternAnalyzer analyzer = CommitPatternAnalyzer("/fake/path") result = analyzer.analyze(days=30) assert result.total_commits == 1 assert result.unique_authors == 1 class TestCodeChurnAnalyzer: @pytest.fixture def mock_commit(self): return Commit( sha="abc123", message="Large commit", author_name="Jane", author_email="jane@test.com", committed_datetime=datetime.now(), author_datetime=datetime.now(), additions=1000, deletions=100, files_changed=5, ) def test_analyze_empty_repo(self): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[]): from src.analyzers import CodeChurnAnalyzer analyzer = CodeChurnAnalyzer("/fake/path") result = analyzer.analyze(days=30) assert result.total_additions == 0 assert result.total_deletions == 0 def test_churn_threshold(self, mock_commit): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[mock_commit]): from src.analyzers import CodeChurnAnalyzer analyzer = CodeChurnAnalyzer("/fake/path", churn_threshold=500) result = analyzer.analyze(days=30) assert len(result.high_churn_files) == 1 class TestVelocityAnalyzer: @pytest.fixture def mock_commits(self): commits = [] for i in range(7): commit = Commit( sha=f"sha{i}", message=f"Commit {i}", author_name="John", author_email="john@test.com", committed_datetime=datetime(2024, 1, i+1, 10, 0, 0), author_datetime=datetime(2024, 1, i+1, 10, 0, 0), additions=10, deletions=5, files_changed=1, ) commits.append(commit) return commits def test_analyze_empty_repo(self): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[]): from src.analyzers import VelocityAnalyzer analyzer = VelocityAnalyzer("/fake/path") result = analyzer.analyze(days=30) assert result.total_commits == 0 assert result.commits_per_day == 0.0 def test_velocity_calculation(self, mock_commits): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=mock_commits): from src.analyzers import VelocityAnalyzer analyzer = VelocityAnalyzer("/fake/path") result = analyzer.analyze(days=7) assert result.total_commits == 7 assert result.commits_per_day == 1.0 class TestRiskyCommitDetector: @pytest.fixture def mock_large_commit(self): return Commit( sha="abc123", message="Huge commit", author_name="John", author_email="john@test.com", committed_datetime=datetime.now(), author_datetime=datetime.now(), additions=1000, deletions=500, files_changed=10, ) @pytest.fixture def mock_merge_commit(self): return Commit( sha="def456", message="Merge branch", author_name="Jane", author_email="jane@test.com", committed_datetime=datetime.now(), author_datetime=datetime.now(), parents=["parent1", "parent2"], is_merge=True, ) def test_detect_large_commits(self, mock_large_commit): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[mock_large_commit]): from src.analyzers import RiskyCommitDetector detector = RiskyCommitDetector("/fake/path", large_commit_threshold=500) result = detector.analyze(days=30) assert len(result.large_commits) == 1 assert result.large_commits[0].risk_type == "large_commit" def test_detect_merge_commits(self, mock_merge_commit): with patch('src.analyzers.git_repository.GitRepository.get_commits', return_value=[mock_merge_commit]): from src.analyzers import RiskyCommitDetector detector = RiskyCommitDetector("/fake/path") result = detector.analyze(days=30) assert len(result.merge_commits) == 1 if __name__ == "__main__": pytest.main([__file__, "-v"])